Skip to content

Instantly share code, notes, and snippets.

@pmatsinopoulos
Last active August 29, 2015 14:26
Show Gist options
  • Save pmatsinopoulos/8259e1a223ac70319e30 to your computer and use it in GitHub Desktop.
Save pmatsinopoulos/8259e1a223ac70319e30 to your computer and use it in GitHub Desktop.
Book&Table - Mobile API - Ruby client program using the Request a Tutor private end point
require 'rest-client'
user_id = 5
secret_key = 'secret key of user with id 5'
def calculate_canonical_string(content_type, content_md5, path, timestamp)
[ content_type,
content_md5,
path,
timestamp
].join(",")
end
def hmac_signature(content_type, content_md5, path, timestamp, secret_key)
canonical_string = calculate_canonical_string(content_type, content_md5, path, timestamp)
digest = OpenSSL::Digest.new('sha1')
hmac_digest = OpenSSL::HMAC.digest(digest, secret_key, canonical_string)
Base64.strict_encode64(hmac_digest)
end
params = ""
{"q[number_of_students]" => 1,
"q[standard_subject_id]" => 101,
"q[state_id]" => 301,
"q[student_grade]" => 'College',
"q[when]" => 'Immediately'}.each do |k, v|
item_built = "#{CGI.escape(k)}=#{CGI.escape(v.to_s)}"
if params == ""
params = item_built
else
params = "#{params}&#{item_built}"
end
end
path_with_params = "/mobile/1.0/request_tutor?#{params}"
timestamp = Time.now.utc.httpdate # Example: "Mon, 03 Aug 2015 20:15:56 GMT"
signature = hmac_signature('', '', path_with_params, timestamp, secret_key)
headers = {'X-BOOK-AND-TABLE-API-KEY-TYPE' => 'IOS_IPHONE',
'X-BOOK-AND-TABLE-API-KEY' => ENV['BOOK_AND_TABLE_API_KEY_IOS_IPHONE'],
'Accept' => 'application/json',
'Date' => timestamp,
'Authorization' => "APIAuth #{user_id}:#{signature}"}
request = RestClient::Request.new(url: "http://localhost:3000#{path_with_params}",
headers: headers,
method: :get)
response = request.execute
response_decoded = ActiveSupport::JSON.decode(response.body)
puts "The results of the request are: #{response_decoded.inspect}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment