Class: Resourceful::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/resourceful/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(accessor, uri, default_header = {}) ⇒ Resource

Build a new resource for a uri

Parameters:



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

#accessorObject (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.

Returns:

Raises:



130
131
132
# File 'lib/resourceful/resource.rb', line 130

def delete(header = {})
  request(:delete, nil, header)
end

#effective_uriObject 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.

Returns:

  • Addressable::URI The current uri of the resource



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.

Returns:

  • (Response)

    The Response to the final request made.

Raises:



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

#hostObject

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

#loggerObject



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:

author_resource.on_redirect do |req, resp|
  post.author_uri = resp.header['Location']
end

Yield Parameters:

  • callback (request, response)

    The action to be executed when a request results in a redirect. Yields the current request and result objects to the callback.

Raises:

  • ArgumentError if called without a block



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.

Parameters:

  • data (String) (defaults to: nil)

    The body of the data to be posted

  • options (Hash)

    Options to pass into the request header. At the least, :content_type is required.

Returns:

  • (Response)

    The Response to the final request that was made.

Raises:

  • (ArgumentError)

    unless :content-type is specified in options

  • (UnsuccessfulHttpRequestError)

    unless the request is a success, ie the final request returned a 2xx response code



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.

Parameters:

  • data (String)

    The body of the data to be posted

  • options (Hash)

    Options to pass into the request header. At the least, :content_type is required.

Returns:

  • (Response)

    The response to the final request made.

Raises:

  • (ArgumentError)

    unless :content-type is specified in options

  • (UnsuccessfulHttpRequestError)

    unless the request is a success, ie the final request returned a 2xx response code



120
121
122
# File 'lib/resourceful/resource.rb', line 120

def put(data, header = {})
  request(:put, data, header)
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