class Withings::Connection
A convenience class for making get requests to WBS API. It verifies the response and raises ApiError if a call failed.
Public Class Methods
get_request(path, token, secret, params)
click to toggle source
# File lib/withings/connection.rb, line 17 def self.get_request(path, token, secret, params) signature = Withings::Connection.sign(base_uri + path, params, token, secret) params.merge!({:oauth_signature => signature}) response = self.get(path, :query => params) verify_response!(response, path, params) end
new(user)
click to toggle source
# File lib/withings/connection.rb, line 13 def initialize(user) @user = user end
Protected Class Methods
calculate_oauth_signature(method, url, params, oauth_token_secret)
click to toggle source
# File lib/withings/connection.rb, line 60 def self.calculate_oauth_signature(method, url, params, oauth_token_secret) # oauth signing is picky with sorting (based on a digest) params = params.to_a.map() do |item| [item.first.to_s, CGI.escape(item.last.to_s)] end.sort param_string = params.map() {|key, value| "#{key}=#{value}"}.join('&') base_string = [method, CGI.escape(url), CGI.escape(param_string)].join('&') secret = [Withings.consumer_secret, oauth_token_secret].join('&') digest = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), secret, base_string) Base64.encode64(digest).chomp.gsub( /\n/, '' ) end
oauth_nonce()
click to toggle source
# File lib/withings/connection.rb, line 56 def self.oauth_nonce rand(10 ** 30).to_s(16) end
oauth_signature_method()
click to toggle source
# File lib/withings/connection.rb, line 52 def self.oauth_signature_method 'HMAC-SHA1' end
oauth_timestamp()
click to toggle source
# File lib/withings/connection.rb, line 44 def self.oauth_timestamp Time.now.to_i end
oauth_version()
click to toggle source
# File lib/withings/connection.rb, line 48 def self.oauth_version '1.0' end
sign(url, params, token, secret)
click to toggle source
# File lib/withings/connection.rb, line 32 def self.sign(url, params, token, secret) params.merge!({ :oauth_consumer_key => Withings.consumer_key, :oauth_nonce => oauth_nonce, :oauth_signature_method => oauth_signature_method, :oauth_timestamp => oauth_timestamp, :oauth_version => oauth_version, :oauth_token => token }) calculate_oauth_signature('GET', url, params, secret) end
verify_response!(response, path, params)
click to toggle source
Verifies the status code in the JSON response and returns either the body element or raises ApiError
# File lib/withings/connection.rb, line 76 def self.verify_response!(response, path, params) if response['status'] == 0 response['body'] || response['status'] else raise Withings::ApiError.new(response['status'], path, params) end end
Public Instance Methods
get_request(path, params)
click to toggle source
# File lib/withings/connection.rb, line 25 def get_request(path, params) params.merge!({:userid => @user.user_id}) self.class.get_request(path, @user.oauth_token, @user.oauth_token_secret, params) end