Class: Chimps::QueryRequest

Inherits:
Request show all
Defined in:
lib/chimps/query_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

#body, #path, #query_params

Instance Method Summary collapse

Methods inherited from Request

#base_url, #delete, #encode, #encoded_body, #get, #handle_exceptions, #initialize, #obj_to_stripped_string, #post, #put, #query_string, #raw?, #should_encode?, #sign, #unsigned_query_string, #unsigned_query_string_stripped

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)


11
12
13
# File 'lib/chimps/query_request.rb', line 11

def authenticable?
  Chimps.config[:query][:key]
end

#authenticate?true

All Query API requests must be signed.

Returns:

  • (true)


37
38
39
# File 'lib/chimps/query_request.rb', line 37

def authenticate?
  return true
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.



47
48
49
50
51
52
# File 'lib/chimps/query_request.rb', line 47

def authenticate_if_necessary!
  return unless authenticate? && should_encode?
  raise Chimps::AuthenticationError.new("Query API key (Chimps.config[:query][:key]) from #{Chimps.config[:config]} or #{Chimps.config[:site_config]}") unless authenticable?
  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:



30
31
32
# File 'lib/chimps/query_request.rb', line 30

def host
  @host ||= Chimps.config[:query][:host]
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:



62
63
64
# File 'lib/chimps/query_request.rb', line 62

def signed_query_string
  unsigned_query_string
end

#url_with_query_stringObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/chimps/query_request.rb', line 15

def url_with_query_string
  qs = (query_string && query_string.size > 0 ? query_string : nil)
  case
  when qs.nil?
    base_url
  when base_url.include?("?")
    base_url + "&#{qs}"
  else
    base_url + "?#{qs}"
  end
end