Class: Wrest::Uri

Inherits:
Object
  • Object
show all
Defined in:
lib/wrest/uri.rb

Overview

Wrest::Uri provides a simple api for REST calls. String#to_uri is a convenience method to build a Wrest::Uri from a string url. Note that a Wrest::Uri is immutable.

Basic HTTP Authentication is supported. Example:

"http://kaiwren:[email protected]/portal/1".to_uri
"http://coathangers.com/portal/1".to_uri(:username => 'kaiwren', :password => 'fupuppies')

The second form is preferred as it can handle passwords with special characters like ^ and @

You can find examples that use real APIs (like delicious) under the wrest/examples directory.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri_string, options = {}) ⇒ Uri

Returns a new instance of Uri.



26
27
28
29
30
31
32
33
# File 'lib/wrest/uri.rb', line 26

def initialize(uri_string, options = {})
  @options = options
  @uri_string = uri_string.clone
  @uri = URI.parse(uri_string)
  @options = options
  @username = (@options[:username] ||= @uri.user)
  @password = (@options[:password] ||= @uri.password)
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



25
26
27
# File 'lib/wrest/uri.rb', line 25

def password
  @password
end

#uriObject (readonly)

Returns the value of attribute uri.



25
26
27
# File 'lib/wrest/uri.rb', line 25

def uri
  @uri
end

#usernameObject (readonly)

Returns the value of attribute username.



25
26
27
# File 'lib/wrest/uri.rb', line 25

def username
  @username
end

Instance Method Details

#==(other) ⇒ Object



58
59
60
61
# File 'lib/wrest/uri.rb', line 58

def ==(other)
  return false if other.class != self.class
  return other.uri == self.uri && self.username == other.username && self.password == other.password
end

#[](path, options = nil) ⇒ Object

Build a new Wrest::Uri by appending path to the current uri. If the original Wrest::Uri has a username and password, that will be copied to the new Wrest::Uri as well.

Example:

uri = "https://localhost:3000/v1".to_uri
uri['/oogas/1'].get

To change the username and password on the new instance, simply pass them as an options map.

Example:

uri = "https://localhost:3000/v1".to_uri(:username => 'foo', :password => 'bar')
uri['/oogas/1', {:username => 'meh', :password => 'baz'}].get


50
51
52
# File 'lib/wrest/uri.rb', line 50

def [](path, options = nil)
  Uri.new(@uri_string+path, options || @options)
end

#delete(parameters = {}, headers = {}) ⇒ Object

Makes a DELETE request to this URI. This is a convenience API that creates a Wrest::Http::Delete, executes it and returns a Wrest::Http::Response.

Remember to escape all parameter strings if necessary, using URI.escape



95
96
97
# File 'lib/wrest/uri.rb', line 95

def delete(parameters = {}, headers = {})
  Http::Delete.new(self, parameters, headers, @options).invoke
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/wrest/uri.rb', line 54

def eql?(other)
  self == other
end

#full_pathObject

Provides the full path of a request. For example, for

http://localhost:3000/demons/1/chi?sort=true

this would return

/demons/1/chi?sort=true


114
115
116
# File 'lib/wrest/uri.rb', line 114

def full_path
  uri.request_uri
end

#get(parameters = {}, headers = {}) ⇒ Object

Make a GET request to this URI. This is a convenience API that creates a Wrest::Http::Get, executes it and returns a Wrest::Http::Response.

Remember to escape all parameter strings if necessary, using URI.escape



71
72
73
# File 'lib/wrest/uri.rb', line 71

def get(parameters = {}, headers = {})
  Http::Get.new(self, parameters, headers, @options).invoke
end

#hashObject



63
64
65
# File 'lib/wrest/uri.rb', line 63

def hash
  @uri.hash + @username.hash + @password.hash + 20090423
end

#hostObject



122
123
124
# File 'lib/wrest/uri.rb', line 122

def host
  uri.host
end

#https?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/wrest/uri.rb', line 105

def https?
  @uri.is_a?(URI::HTTPS)
end

#optionsObject

Makes an OPTIONS request to this URI. This is a convenience API that creates a Wrest::Http::Options, executes it and returns the Wrest::Http::Response.



101
102
103
# File 'lib/wrest/uri.rb', line 101

def options
  Http::Options.new(self, @options).invoke
end

#portObject



126
127
128
# File 'lib/wrest/uri.rb', line 126

def port
  uri.port
end

#post(body = '', headers = {}, parameters = {}) ⇒ Object

Makes a POST request to this URI. This is a convenience API that creates a Wrest::Http::Post, executes it and returns a Wrest::Http::Response.

Remember to escape all parameter strings if necessary, using URI.escape



87
88
89
# File 'lib/wrest/uri.rb', line 87

def post(body = '', headers = {}, parameters = {})
  Http::Post.new(self, body.to_s, headers, parameters, @options).invoke
end

#protocolObject



118
119
120
# File 'lib/wrest/uri.rb', line 118

def protocol
  uri.scheme
end

#put(body = '', headers = {}, parameters = {}) ⇒ Object

Make a PUT request to this URI. This is a convenience API that creates a Wrest::Http::Put, executes it and returns a Wrest::Http::Response.

Remember to escape all parameter strings if necessary, using URI.escape



79
80
81
# File 'lib/wrest/uri.rb', line 79

def put(body = '', headers = {}, parameters = {})
  Http::Put.new(self, body.to_s, headers, parameters, @options).invoke
end