Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add access token method #52

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions lib/fitgem/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
29 changes: 27 additions & 2 deletions spec/fitgem_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand All @@ -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
Expand Down