Class: HttpClient::Resource
- Inherits:
-
Object
- Object
- HttpClient::Resource
- Defined in:
- lib/http_client/resource.rb
Overview
A class that can be instantiated for access to a HTTPful resource, including authentication.
Example:
resource = HttpClient::Resource.new('http://some/resource')
jpg = resource.get(:accept => 'image/jpg')
With HTTP basic authentication:
resource = HttpClient::Resource.new('http://protected/resource', :user => 'user', :password => 'password')
resource.delete
With a timeout (seconds):
HttpClient::Resource.new('http://slow', :timeout => 10)
With an open timeout (seconds):
HttpClient::Resource.new('http://behindfirewall', :open_timeout => 10)
You can also use resources to share common headers. For headers keys, symbols are converted to strings. Example:
resource = HttpClient::Resource.new('http://some/resource', :headers => { :client_version => 1 })
This header will be transported as X-Client-Version (notice the X prefix, capitalization and hyphens)
Use the [] syntax to allocate subresources:
site = HttpClient::Resource.new('http://example.com', :user => 'adam', :password => 'mypasswd')
site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#[](suburl) ⇒ Object
Construct a subresource, preserving authentication.
-
#concat_urls(url, suburl) ⇒ Object
:nodoc:.
- #delete(additional_headers = {}) ⇒ Object
- #get(additional_headers = {}) ⇒ Object
- #headers ⇒ Object
-
#initialize(url, options = {}, backwards_compatibility = nil) ⇒ Resource
constructor
A new instance of Resource.
- #open_timeout ⇒ Object
- #password ⇒ Object
- #post(payload, additional_headers = {}) ⇒ Object
- #put(payload, additional_headers = {}) ⇒ Object
- #timeout ⇒ Object
- #to_s ⇒ Object
- #user ⇒ Object
Constructor Details
#initialize(url, options = {}, backwards_compatibility = nil) ⇒ Resource
Returns a new instance of Resource.
39 40 41 42 43 44 45 46 |
# File 'lib/http_client/resource.rb', line 39 def initialize(url, ={}, backwards_compatibility=nil) @url = url if .class == Hash @options = else # compatibility with previous versions @options = { :user => , :password => backwards_compatibility } end end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
37 38 39 |
# File 'lib/http_client/resource.rb', line 37 def @options end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
37 38 39 |
# File 'lib/http_client/resource.rb', line 37 def url @url end |
Instance Method Details
#[](suburl) ⇒ Object
Construct a subresource, preserving authentication.
Example:
site = HttpClient::Resource.new('http://example.com', 'adam', 'mypasswd')
site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
This is especially useful if you wish to define your site in one place and call it in multiple locations:
def orders
HttpClient::Resource.new('http://example.com/orders', 'admin', 'mypasswd')
end
orders.get # GET http://example.com/orders
orders['1'].get # GET http://example.com/orders/1
orders['1/items'].delete # DELETE http://example.com/orders/1/items
Nest resources as far as you want:
site = HttpClient::Resource.new('http://example.com')
posts = site['posts']
first_post = posts['1']
comments = first_post['comments']
comments.post 'Hello', :content_type => 'text/plain'
132 133 134 |
# File 'lib/http_client/resource.rb', line 132 def [](suburl) self.class.new(concat_urls(url, suburl), ) end |
#concat_urls(url, suburl) ⇒ Object
:nodoc:
136 137 138 139 140 141 142 143 144 |
# File 'lib/http_client/resource.rb', line 136 def concat_urls(url, suburl) # :nodoc: url = url.to_s suburl = suburl.to_s if url.slice(-1, 1) == '/' or suburl.slice(0, 1) == '/' url + suburl else "#{url}/#{suburl}" end end |
#delete(additional_headers = {}) ⇒ Object
74 75 76 77 78 79 80 |
# File 'lib/http_client/resource.rb', line 74 def delete(additional_headers={}) Request.execute(.merge( :method => :delete, :url => url, :headers => headers.merge(additional_headers) )) end |
#get(additional_headers = {}) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/http_client/resource.rb', line 48 def get(additional_headers={}) Request.execute(.merge( :method => :get, :url => url, :headers => headers.merge(additional_headers) )) end |
#headers ⇒ Object
94 95 96 |
# File 'lib/http_client/resource.rb', line 94 def headers [:headers] || {} end |
#open_timeout ⇒ Object
102 103 104 |
# File 'lib/http_client/resource.rb', line 102 def open_timeout [:open_timeout] end |
#password ⇒ Object
90 91 92 |
# File 'lib/http_client/resource.rb', line 90 def password [:password] end |
#post(payload, additional_headers = {}) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/http_client/resource.rb', line 56 def post(payload, additional_headers={}) Request.execute(.merge( :method => :post, :url => url, :payload => payload, :headers => headers.merge(additional_headers) )) end |
#put(payload, additional_headers = {}) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/http_client/resource.rb', line 65 def put(payload, additional_headers={}) Request.execute(.merge( :method => :put, :url => url, :payload => payload, :headers => headers.merge(additional_headers) )) end |
#timeout ⇒ Object
98 99 100 |
# File 'lib/http_client/resource.rb', line 98 def timeout [:timeout] end |
#to_s ⇒ Object
82 83 84 |
# File 'lib/http_client/resource.rb', line 82 def to_s url end |
#user ⇒ Object
86 87 88 |
# File 'lib/http_client/resource.rb', line 86 def user [:user] end |