Class: Resourceful::Resource
Instance Attribute Summary collapse
-
#accessor ⇒ Object
readonly
Returns the value of attribute accessor.
Instance Method Summary collapse
- #default_header(temp_defaults = {}) ⇒ Object
-
#delete(header = {}) ⇒ Response
Performs a DELETE on the resource, following redirects as neccessary.
-
#effective_uri ⇒ Object
(also: #uri)
The uri used to identify this resource.
-
#get(header = {}) ⇒ Response
Performs a GET on the resource, following redirects as neccessary, and retriving it from the local cache if its available and valid.
- #head(header = {}) ⇒ Object
-
#host ⇒ Object
Returns the host for this Resource’s current uri.
-
#initialize(accessor, uri, default_header = {}) ⇒ Resource
constructor
Build a new resource for a uri.
- #logger ⇒ Object
-
#on_redirect {|callback| ... } ⇒ Object
When performing a redirect, this callback will be executed first.
-
#post(data = nil, header = {}) ⇒ Response
:call-seq: post(data = “”, :content_type => mime_type).
-
#put(data, header = {}) ⇒ Response
:call-seq: put(data = “”, :content_type => mime_type).
-
#request(method, data, header) ⇒ Object
Actually make the request.
-
#update_uri(uri) ⇒ Object
Updates the effective uri after following a permanent redirect.
Constructor Details
#initialize(accessor, uri, default_header = {}) ⇒ Resource
Build a new resource for a uri
15 16 17 18 19 |
# File 'lib/resourceful/resource.rb', line 15 def initialize(accessor, uri, default_header = {}) @accessor, @uris = accessor, [uri] @default_header = Resourceful::Header.new({'User-Agent' => Resourceful::RESOURCEFUL_USER_AGENT_TOKEN}.merge(default_header)) @on_redirect = nil end |
Instance Attribute Details
#accessor ⇒ Object (readonly)
Returns the value of attribute accessor.
7 8 9 |
# File 'lib/resourceful/resource.rb', line 7 def accessor @accessor end |
Instance Method Details
#default_header(temp_defaults = {}) ⇒ Object
32 33 34 |
# File 'lib/resourceful/resource.rb', line 32 def default_header(temp_defaults = {}) @default_header.merge(temp_defaults) end |
#delete(header = {}) ⇒ Response
Performs a DELETE on the resource, following redirects as neccessary.
130 131 132 |
# File 'lib/resourceful/resource.rb', line 130 def delete(header = {}) request(:delete, nil, header) end |
#effective_uri ⇒ Object Also known as: uri
The uri used to identify this resource. This is almost always the uri used to create the resource, but in the case of a permanent redirect, this will always reflect the lastest uri.
27 28 29 |
# File 'lib/resourceful/resource.rb', line 27 def effective_uri @uris.first end |
#get(header = {}) ⇒ Response
Performs a GET on the resource, following redirects as neccessary, and retriving it from the local cache if its available and valid.
77 78 79 |
# File 'lib/resourceful/resource.rb', line 77 def get(header = {}) request(:get, nil, header) end |
#head(header = {}) ⇒ Object
81 82 83 |
# File 'lib/resourceful/resource.rb', line 81 def head(header = {}) request(:head, nil, header) end |
#host ⇒ Object
Returns the host for this Resource’s current uri
37 38 39 |
# File 'lib/resourceful/resource.rb', line 37 def host Addressable::URI.parse(uri).host end |
#logger ⇒ Object
134 135 136 |
# File 'lib/resourceful/resource.rb', line 134 def logger accessor.logger end |
#on_redirect {|callback| ... } ⇒ Object
When performing a redirect, this callback will be executed first. If the callback returns true, then the redirect is followed, otherwise it is not. The request that triggered the redirect and the response will be passed into the block. This can be used to update any links on the client side.
Example:
.on_redirect do |req, resp|
post. = resp.header['Location']
end
62 63 64 65 66 67 68 |
# File 'lib/resourceful/resource.rb', line 62 def on_redirect(&callback) if block_given? @on_redirect = callback else @on_redirect end end |
#post(data = nil, header = {}) ⇒ Response
:call-seq:
post(data = "", :content_type => mime_type)
Performs a POST with the given data to the resource, following redirects as neccessary.
100 101 102 |
# File 'lib/resourceful/resource.rb', line 100 def post(data = nil, header = {}) request(:post, data, header) end |
#put(data, header = {}) ⇒ Response
:call-seq:
put(data = "", :content_type => mime_type)
Performs a PUT with the given data to the resource, following redirects as neccessary.
120 121 122 |
# File 'lib/resourceful/resource.rb', line 120 def put(data, header = {}) request(:put, data, header) end |
#request(method, data, header) ⇒ Object
Actually make the request
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/resourceful/resource.rb', line 139 def request(method, data, header) header = default_header.merge(header) ensure_content_type(data, header) if data data = StringIO.new(data) if data.kind_of?(String) log_request_with_time "#{method.to_s.upcase} [#{uri}]" do request = Request.new(method, self, data, header) request.fetch_response end end |
#update_uri(uri) ⇒ Object
Updates the effective uri after following a permanent redirect
42 43 44 |
# File 'lib/resourceful/resource.rb', line 42 def update_uri(uri) @uris.unshift(uri) end |