fitgem-app 2.2.0 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/bin/fitgem +69 -32
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 599642621d3c3e531fc7d34e39c83fe6dafbc25d
4
- data.tar.gz: b1b8399be2ba955ae87304bff59518474f710c8a
3
+ metadata.gz: 97d44a1d25f258f2211c4296ab1d54b32c11ced4
4
+ data.tar.gz: 1481da14324a20f5ea0eec3bd289c39bf4448de4
5
5
  SHA512:
6
- metadata.gz: af5ca38a5e4431524e13e5bd09d781f1ccd1a468ef8d812aa5032a92d797ea69cba680eab6595805085bfe7fc25f40f25506ffe11efb39069f91fea6e6cfd093
7
- data.tar.gz: bff15f4e640f56cafd99316a2f6ba14b2968c8e26bb3856b05101f4ffb2341b01a1850cbf0b52864ae902c9f6787d8579339c2f9e8e7ce5ef4f53bd082e57aa5
6
+ metadata.gz: 2ae5df19fcf24d0cdf84a567f3c532c01737a0e9a29363dc2674a88c66a30ac6e993da1fac31055709cbcd51eb2c1410215002a0cd7501d8f77fff00ae5a6892
7
+ data.tar.gz: a769a150e741a19f2db9c211c682346efbb788368ae08524f5e08c0a436ac327ad13ca5f5071ac08926ab90d4c87168bfa4c0c6ef91b7e0b02ad66bb7f1a5978
data/bin/fitgem CHANGED
@@ -2,6 +2,9 @@
2
2
  require 'httparty'
3
3
  require 'multi_json'
4
4
 
5
+ class APIError < RuntimeError
6
+ end
7
+
5
8
  class FitbitAccount
6
9
  include HTTParty
7
10
  @@base_uri = 'api.fitbit.com/1/user'
@@ -9,24 +12,33 @@ class FitbitAccount
9
12
  def initialize()
10
13
  @access_token = `echo -n $FB_ACCESS_TOKEN`
11
14
  @user_id = `echo -n $FB_USER_ID`
15
+ end
12
16
 
13
- if @access_token == "" && @user_id == ""
14
- puts "We need some information. Please navigate to http://juniorrubyist.github.io/fitgem/authorize.html in a browser. Then, authorize and copy/paste the information."
15
- print "Where is your shell configuration/variables file in your home directory?\n(e.g. .bashrc, .zshenv, etc.; usually .bashrc) "
16
- @default_shell = File.open(ENV['HOME']+"/#{gets.chomp}", "a+")
17
- @default_shell.puts("\n### Added by Fitgem")
18
-
19
- print "Copy your Access Token: "
20
- @access_token = gets.chomp
21
- @default_shell.puts("export FB_ACCESS_TOKEN=\"#{@access_token}\"")
22
-
23
- print "Copy your User ID: "
24
- @user_id = gets.chomp
25
- @default_shell.puts("export FB_USER_ID=\"#{@user_id}\"")
26
-
27
- puts "OK, your terminal will close in 30 seconds for changes to take effect, please re-open and run fitgem to open help."
28
- `sleep 30s`
29
- `exit`
17
+ def error_check
18
+ if @parsed_response["success"] == false
19
+ case @parsed_response.code
20
+ when 401
21
+ if @parsed_response["errors"][0]["errorType"] == "invalid_client"
22
+ puts "Please report this issue on the FitGem (juniorRubyist/fitgem) issue tracker on Github."
23
+ end
24
+ when 403
25
+ if @parsed_response["errors"][0]["errorType"] == "insufficient_scope"
26
+ puts "Please report this issue on the FitGem (juniorRubyist/fitgem) issue tracker on Github."
27
+ else
28
+ puts "You may need to modify your privacy settings. Then, try again."
29
+ end
30
+ when 404
31
+ puts "Please report this issue on the FitGem (juniorRubyist/fitgem) issue tracker on Github."
32
+ when 429
33
+ puts "Oops, you've exceeded your limit of 150 requests per hour. Try again on the :00."
34
+ exit
35
+ when 500...600
36
+ puts "Oh no, there might be a Fitbit service outage! Try again soon."
37
+ exit
38
+ else
39
+ puts "Please report this issue on the FitGem (juniorRubyist/fitgem) issue tracker on Github."
40
+ end
41
+ raise APIError, @parsed_response
30
42
  end
31
43
  end
32
44
 
