lib/fitbit_api/client.rb in fitbit_api-0.9.1 vs lib/fitbit_api/client.rb in fitbit_api-0.10.0
- old
+ new
@@ -12,12 +12,12 @@
require 'fitbit_api/water'
module FitbitAPI
class Client
attr_accessor :api_version, :unit_system, :locale, :scope,
- :snake_case_keys, :symbolize_keys
- attr_reader :user_id
+ :snake_case_keys, :symbolize_keys, :auto_refresh_token, :on_token_refresh
+ attr_reader :token, :user_id
def initialize(opts={})
validate_args(opts)
assign_attrs(opts)
set_client
@@ -30,63 +30,36 @@
def get_token(auth_code)
@token = @client.auth_code.get_token(
auth_code,
redirect_uri: @redirect_uri,
- headers: auth_header
+ headers: auth_headers
)
@user_id = @token.params['user_id']
@token
end
- def token
- @token.expired? ? refresh_token! : @token
- end
-
def refresh_token!
- @token = @token.refresh!(headers: auth_header)
+ @token = @token.refresh!(headers: auth_headers)
@user_id ||= @token.params['user_id']
+ on_token_refresh.call(@token) if on_token_refresh.respond_to?(:call)
+
@token
end
- def auth_header
- { 'Authorization' => ('Basic ' + Base64.encode64(@client_id + ':' + @client_secret)) }
- end
-
- def request_headers
- {
- 'User-Agent' => "fitbit_api gem (v#{FitbitAPI::VERSION})",
- 'Accept-Language' => @unit_system,
- 'Accept-Locale' => @locale
- }
- end
-
def get(path, opts={})
- params = opts.delete(:params) || {}
- response = token.get(("#{@api_version}/" + path), params: deep_keys_to_camel_case!(params), headers: request_headers).response
- object = MultiJson.load(response.body) unless response.status == 204
- process_keys!(object, opts)
+ request(:get, path, opts)
end
def post(path, opts={})
- response = token.post(("#{@api_version}/" + path), body: deep_keys_to_camel_case!(opts), headers: request_headers).response
- object = MultiJson.load(response.body) unless response.status == 204
- process_keys!(object, opts)
+ request(:post, path, opts)
end
def delete(path, opts={})
- response = token.delete(("#{@api_version}/" + path), headers: request_headers).response
- object = MultiJson.load(response.body) unless response.status == 204
- process_keys!(object, opts)
+ request(:delete, path, opts)
end
- def process_keys!(object, opts={})
- deep_keys_to_snake_case!(object) if (opts[:snake_case_keys] || snake_case_keys)
- deep_symbolize_keys!(object) if (opts[:symbolize_keys] || symbolize_keys)
- return object
- end
-
private
def validate_args(opts)
required_args = %i[client_id client_secret].freeze
missing_args = []
@@ -101,11 +74,12 @@
end
def assign_attrs(opts)
attrs = %i[client_id client_secret redirect_uri site_url
authorize_url token_url unit_system locale scope
- api_version snake_case_keys symbolize_keys].freeze
+ api_version snake_case_keys symbolize_keys
+ auto_refresh_token on_token_refresh].freeze
attrs.each do |attr|
instance_variable_set("@#{attr}", (opts[attr] || FitbitAPI.send(attr)))
end
@@ -136,8 +110,41 @@
refresh_token: opts[:refresh_token],
expires_at: opts[:expires_at]
)
refresh_token! if @token.token.empty?
+ end
+
+ def request(verb, path, opts={})
+ request_path = "#{@api_version}/#{path}"
+ request_options = opts.merge(headers: request_headers)
+
+ deep_keys_to_camel_case!(request_options[:params])
+ deep_keys_to_camel_case!(request_options[:body])
+
+ refresh_token! if auto_refresh_token && token.expired?
+
+ response = token.public_send(verb, request_path, request_options).response
+ object = MultiJson.load(response.body) unless response.status == 204
+
+ process_keys!(object, opts)
+ end
+
+ def auth_headers
+ { 'Authorization' => ('Basic ' + Base64.encode64(@client_id + ':' + @client_secret)) }
+ end
+
+ def request_headers
+ {
+ 'User-Agent' => "fitbit_api gem (v#{FitbitAPI::VERSION})",
+ 'Accept-Language' => @unit_system,
+ 'Accept-Locale' => @locale
+ }
+ end
+
+ def process_keys!(object, opts={})
+ deep_keys_to_snake_case!(object) if (opts[:snake_case_keys] || snake_case_keys)
+ deep_symbolize_keys!(object) if (opts[:symbolize_keys] || symbolize_keys)
+ return object
end
end
end