This repository has been archived by the owner on Jul 19, 2022. It is now read-only.
/
request.rb
62 lines (52 loc) · 1.86 KB
/
request.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module RunKeeper
class Request
HEADERS = {
'fitness_activities' => 'application/vnd.com.runkeeper.FitnessActivityFeed+json',
'profile' => 'application/vnd.com.runkeeper.Profile+json',
'user' => 'application/vnd.com.runkeeper.User+json'
}
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
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
def user
@user ||= begin
response = access_token.get('/user', :headers => {'Accept' => HEADERS['user']}, :parse => :json)
User.new parse_response(response).parsed
end
end
private
def access_token
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
else
raise Error.new(response)
end
end
end
end