From 7ff94b252c6d5318815f4ddb8ca22e9db5c26469 Mon Sep 17 00:00:00 2001 From: Tim Cooper Date: Tue, 13 Sep 2011 19:07:57 +1000 Subject: [PATCH] Allow gem to create authorize_url and token. --- lib/run_keeper/mock_requests.rb | 2 +- lib/run_keeper/request.rb | 15 ++++++++++++- test/mock_requests.rb | 37 --------------------------------- test/request_test.rb | 20 ++++++++++++++++-- 4 files changed, 33 insertions(+), 41 deletions(-) delete mode 100644 test/mock_requests.rb diff --git a/lib/run_keeper/mock_requests.rb b/lib/run_keeper/mock_requests.rb index fd7f1c4..8e0e01b 100644 --- a/lib/run_keeper/mock_requests.rb +++ b/lib/run_keeper/mock_requests.rb @@ -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 diff --git a/lib/run_keeper/request.rb b/lib/run_keeper/request.rb index ff7af79..fa7edb9 100644 --- a/lib/run_keeper/request.rb +++ b/lib/run_keeper/request.rb @@ -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 @@ -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 diff --git a/test/mock_requests.rb b/test/mock_requests.rb deleted file mode 100644 index e8008a1..0000000 --- a/test/mock_requests.rb +++ /dev/null @@ -1,37 +0,0 @@ -module MockRequests - def stub_successful_runkeeper_fitness_activities_request - stub_successful_runkeeper_user_request - stub_runkeeper_fitness_activities_request.to_return :status => 200, :body => {"size" => "4", "items" => [{"type" => "Running", "start_time" => "Thu, 25 Aug 2011 17:41:28", "total_distance" => "5492.22273600001", "duration" => "1743.946", "uri" => "/activities/39"}, {"type" => "Running", "start_time" => "Thu, 1 Sep 2011 17:41:28", "total_distance" => "5492.22273600001", "duration" => "1743.946", "uri" => "/activities/40"}, {"type" => "Running", "start_time" => "Fri, 2 Sep 2011 17:41:28", "total_distance" => "5492.22273600001", "duration" => "2090.123", "uri" => "/activities/41"}, {"type" => "Running", "start_time" => "Wed, 7 Sep 2011 17:41:28", "total_distance" => "5492.22273600001", "duration" => "1743.946", "uri" => "/activities/42"}], "next" => "/fitnessActivities?page=1"}, :headers => {"content-type" => "application/vnd.com.runkeeper.FitnessActivityFeed+json;charset=ISO-8859-1"} - end - - def stub_successful_runkeeper_fitness_activities_page_2_request - stub_request(:get, "https://api.runkeeper.com/fitnessActivities?page=1"). - with(:headers => {'Accept' => 'application/vnd.com.runkeeper.FitnessActivityFeed+json', 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => /Bearer \w+/, 'User-Agent'=>'Ruby'}). - to_return(:status => 200, :body => {"size" => "2", "items" => [{"type" => "Running", "start_time" => "Tue, 23 Aug 2011 17:41:28", "total_distance" => "5492.22273600001", "duration" => "1743.946", "uri" => "/activities/38"}, {"type" => "Running", "start_time" => "Sun, 21 Aug 2011 17:41:28", "total_distance" => "5492.22273600001", "duration" => "1743.946", "uri" => "/activities/37"}]}, :headers => {"content-type" => "application/vnd.com.runkeeper.FitnessActivityFeed+json;charset=ISO-8859-1"}) - end - - def stub_successful_runkeeper_profile_request - stub_successful_runkeeper_user_request - stub_runkeeper_profile_request.to_return :status => 200, :body => {"name" => "John Doe", "location" => "Anytown, USA", "athlete_type" => "Runner", "goal" => "To get off the couch", "gender" => "M", "birthday" => "Sat, Jan 1 2011 00:00:00", "elite" => "true", "profile" => "http://www.runkeeper.com/user/JohnDoe", "small_picture" => "http://www.runkeeper.com/user/JohnDoe/small.jpg", "normal_picture" => "http://www.runkeeper.com/user/JohnDoe/normal.jpg", "medium_picture" => "http://www.runkeeper.com/user/JohnDoe/medium.jpg", "large_picture" => "http://www.runkeeper.com/user/JohnDoe/large.jpg"}, :headers => {"content-type" => "application/vnd.com.runkeeper.profile+json;charset=ISO-8859-1"} - end - - def stub_successful_runkeeper_user_request - stub_runkeeper_user_request.to_return :status => 200, :body => {"userID" => /\d+/, "profile" => "/profile", "settings" => "/settings", "fitness_activities" => "/fitnessActivities", "background_activities" => "/backgroundActivities", "sleep" => "/sleep", "nutrition" => "/nutrition", "weight" => "/weight", "general_measurements" => "/generalMeasurements", "diabetes" => "/diabetes", "records" => "/records", "team" => "/team", "strength_training_activities" => "/strengthTrainingActivities"}, :headers => {'content-type' => 'application/vnd.com.runkeeper.user+json;charset=ISO-8859-1'} - end - -private - def stub_runkeeper_fitness_activities_request - stub_request(:get, "https://api.runkeeper.com/fitnessActivities"). - with :headers => {'Accept'=>'application/vnd.com.runkeeper.FitnessActivityFeed+json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => /Bearer \w+/, 'User-Agent'=>'Ruby'} - end - - def stub_runkeeper_profile_request - stub_request(:get, "https://api.runkeeper.com/profile"). - with :headers => {'Accept'=>'application/vnd.com.runkeeper.Profile+json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => /Bearer \w+/, 'User-Agent'=>'Ruby'} - end - - def stub_runkeeper_user_request - stub_request(:get, "https://api.runkeeper.com/user"). - with :headers => {'Accept'=>'application/vnd.com.runkeeper.User+json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization' => /Bearer \w+/, 'User-Agent'=>'Ruby'} - end -end diff --git a/test/request_test.rb b/test/request_test.rb index 102e0ae..7b1a944 100644 --- a/test/request_test.rb +++ b/test/request_test.rb @@ -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) @@ -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