Class: Chimps::Request

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

QueryRequest

Constant Summary collapse

DEFAULT_HEADERS =

Default headers to pass with every request.

{ :content_type => 'application/json', :accept => 'application/json' }

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

Options Hash (options):

  • params (Hash)

    Query parameters to include in the URL

  • data (Hash)

    Data to include in the request body

  • sign (true, false)

    Sign this request, raising an error on failure

  • sign_if_possible (true, false)

    Sign this request, no error on failure



46
47
48
49
50
51
52
53
54
# File 'lib/chimps/request.rb', line 46

def initialize path, options={}
  @path         = path
  @query_params = options[:query_params] || options[:params] || {}
  @data         = options[:data]         || {}
  @authentication_required      = [:authenticate, :authenticated, :authenticate_if_possible, :sign, :signed, :sign_if_possible].any? { |key| options.include?(key) }
  @forgive_authentication_error = options[:sign_if_possible] || options[:authenticate_if_possible]
  authenticate_if_necessary!
  super url_with_query_string
end

Instance Attribute Details

#dataObject

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

#pathObject

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_paramsObject

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)?

Returns:

  • (true, false)


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?

Returns:

  • (true, false)


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.

Parameters:

  • options (Hash) (defaults to: {})

Returns:



147
148
149
150
151
152
# File 'lib/chimps/request.rb', line 147

def delete options={}
  handle_exceptions do
    Chimps.log.info("DELETE #{url}")
    Response.new(super(DEFAULT_HEADERS.merge(options)))
  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.

Parameters:

  • options (Hash) (defaults to: {})

Returns:



104
105
106
107
108
109
# File 'lib/chimps/request.rb', line 104

def get options={}
  handle_exceptions do
    Chimps.log.info("GET #{url}")
    Response.new(super(DEFAULT_HEADERS.merge(options)))
  end
end

#hostString

The host to send requests to.

Returns:



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.

Parameters:

  • options (Hash) (defaults to: {})

Returns:



118
119
120
121
122
123
# File 'lib/chimps/request.rb', line 118

def post options={}
  handle_exceptions do
    Chimps.log.info("POST #{url}")
    Response.new(super(data_text, DEFAULT_HEADERS.merge(options)))
  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.

Parameters:

  • options (Hash) (defaults to: {})

Returns:



132
133
134
135
136
137
# File 'lib/chimps/request.rb', line 132

def put options={}
  handle_exceptions do
    Chimps.log.info("PUT #{url}")
    Response.new(super(data_text, DEFAULT_HEADERS.merge(options)))
  end
end

#query_stringString

Return the query string for this request, signed if necessary.

Returns:



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_stringString

Return the URL for this request with the (signed, if necessary) query string appended.

Returns:



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