Class: Chimps::Request
- Defined in:
- lib/chimps/request.rb
Overview
A class to encapsulate requests made of Infochimps.
Essentialy a wrapper for RestClient::Resource with added funcionality for automatically signing requests and parsing Infochimps API responses.
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_HEADERS =
Default headers to pass with every request.
{ :content_type => 'application/json', :accept => 'application/json' }
Instance Attribute Summary collapse
-
#data ⇒ Object
Data to include in the body of the request.
-
#path ⇒ Object
Path of the URL to submit to.
-
#query_params ⇒ Object
Parameters to include in the query string of the URL to submit to.
Instance Method Summary collapse
-
#authenticable? ⇒ true, false
(also: #signable?)
Is this request authentiable (has the Chimps user specified an API key and secret in their configuration file)?.
-
#authenticate? ⇒ true, false
(also: #sign?)
Should the request be authenticated?.
-
#delete(options = {}) ⇒ Chimps::Response
Perform a DELETE request to this URL, returning a parsed response.
-
#get(options = {}) ⇒ Chimps::Response
Perform a GET request to this URL, returning a parsed response.
-
#host ⇒ String
The host to send requests to.
-
#initialize(path, options = {}) ⇒ Chimps::Request
constructor
Initialize a Request to the given
path. -
#post(options = {}) ⇒ Chimps::Response
Perform a POST request to this URL, returning a parsed response.
-
#put(options = {}) ⇒ Chimps::Response
Perform a PUT request to this URL, returning a parsed response.
-
#query_string ⇒ String
Return the query string for this request, signed if necessary.
-
#url_with_query_string ⇒ String
Return the URL for this request with the (signed, if necessary) query string appended.
Constructor Details
#initialize(path, options = {}) ⇒ Chimps::Request
Initialize a Request to the given path.
Query parameters and data can be passed in as hashes named :params and :data, respectively.
If :sign is passed in the options then the URL of this request will be signed with the Chimps user’s Infochimps API key and secret. Failure to properly sign will raise an error.
If :sign_if_possible is passed in the options then an attemp to sign the URL will be made though an error will not raise an error.
46 47 48 49 50 51 52 53 54 |
# File 'lib/chimps/request.rb', line 46 def initialize path, ={} @path = path @query_params = [:query_params] || [:params] || {} @data = [:data] || {} @authentication_required = [:authenticate, :authenticated, :authenticate_if_possible, :sign, :signed, :sign_if_possible].any? { |key| .include?(key) } @forgive_authentication_error = [:sign_if_possible] || [:authenticate_if_possible] authenticate_if_necessary! super url_with_query_string end |
Instance Attribute Details
#data ⇒ Object
Data to include in the body of the request. Must be a Hash.
23 24 25 |
# File 'lib/chimps/request.rb', line 23 def data @data end |
#path ⇒ Object
Path of the URL to submit to. Must be a String.
16 17 18 |
# File 'lib/chimps/request.rb', line 16 def path @path end |
#query_params ⇒ Object
Parameters to include in the query string of the URL to submit to. Must be a Hash.
20 21 22 |
# File 'lib/chimps/request.rb', line 20 def query_params @query_params end |
Instance Method Details
#authenticable? ⇒ true, false Also known as: signable?
Is this request authentiable (has the Chimps user specified an API key and secret in their configuration file)?
68 69 70 |
# File 'lib/chimps/request.rb', line 68 def authenticable? !Chimps::CONFIG[:site][:key].blank? && !Chimps::CONFIG[:site][:secret].blank? end |
#authenticate? ⇒ true, false Also known as: sign?
Should the request be authenticated?
59 60 61 |
# File 'lib/chimps/request.rb', line 59 def authenticate? @authentication_required end |
#delete(options = {}) ⇒ Chimps::Response
Perform a DELETE request to this URL, returning a parsed response.
Any headers in options will passed to RestClient::Resource.delete.
147 148 149 150 151 152 |
# File 'lib/chimps/request.rb', line 147 def delete ={} handle_exceptions do Chimps.log.info("DELETE #{url}") Response.new(super(DEFAULT_HEADERS.merge())) end end |
#get(options = {}) ⇒ Chimps::Response
Perform a GET request to this URL, returning a parsed response.
Any headers in options will passed to RestClient::Resource.get.
104 105 106 107 108 109 |
# File 'lib/chimps/request.rb', line 104 def get ={} handle_exceptions do Chimps.log.info("GET #{url}") Response.new(super(DEFAULT_HEADERS.merge())) end end |
#host ⇒ String
The host to send requests to.
76 77 78 |
# File 'lib/chimps/request.rb', line 76 def host @host ||= Chimps::CONFIG[:site][:host] end |
#post(options = {}) ⇒ Chimps::Response
Perform a POST request to this URL, returning a parsed response.
Any headers in options will passed to RestClient::Resource.post.
118 119 120 121 122 123 |
# File 'lib/chimps/request.rb', line 118 def post ={} handle_exceptions do Chimps.log.info("POST #{url}") Response.new(super(data_text, DEFAULT_HEADERS.merge())) end end |
#put(options = {}) ⇒ Chimps::Response
Perform a PUT request to this URL, returning a parsed response.
Any headers in options will passed to RestClient::Resource.put.
132 133 134 135 136 137 |
# File 'lib/chimps/request.rb', line 132 def put ={} handle_exceptions do Chimps.log.info("PUT #{url}") Response.new(super(data_text, DEFAULT_HEADERS.merge())) end end |
#query_string ⇒ String
Return the query string for this request, signed if necessary.
93 94 95 |
# File 'lib/chimps/request.rb', line 93 def query_string (authenticate? && authenticable?) ? signed_query_string : unsigned_query_string end |
#url_with_query_string ⇒ String
Return the URL for this request with the (signed, if necessary) query string appended.
84 85 86 87 88 |
# File 'lib/chimps/request.rb', line 84 def url_with_query_string base_url = File.join(host, path) base_url += "?#{query_string}" unless query_string.blank? base_url end |