Class: Weary::Resource

Inherits:
Object
  • Object
show all
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

UnmetRequirementsError =
Class.new(StandardError)

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Requestable

#adapter, #has_middleware?, #headers, #pass_values_onto_requestable, #use, #user_agent

Constructor Details

- (Resource) initialize(method, uri)

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

- (Object) method (readonly)

Returns the value of attribute method



14
15
16
# File 'lib/weary/resource.rb', line 14

def method
  @method
end

Instance Method Details

- (Boolean) authenticates?

Does the Resource anticipate some sort of authentication parameters?

Returns:

  • (Boolean)


77
78
79
# File 'lib/weary/resource.rb', line 77

def authenticates?
  !!@authenticates
end

- (Object) basic_auth!(user = :username, pass = :password)

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

- (Object) defaults(hash = nil)

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

- (Object) expected_params

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

- (Boolean) expects?(param)

Does the Resource expect this parameter to be used to make the Request?

Returns:

  • (Boolean)


87
88
89
# File 'lib/weary/resource.rb', line 87

def expects?(param)
  expected_params.include? param.to_s
end

- (Boolean) meets_requirements?(params)

Given a hash of Request parameters, do they meet the requirements?

Returns:

  • (Boolean)


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

- (Object) oauth!(key = :consumer_key, token = :token, secret = :token_secret, consumer_secret = :consumer_secret)

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

- (Object) optional(*params)

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

- (Object) request(params = {}) {|request| ... } 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.

Yields:

  • (request)

Raises:



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 expand_url(pairs), params, credentials
  yield request if block_given?
  request
end

- (Object) required(*params)

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

- (Object) requirements

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

- (Object) url(uri = nil)

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