-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-fitgem.rb
executable file
·59 lines (47 loc) · 1.81 KB
/
test-fitgem.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
#!/usr/bin/env ruby
require "fitgem"
require "pp"
require "yaml"
# Load the existing yml config
config = begin
Fitgem::Client.symbolize_keys(YAML.load(File.open(".fitgem.yml")))
rescue ArgumentError => e
puts "Could not parse YAML: #{e.message}"
exit
end
client = Fitgem::Client.new(config[:oauth])
# With the token and secret, we will try to use them
# to reconstitute a usable Fitgem::Client
if config[:oauth][:token] && config[:oauth][:secret]
begin
access_token = client.reconnect(config[:oauth][:token], config[:oauth][:secret])
rescue Exception => e
puts "Error: Could not reconnect Fitgem::Client due to invalid keys in .fitgem.yml"
exit
end
# Without the secret and token, initialize the Fitgem::Client
# and send the user to login and get a verifier token
else
request_token = client.request_token
token = request_token.token
secret = request_token.secret
puts "Go to http://www.fitbit.com/oauth/authorize?oauth_token=#{token} and then enter the verifier code below"
verifier = gets.chomp
begin
access_token = client.authorize(token, secret, { :oauth_verifier => verifier })
rescue Exception => e
puts "Error: Could not authorize Fitgem::Client with supplied oauth verifier"
exit
end
puts 'Verifier is: '+verifier
puts "Token is: "+access_token.token
puts "Secret is: "+access_token.secret
user_id = client.user_info['user']['encodedId']
puts "Current User is: "+user_id
config[:oauth].merge!(:token => access_token.token, :secret => access_token.secret, :user_id => user_id)
# Write the whole oauth token set back to the config file
File.open(".fitgem.yml", "w") {|f| f.write(config.to_yaml) }
end
# ============================================================
# Add Fitgem API calls on the client object below this line
pp client.activities_on_date 'today'