Navigation Menu

Skip to content
This repository has been archived by the owner on Jul 19, 2022. It is now read-only.

Commit

Permalink
Allow gem to create authorize_url and token.
Browse files Browse the repository at this point in the history
  • Loading branch information
coop committed Sep 13, 2011
1 parent 8f15322 commit 7ff94b2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 41 deletions.
2 changes: 1 addition & 1 deletion lib/run_keeper/mock_requests.rb
Expand Up @@ -50,7 +50,7 @@ def stub_runkeeper_profile_request

def stub_runkeeper_token_request
stub_request(:post, "https://runkeeper.com/apps/token").
with :body => {"grant_type" => "authorization_code", "code" => /\w+/, "client_id" => /\w+/, "client_secret" => /\w+/, "redirect_uri" => "#{CONFIG['app_url']}/authorize/callback"},
with :body => {"grant_type" => "authorization_code", "code" => /\w+/, "client_id" => /\w+/, "client_secret" => /\w+/, "redirect_uri" => /\w+/},
:headers => {'Accept' => '*/*', 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => 'Ruby'}
end

Expand Down
15 changes: 14 additions & 1 deletion lib/run_keeper/request.rb
Expand Up @@ -10,10 +10,20 @@ def initialize client_id, client_secret, token
@client_id, @client_secret, @token = client_id, client_secret, token
end

def authorize_url redirect_uri
client('https://runkeeper.com').auth_code.authorize_url :redirect_uri => redirect_uri
end

def fitness_activities options = {}
ActivityRequest.new(self, options).get_activities
end

def get_token code, redirect_uri
client('https://runkeeper.com').auth_code.get_token(code, :redirect_uri => redirect_uri).token
rescue OAuth2::Error => e
raise Error.new(e.response)
end

def profile
Profile.new request('profile').parsed.merge(:userid => @user.userid)
end
Expand All @@ -34,10 +44,13 @@ def user

private
def access_token
client = OAuth2::Client.new @client_id, @client_secret, :site => 'https://api.runkeeper.com', :authorize_url => '/apps/authorize', :token_url => '/apps/token', :raise_errors => false
OAuth2::AccessToken.new client, @token
end

def client site = 'https://api.runkeeper.com'
OAuth2::Client.new @client_id, @client_secret, :site => site, :authorize_url => '/apps/authorize', :token_url => '/apps/token', :raise_errors => false
end

def parse_response response
if [200, 304].include? response.status
response
Expand Down
37 changes: 0 additions & 37 deletions test/mock_requests.rb

This file was deleted.

20 changes: 18 additions & 2 deletions test/request_test.rb
Expand Up @@ -7,6 +7,22 @@ def test_initialization_sets_client_id_and_client_secret
assert_includes runkeeper.instance_variables, :@token
end

def test_authorize_url_creates_a_valid_runkeeper_authorize_url
assert_equal 'https://runkeeper.com/apps/authorize?response_type=code&client_id=client_id&redirect_uri=http%3A%2F%2Fgoogle.com', runkeeper(nil).authorize_url('http://google.com')
end

def test_get_token_returns_a_token
stub_successful_runkeeper_token_request
assert_equal 'my_token', runkeeper(nil).get_token('code', 'http://google.com')
end

def test_get_token_raises_an_error_when_it_fails
stub_unsuccessful_runkeeper_token_request
assert_raises RunKeeper::Error do
runkeeper(nil).get_token('code', 'http://google.com')
end
end

def test_fitness_activities_with_a_valid_token_returns_an_array
stub_successful_runkeeper_fitness_activities_request
assert_instance_of Array, runkeeper.fitness_activities(:limit => 1)
Expand Down Expand Up @@ -86,7 +102,7 @@ def parsed; end
end
end

def runkeeper
Request.new 'client_id', 'client_secret', 'valid_token'
def runkeeper token = 'valid_token'
Request.new 'client_id', 'client_secret', token
end
end

0 comments on commit 7ff94b2

Please sign in to comment.