lib/fitbit_api/client.rb in fitbit_api-0.14.2 vs lib/fitbit_api/client.rb in fitbit_api-0.15.0

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + require 'fitbit_api/base' require 'fitbit_api/activities' require 'fitbit_api/breathing_rate' require 'fitbit_api/cardio_score' require 'fitbit_api/heart_rate' @@ -22,21 +24,21 @@ class Client attr_accessor :api_version, :unit_system, :locale, :scope, :snake_case_keys, :symbolize_keys, :auto_refresh_token, :on_token_refresh attr_reader :token, :user_id - def initialize(opts={}) + def initialize(opts = {}) validate_args(opts) assign_attrs(opts) set_client establish_token(opts) end # Returns the authorize endpoint URL of the OAuth2 provider. def auth_url - @client.auth_code.authorize_url(redirect_uri: @redirect_uri, scope: @scope) + @client.auth_code.authorize_url(redirect_uri: @redirect_uri, scope: format_scope(@scope)) end # Returns an OAuth2::AccessToken instance obtained from the given authorization code. # # @param auth_code [String] An authorization code @@ -75,31 +77,31 @@ # # @param path [String] The request path # @param params [Hash] The query parameters # @param opts [Hash] Additional request options (e.g. headers) - def get(path, params={}, opts={}, &block) + def get(path, params = {}, opts = {}, &block) request(:get, path, opts.merge(params: params), &block) end # Performs an authorized POST request to the configured API namespace. # # @param path [String] The request path # @param body [Hash] The request body # @param opts [Hash] Additional request options (e.g. headers) - def post(path, body={}, opts={}, &block) + def post(path, body = {}, opts = {}, &block) request(:post, path, opts.merge(body: body), &block) end # Performs an authorized DELETE request to the configured API namespace. # # @param path [String] The request path # @param params [Hash] The query parameters # @param opts [Hash] Additional request options (e.g. headers) - def delete(path, params={}, opts={}, &block) + def delete(path, params = {}, opts = {}, &block) request(:delete, path, opts.merge(params: params), &block) end private @@ -110,10 +112,11 @@ required_args.each do |arg| missing_args << arg if (opts[arg] || FitbitAPI.send(arg)).nil? end return if missing_args.empty? + raise FitbitAPI::InvalidArgumentError, "Required arguments: #{missing_args.join(', ')}" end def assign_attrs(opts) @@ -155,11 +158,11 @@ ) refresh_token! if @token.token.empty? end - def request(verb, path, opts={}, &block) + def request(verb, path, opts = {}, &block) 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]) @@ -172,10 +175,10 @@ process_keys!(response_body) end def auth_headers - { 'Authorization' => ('Basic ' + Base64.encode64(@client_id + ':' + @client_secret)) } + { 'Authorization' => "Basic #{Base64.encode64("#{@client_id}:#{@client_secret}")}" } end def default_request_headers { 'User-Agent' => "fitbit_api gem (v#{FitbitAPI::VERSION})",