Class: RunKeeper::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/run_keeper/request.rb

Constant Summary collapse

HEADERS =
{
  'fitness_activities' => 'application/vnd.com.runkeeper.FitnessActivityFeed+json',
  'profile'            => 'application/vnd.com.runkeeper.Profile+json',
  'user'               => 'application/vnd.com.runkeeper.User+json'
}

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, token) ⇒ Request

Returns a new instance of Request.



9
10
11
# File 'lib/run_keeper/request.rb', line 9

def initialize client_id, client_secret, token
  @client_id, @client_secret, @token = client_id, client_secret, token
end

Instance Method Details

#authorize_url(redirect_uri) ⇒ Object



13
14
15
# File 'lib/run_keeper/request.rb', line 13

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

#fitness_activities(options = {}) ⇒ Object



17
18
19
# File 'lib/run_keeper/request.rb', line 17

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

#get_token(code, redirect_uri) ⇒ Object



21
22
23
24
25
# File 'lib/run_keeper/request.rb', line 21

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

#profileObject



27
28
29
# File 'lib/run_keeper/request.rb', line 27

def profile
  Profile.new request('profile').parsed.merge(:userid => @user.userid)
end

#request(endpoint, params = {}) ⇒ Object



31
32
33
34
35
36
# File 'lib/run_keeper/request.rb', line 31

def request endpoint, params = {}
  response = access_token.get(user.send(endpoint), :headers => {'Accept' => HEADERS[endpoint]}, :parse => :json) do |request|
    request.params = params
  end
  parse_response response
end

#userObject



38
39
40
41
42
43
# File 'lib/run_keeper/request.rb', line 38

def user
  @user ||= begin
    response = access_token.get('/user', :headers => {'Accept' => HEADERS['user']}, :parse => :json)
    User.new parse_response(response).parsed
  end
end