lib/fitbit_api/client.rb in fitbit_api-0.10.2 vs lib/fitbit_api/client.rb in fitbit_api-0.11.0

- old
+ new

@@ -6,10 +6,11 @@ require 'fitbit_api/body' require 'fitbit_api/devices' require 'fitbit_api/food' require 'fitbit_api/friends' require 'fitbit_api/sleep' +require 'fitbit_api/subscriptions' require 'fitbit_api/user' require 'fitbit_api/water' module FitbitAPI class Client @@ -44,20 +45,20 @@ on_token_refresh.call(@token) if on_token_refresh.respond_to?(:call) @token end - def get(path, opts={}) - request(:get, path, opts) + def get(path, params={}, opts={}) + request(:get, path, opts.merge(params: params)) end - def post(path, opts={}) - request(:post, path, opts) + def post(path, body={}, opts={}) + request(:post, path, opts.merge(body: body)) end - def delete(path, opts={}) - request(:delete, path, opts) + def delete(path, params={}, opts={}) + request(:delete, path, opts.merge(params: params)) end private def validate_args(opts) @@ -114,37 +115,39 @@ refresh_token! if @token.token.empty? end def request(verb, path, opts={}) request_path = "#{@api_version}/#{path}" + request_headers = default_request_headers.merge(opts[:headers] || {}) 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 + response_body = MultiJson.load(response.body) unless response.status == 204 - process_keys!(object, opts) + process_keys!(response_body) end def auth_headers { 'Authorization' => ('Basic ' + Base64.encode64(@client_id + ':' + @client_secret)) } end - def request_headers + def default_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 + def process_keys!(object) + deep_keys_to_snake_case!(object) if snake_case_keys + deep_symbolize_keys!(object) if symbolize_keys + + object end end end