Class Wrest::Uri

  1. lib/wrest/multipart.rb
  2. lib/wrest/uri.rb
  3. show all
Parent: Object

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:fupuppies@coathangers.com/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.

Included modules

  1. Multipart
  2. Http::ConnectionFactory

Attributes

password [R]
query [R]
uri [R]
uri_path [R]
uri_string [R]
username [R]

Public class methods

new (uri_string, options = {})

See Wrest::Native::Request for the available options and their default values.

[show source]
    # File lib/wrest/uri.rb, line 28
28:     def initialize(uri_string, options = {})
29:         @options = options
30:         @uri_string = uri_string.to_s
31:         @uri = URI.parse(@uri_string)
32:         uri_scheme = URI.split(@uri_string)
33:         @uri_path = uri_scheme[-4].split('?').first || ''
34:         @uri_path = (@uri_path.empty? ? '/' : @uri_path) 
35:         @query = uri_scheme[-2] || ''
36:         @username = (@options[:username] ||= @uri.user)
37:         @password = (@options[:password] ||= @uri.password)
38:     end

Public instance methods

== (other)
[show source]
    # File lib/wrest/uri.rb, line 74
74:     def ==(other)
75:       return false if other.class != self.class
76:       return other.uri == self.uri && self.username == other.username && self.password == other.password
77:     end
[] (path, options = nil)

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
[show source]
    # File lib/wrest/uri.rb, line 60
60:     def [](path, options = nil)
61:       Uri.new(uri + File.join(uri_path, path), options || @options)
62:     end
clone (opts = {})

Clones a Uri, building a new instance with exactly the same uri string. You can however change the Uri options or add new ones.

[show source]
    # File lib/wrest/uri.rb, line 66
66:     def clone(opts = {})
67:       Uri.new(@uri_string, @options.merge(opts))
68:     end
delete (parameters = {}, headers = {}, &block)

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

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

[show source]
     # File lib/wrest/uri.rb, line 132
132:     def delete(parameters = {}, headers = {}, &block)
133:       Http::Delete.new(self, parameters, headers, block ? @options.merge(:callback_block => block) : @options).invoke
134:     end
eql? (other)
[show source]
    # File lib/wrest/uri.rb, line 70
70:     def eql?(other)
71:       self == other
72:     end
full_path ()

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
[show source]
     # File lib/wrest/uri.rb, line 151
151:     def full_path
152:       uri.request_uri
153:     end
get (parameters = {}, headers = {}, &block)

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

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

[show source]
    # File lib/wrest/uri.rb, line 94
94:     def get(parameters = {}, headers = {}, &block)
95:       Http::Get.new(self, parameters, headers, block ? @options.merge(:callback_block => block) : @options).invoke
96:     end
hash ()
[show source]
    # File lib/wrest/uri.rb, line 79
79:     def hash
80:       @uri.hash + @username.hash + @password.hash + 20090423
81:     end
host ()
[show source]
     # File lib/wrest/uri.rb, line 159
159:     def host
160:       uri.host
161:     end
https? ()
[show source]
     # File lib/wrest/uri.rb, line 142
142:     def https? 
143:       @uri.is_a?(URI::HTTPS)
144:     end
options ()

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

[show source]
     # File lib/wrest/uri.rb, line 138
138:     def options
139:       Http::Options.new(self, @options).invoke
140:     end
port ()
[show source]
     # File lib/wrest/uri.rb, line 163
163:     def port
164:       uri.port
165:     end
post (body = '', headers = {}, parameters = {}, &block)

Makes a POST request to this URI. This is a convenience API that creates a Wrest::Native::Post, executes it and returns a Wrest::Native::Response. Note that sending an empty body will blow up if you’re using libcurl.

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

[show source]
     # File lib/wrest/uri.rb, line 111
111:     def post(body = '', headers = {}, parameters = {}, &block)
112:       Http::Post.new(self, body.to_s, headers, parameters, block ? @options.merge(:callback_block => block) : @options).invoke
113:     end
post_form (parameters = {}, headers = {}, &block)

Makes a POST request to this URI. This is a convenience API that mimics a form being posted; some allegly RESTful APIs like FCBK require this.

Form encoding involves munging the parameters into a string and placing them in the body, as well as setting the Content-Type header to application/x-www-form-urlencoded

[show source]
     # File lib/wrest/uri.rb, line 122
122:     def post_form(parameters = {}, headers = {}, &block)
123:       headers = headers.merge(Wrest::H::ContentType => Wrest::T::FormEncoded)
124:       body = parameters.to_query
125:       Http::Post.new(self, body, headers, {}, block ? @options.merge(:callback_block => block) : @options).invoke
126:     end
protocol ()
[show source]
     # File lib/wrest/uri.rb, line 155
155:     def protocol
156:       uri.scheme
157:     end
put (body = '', headers = {}, parameters = {}, &block)

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

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

[show source]
     # File lib/wrest/uri.rb, line 102
102:     def put(body = '', headers = {}, parameters = {}, &block)
103:       Http::Put.new(self, body.to_s, headers, parameters, block ? @options.merge(:callback_block => block) : @options).invoke
104:     end
to_s ()

This produces exactly the same string as the Wrest::Uri was constructed with. If the orignial URI contained a HTTP username and password, that too will show up, so be careful if using this for logging.

[show source]
    # File lib/wrest/uri.rb, line 86
86:     def to_s
87:       uri_string
88:     end
to_template (pattern)
[show source]
    # File lib/wrest/uri.rb, line 40
40:     def to_template(pattern)
41:       template_pattern = URI.join(uri_string,pattern).to_s
42:       UriTemplate.new(template_pattern, @options)
43:     end