@@ -34,6 +46,7 @@ class FitbitAccount
34
46
  @response = self.class.get("https://#{@@base_uri}/#{@user_id}/activities/steps/date/today/1d/1min.json",
35
47
  :headers => {"Authorization" => "Bearer #{@access_token}"})
36
48
  @parsed_response = MultiJson.load(@response.body)
49
+ self.error_check
37
50
  @parsed_response = @parsed_response["activities-steps"][0]["value"].to_i
38
51
  end
39
52
 
@@ -41,6 +54,7 @@ class FitbitAccount
41
54
  @response = self.class.get("https://#{@@base_uri}/#{@user_id}/activities/floors/date/today/1d/1min.json",
42
55
  :headers => {"Authorization" => "Bearer #{@access_token}"})
43
56
  @parsed_response = MultiJson.load(@response.body)
57
+ self.error_check
44
58
  @parsed_response = @parsed_response["activities-floors"][0]["value"].to_i
45
59
  end
46
60
 
@@ -48,6 +62,7 @@ class FitbitAccount
48
62
  @response = self.class.get("https://#{@@base_uri}/#{@user_id}/activities/calories/date/today/1d/1min.json",
49
63
  :headers => {"Authorization" => "Bearer #{@access_token}"})
50
64
  @parsed_response = MultiJson.load(@response.body)
65
+ self.error_check
51
66
  @parsed_response = @parsed_response["activities-calories"][0]["value"].to_i
52
67
  end
53
68
 
@@ -55,6 +70,7 @@ class FitbitAccount
55
70
  @response = self.class.get("https://#{@@base_uri}/#{@user_id}/activities/distance/date/today/1d/1min.json",
56
71
  :headers => {"Authorization" => "Bearer #{@access_token}"})
57
72
  @parsed_response = MultiJson.load(@response.body)
73
+ self.error_check
58
74
  @parsed_response = @parsed_response["activities-distance"][0]["value"]
59
75
  end
60
76
 
@@ -92,23 +108,44 @@ class FitbitAccount
92
108
  puts "AUTHOR"
93
109
  puts " Joseph Geis <geis28@gmail.com> (see files AUTHORS for details)."
94
110
  end
111
+
112
+ def setup
113
+ if @access_token == "" && @user_id == ""
114
+ @configuration = File.new(ENV['HOME']+"/.fitgem", "a+")
115
+ @configuration.puts("")
116
+
117
+ print "Copy your Access Token: "
118
+ @access_token = gets.chomp
119
+ @default_shell.puts("export FB_ACCESS_TOKEN=\"#{@access_token}\"")
120
+
121
+ print "Copy your User ID: "
122
+ @user_id = gets.chomp
123
+ @default_shell.puts("export FB_USER_ID=\"#{@user_id}\"")
124
+
125
+ puts "Alright sparky! Close your terminal or logout and login to get started."
126
+ else
127
+ puts "Hey, you've already finished setup!"
128
+ end
129
+ end
95
130
  end
96
131
 
97
132
  fitgem = FitbitAccount.new()
98
133
  choice = ARGV[0]
99
- case choice
100
- when 'steps', 's'
101
- puts "#{fitgem.steps} steps taken"
102
- when 'distance', 'd'
103
- puts "#{fitgem.distance} miles walked"
104
- when 'floors', 'f'
105
- puts "#{fitgem.floors} floors climbed"
106
- when 'calories', 'c'
107
- puts "#{fitgem.calories} calories burned"
108
- when 'full', 'all', 'a'
109
- fitgem.full_report
110
- when 'help', 'h', '?'
111
- fitgem.help
112
- else
113
- puts "Are you lost?, run Fitgem help"
134
+ unless `echo -n $TRAVIS_CI` == "true"
135
+ case choice
136
+ when 'steps', 's'
137
+ puts "#{fitgem.steps} steps taken"
138
+ when 'distance', 'd'
139
+ puts "#{fitgem.distance} miles walked"
140
+ when 'floors', 'f'
141
+ puts "#{fitgem.floors} floors climbed"
142
+ when 'calories', 'c'
143
+ puts "#{fitgem.calories} calories burned"
144
+ when 'full', 'all', 'a'
145
+ fitgem.full_report
146
+ when 'help', 'h', '?'
147
+ fitgem.help
148
+ else
149
+ puts "Are you lost?, run fitgem help"
150
+ end
114
151
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fitgem-app
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.0
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Geis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-04 00:00:00.000000000 Z
11
+ date: 2016-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json