Class: Weary::Resource
- Inherits:
-
Object
- Object
- Weary::Resource
- Includes:
- Requestable
- Defined in:
- lib/weary/resource.rb
Overview
A description of a resource made available by an HTTP request. That description is composed primarily of a url template, the HTTP method to retrieve the resource and some constraints on the parameters necessary to complete the request.
Constant Summary collapse
- UnmetRequirementsError =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#method ⇒ Object
readonly
Returns the value of attribute method.
Instance Method Summary collapse
-
#authenticates? ⇒ Boolean
Does the Resource anticipate some sort of authentication parameters?.
-
#basic_auth!(user = :username, pass = :password) ⇒ Object
Tell the Resource to anticipate Basic Authentication.
-
#defaults(hash = nil) ⇒ Object
An accessor to set default paramers to send to the resource.
-
#expected_params ⇒ Object
The keys expected as parameters to the Request.
-
#expects?(param) ⇒ Boolean
Does the Resource expect this parameter to be used to make the Request?.
-
#initialize(method, uri) ⇒ Resource
constructor
A new instance of Resource.
-
#meets_requirements?(params) ⇒ Boolean
Given a hash of Request parameters, do they meet the requirements?.
-
#oauth!(key = :consumer_key, token = :token, secret = :token_secret, consumer_secret = :consumer_secret) ⇒ Object
Tell the Resource to anticipate OAuth.
-
#optional(*params) ⇒ Object
An accessor to set optional parameters permitted by the resource.
-
#request(params = {}) {|request| ... } ⇒ Object
(also: #build)
Construct the request from the given parameters.
-
#required(*params) ⇒ Object
An accessor to set optional parameters required in order to access the resource.
-
#requirements ⇒ Object
The parameter keys that must be fulfilled to create the Request.
-
#url(uri = nil) ⇒ Object
An accessor method to set the url to retrieve the resource.
Methods included from Requestable
#adapter, #has_middleware?, #headers, #pass_values_onto_requestable, #use, #user_agent
Constructor Details
#initialize(method, uri) ⇒ Resource
Returns a new instance of Resource.
16 17 18 19 |
# File 'lib/weary/resource.rb', line 16 def initialize(method, uri) @method = method self.url uri end |
Instance Attribute Details
#method ⇒ Object (readonly)
Returns the value of attribute method.
14 15 16 |
# File 'lib/weary/resource.rb', line 14 def method @method end |
Instance Method Details
#authenticates? ⇒ Boolean
Does the Resource anticipate some sort of authentication parameters?
77 78 79 |
# File 'lib/weary/resource.rb', line 77 def authenticates? !!@authenticates end |
#basic_auth!(user = :username, pass = :password) ⇒ Object
Tell the Resource to anticipate Basic Authentication. Optionally, tell the Resource what parameters to use as credentials.
user - The parameter in which to expect the username (defaults to :username) pass - The parameter in which to expect the password (defaults to :password)
59 60 61 62 |
# File 'lib/weary/resource.rb', line 59 def basic_auth!(user = :username, pass = :password) @authenticates = :basic_auth @credentials = [user, pass] end |
#defaults(hash = nil) ⇒ Object
An accessor to set default paramers to send to the resource.
49 50 51 52 |
# File 'lib/weary/resource.rb', line 49 def defaults(hash=nil) @defaults = hash unless hash.nil? @defaults ||= {} end |
#expected_params ⇒ Object
The keys expected as parameters to the Request.
82 83 84 |
# File 'lib/weary/resource.rb', line 82 def expected_params (defaults.keys | optional | required).map(&:to_s).uniq end |
#expects?(param) ⇒ Boolean
Does the Resource expect this parameter to be used to make the Request?
87 88 89 |
# File 'lib/weary/resource.rb', line 87 def expects?(param) expected_params.include? param.to_s end |
#meets_requirements?(params) ⇒ Boolean
Given a hash of Request parameters, do they meet the requirements?
97 98 99 |
# File 'lib/weary/resource.rb', line 97 def meets_requirements?(params) requirements.reject {|k| params.keys.map(&:to_s).include? k.to_s }.empty? end |
#oauth!(key = :consumer_key, token = :token, secret = :token_secret, consumer_secret = :consumer_secret) ⇒ Object
Tell the Resource to anticipate OAuth. Optionally, tell the Resource what parameters to use as the consumer key and access token
key - The parameter in which to expect the consumer key (defaults to
:consumer_key)
token - The parameter in which to expect the user access token (defaults
to :token)
71 72 73 74 |
# File 'lib/weary/resource.rb', line 71 def oauth!(key = :consumer_key, token = :token, secret = :token_secret, consumer_secret = :consumer_secret) @authenticates = :oauth @credentials = [key, token, secret, consumer_secret] end |
#optional(*params) ⇒ Object
An accessor to set optional parameters permitted by the resource.
Returns an Array of parameters.
34 35 36 37 |
# File 'lib/weary/resource.rb', line 34 def optional(*params) @optional = params unless params.empty? @optional ||= [] end |
#request(params = {}) {|request| ... } ⇒ Object Also known as: build
Construct the request from the given parameters.
Yields the Request
Returns the Request. Raises a Weary::Resource::UnmetRequirementsError if the requirements
are not met.
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/weary/resource.rb', line 108 def request(params={}) normalize_parameters params raise UnmetRequirementsError, "Required parameters: #{requirements}" \ unless meets_requirements? params credentials = pull_credentials params pairs = pull_url_pairs params request = construct_request (pairs), params, credentials yield request if block_given? request end |
#required(*params) ⇒ Object
An accessor to set optional parameters required in order to access the resource.
Returns an Array of parameters.
43 44 45 46 |
# File 'lib/weary/resource.rb', line 43 def required(*params) @required = params unless params.empty? @required ||= [] end |
#requirements ⇒ Object
The parameter keys that must be fulfilled to create the Request.
92 93 94 |
# File 'lib/weary/resource.rb', line 92 def requirements required.map(&:to_s) | url.keys end |
#url(uri = nil) ⇒ Object
An accessor method to set the url to retrieve the resource. Use either brackets to delimit url variables or prefix them with a colon, like Sinatra.
Returns an Addressable::Template
26 27 28 29 |
# File 'lib/weary/resource.rb', line 26 def url(uri=nil) @uri = Addressable::Template.new(uri.gsub(/:(?![0-9])(\w+)/) { "{#{$1}}" }) unless uri.nil? @uri end |