Class: Runkeeper::Connection
- Inherits:
-
Object
- Object
- Runkeeper::Connection
- Includes:
- HTTParty
- Defined in:
- lib/runkeeper/connection.rb,
lib/runkeeper/token_generator.rb
Constant Summary collapse
- ACCEPT_HEADERS =
{ :user => "application/vnd.com.runkeeper.User+json", :fitness_activities => "application/vnd.com.runkeeper.FitnessActivityFeed+json", :past_activities => "application/vnd.com.runkeeper.FitnessActivity+json", :records => "application/vnd.com.runkeeper.Records+json", :profile => "application/vnd.com.runkeeper.Profile+json", :new_activity => "application/vnd.com.runkeeper.NewFitnessActivity+json" }
Instance Attribute Summary collapse
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#user_response ⇒ Object
readonly
Returns the value of attribute user_response.
Class Method Summary collapse
Instance Method Summary collapse
- #fitness_activities ⇒ Object
-
#initialize(token) ⇒ Connection
constructor
A new instance of Connection.
- #past_activities ⇒ Object
- #profile ⇒ Object
- #records ⇒ Object
- #save_activity(hash) ⇒ Object
- #user ⇒ Object
Constructor Details
#initialize(token) ⇒ Connection
Returns a new instance of Connection.
20 21 22 23 24 |
# File 'lib/runkeeper/connection.rb', line 20 def initialize(token) @token = token @auth_header = {"Authorization" => "Bearer #{token}"} user end |
Instance Attribute Details
#token ⇒ Object (readonly)
Returns the value of attribute token.
18 19 20 |
# File 'lib/runkeeper/connection.rb', line 18 def token @token end |
#user_response ⇒ Object (readonly)
Returns the value of attribute user_response.
18 19 20 |
# File 'lib/runkeeper/connection.rb', line 18 def user_response @user_response end |
Class Method Details
.authorize ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/runkeeper/token_generator.rb', line 67 def self. server = Runkeeper::AuthorizationServer.new uri = URI.parse(Configuration.) uri.query = "client_id=#{Configuration.client_id}&response_type=code&redirect_uri=#{server.uri}" Launchy.open(uri.to_s) t1 = Thread.new { server.start } t1.join(0.5) until server.has_code? server.stop puts "Access Token: #{(server.code, server.uri)}" end |
.post_authorization(code, uri) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/runkeeper/connection.rb', line 50 def self.(code, uri) response = post(Configuration.access_token_url, :query => { :grant_type => 'authorization_code', :code => code, :client_id => Configuration.client_id, :client_secret => Configuration.client_secret, :redirect_uri => uri }) response['access_token'] end |
Instance Method Details
#fitness_activities ⇒ Object
34 35 36 |
# File 'lib/runkeeper/connection.rb', line 34 def fitness_activities self.class.get(user_response['fitness_activities'], :headers => headers(:fitness_activities)) end |
#past_activities ⇒ Object
38 39 40 |
# File 'lib/runkeeper/connection.rb', line 38 def past_activities self.class.get(user_response['fitness_activities'], :headers => headers(:past_activities)) end |
#profile ⇒ Object
30 31 32 |
# File 'lib/runkeeper/connection.rb', line 30 def profile self.class.get(user_response['profile'], :headers => headers(:profile)) end |
#records ⇒ Object
42 43 44 |
# File 'lib/runkeeper/connection.rb', line 42 def records self.class.get(user_response['records'], :headers => headers(:records)) end |
#save_activity(hash) ⇒ Object
46 47 48 |
# File 'lib/runkeeper/connection.rb', line 46 def save_activity(hash) self.class.post(user_response['fitness_activities'], :headers => headers(:new_activity), :body => hash.to_json) end |
#user ⇒ Object
26 27 28 |
# File 'lib/runkeeper/connection.rb', line 26 def user @user_response = self.class.get('/user', :headers => headers(:user)) end |