diff --git a/lib/fitgem/client.rb b/lib/fitgem/client.rb index 90cf028..3b0e9a3 100644 --- a/lib/fitgem/client.rb +++ b/lib/fitgem/client.rb @@ -119,6 +119,37 @@ def initialize(opts) @api_locale = opts[:locale] || Fitgem::ApiLocale.US end + # Get authorize_url + # + # @param [String] Space separated access scope (i.e. 'activity nutrition') + # @param [String] Redirect URI + # @return [String] Authorize URL + def authorize_url(scope, redirect_uri) + consumer.auth_code.authorize_url(redirect_uri: redirect_uri, scope: scope) + end + + # Get token + # + # @param [String] Authorization code + # @param [String] Redirect URI + # @return [OAuth2::AccessToken] Accesstoken and refresh token + def get_token(authorization_code, redirect_uri) + consumer.auth_code.get_token(authorization_code, headers: auth_header, redirect_uri: redirect_uri) + end + + # Refresh access token + # + # @param [String] Refresh token + # @return [OAuth2::AccessToken] Accesstoken and refresh token + def refresh_access_token!(refresh_token) + new_access_token = OAuth2::AccessToken.new(consumer, @token, refresh_token: refresh_token) + # refresh! method return new object not itself and not change itself + new_token = new_access_token.refresh!(headers: auth_header) + @token = new_token.token + @access_token = nil + new_token + end + private def consumer @@ -182,5 +213,9 @@ def default_headers 'Accept-Locale' => @api_locale } end + + def auth_header + {Authorization: "Basic #{ Base64.encode64("#{ @consumer_key }:#{ @consumer_secret }") }" } + end end end diff --git a/spec/fitgem_client_spec.rb b/spec/fitgem_client_spec.rb index eb1db5d..8c45930 100644 --- a/spec/fitgem_client_spec.rb +++ b/spec/fitgem_client_spec.rb @@ -4,12 +4,24 @@ let(:access_token) { double 'Access Token', :get => response, :request => response } + let(:consumer_key) { '12345' } + let(:consumer_secret) { '67890' } let(:client) { Fitgem::Client.new({ - :consumer_key => '12345', - :consumer_secret => '67890' + :consumer_key => consumer_key, + :consumer_secret => consumer_secret }) } let(:response) { double :body => {:foo => :bar}.to_json, :status => 200 } let(:consumer) { double 'Consumer' } + let(:redirect_uri) { 'http://example.com/redirect_url' } + let(:auth_url) { 'http://example.com/auth_url' } + let(:auth_code) { 'code' } + let(:scope) { 'activity nutrition heartrate location nutrition profile settings sleep social weight' } + let(:auth_header) { + { + Authorization: "Basic #{Base64.encode64("#{consumer_key}:#{consumer_secret}")}" + } + } + let(:auth_token) { 'token' } before :each do allow(OAuth2::Client).to receive(:new).with('12345', '67890', { @@ -18,6 +30,19 @@ :authorize_url => "https://www.fitbit.com/oauth2/authorize" }).and_return(consumer) allow(OAuth2::AccessToken).to receive(:new).and_return(access_token) + + allow(consumer).to receive_message_chain('auth_code.authorize_url'). + with({scope: scope, redirect_uri: redirect_uri}).and_return(auth_url) + allow(consumer).to receive_message_chain('auth_code.get_token'). + with(auth_code, {headers: auth_header, redirect_uri: redirect_uri}).and_return(auth_token) + end + + it 'get authorize url' do + expect(client.authorize_url(scope, redirect_uri)).to eq auth_url + end + + it 'get token'do + expect(client.get_token(auth_code, redirect_uri)).to eq auth_token end it 'returns JSON from the request' do