Class: Chimps::QueryRequest

Inherits:
Request show all
Defined in:
lib/chimps/request.rb

Overview

A class to encapsulate requests made against the Infochimps paid query API.

Constant Summary

Constants inherited from Request

Request::DEFAULT_HEADERS

Instance Attribute Summary

Attributes inherited from Request

#data, #path, #query_params

Instance Method Summary collapse

Methods inherited from Request

#authenticate?, #delete, #get, #initialize, #post, #put, #query_string, #url_with_query_string

Constructor Details

This class inherits a constructor from Chimps::Request

Instance Method Details

#authenticable?true, false

Is this request authentiable (has the Chimps user specified an API key and secret in their configuration file)?

Returns:

  • (true, false)


252
253
254
# File 'lib/chimps/request.rb', line 252

def authenticable?
  !Chimps::CONFIG[:query][:key].blank? && !Chimps::CONFIG[:query][:secret].blank?
end

#authenticate_if_necessary!Object

Authenticate this request by stuffing the :requested_at and :apikey properties into its :query_params hash.

Will do nothing at all if Chimps::Request#authenticate? returns false.



269
270
271
272
273
274
# File 'lib/chimps/request.rb', line 269

def authenticate_if_necessary!
  return unless authenticate?
  raise Chimps::AuthenticationError.new("API key or secret missing from #{CONFIG[:identity_file]}") unless (authenticable? || @forgive_authentication_error)
  query_params[:requested_at] = Time.now.to_i.to_s
  query_params[:apikey]       = Chimps::CONFIG[:query][:key]
end

#hostString

The host to send requests to.

Returns:



259
260
261
# File 'lib/chimps/request.rb', line 259

def host
  @host ||= Chimps::CONFIG[:query][:host]
end

#sign(string) ⇒ String

Sign string by concatenting it with the secret and computing the MD5 digest of the whole thing.

Parameters:

Returns:

Raises:



281
282
283
284
285
# File 'lib/chimps/request.rb', line 281

def sign string
  raise Chimps::AuthenticationError.new("No API secret stored in #{CONFIG[:identity_file]}.") unless (authenticable? || @forgive_authentication_error)
  require 'digest/md5'
  Digest::MD5.hexdigest(string + CONFIG[:key][:secret])
end

#signed_query_stringString

Append the signature to the unsigned query string.

The signature made from the Chimps user’s API secret and either the query string text (stripped of & and =) for GET and DELETE requests or the request body for POST and PUT requests.

Returns:



295
296
297
# File 'lib/chimps/request.rb', line 295

def signed_query_string
  unsigned_query_string
end