fitgem_oauth2 1.3.0 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fd9d0f60ab3531d3ee135513c6e6ef7bf584978ee2aa760112ae317528bce9f4
4
- data.tar.gz: a65d59a4e239ae99f068d1886166fbc81c04d544bcf1cc03acda2703d1e7497c
3
+ metadata.gz: a0f9bc467cea52fdb75b24bcb633887178a9db1066e0c181ce46ac48779d35f7
4
+ data.tar.gz: ab134a17a8b3c5ed82400e938dbfe291e12881800e18172d79468b057fa03514
5
5
  SHA512:
6
- metadata.gz: 0a747382a12149b6aa73fab4df12672fde5be3a35ce1c33ab8bd9ee969146c2ca6368b5667f7c94cfbd0ae7c09a30a0d051870c766149afaf37f7658fd0e33b7
7
- data.tar.gz: 3400e8170f577dd2414bce4cea863ee02a97d68c49e2dff03ba1af3c8f1616634d4c35acf5e4111dce08692c87d96bd4427cc6d8408ec9bf45e98bd471f245dd
6
+ metadata.gz: a68a72381737351e096ecd8d915d6169c6d64533de31cb60fbea81d18abcca523704ec2ca080ba757e3ed056b5c3db454f246cbede1f456675809d809edfe28e
7
+ data.tar.gz: 1b69b35ea268ea111d03fb8b9e81605e4229fd48267a7c0172b9d7dd520bc9436e59c8b088a332bb66f1c7dc88e0f957ed7b0634cbd3ab293bf9fd60a4cae7ca
@@ -1,22 +1,22 @@
1
- # encoding: utf-8
2
-
3
- $:.unshift File.expand_path('../lib', __FILE__)
4
- require 'fitgem_oauth2/version'
5
-
6
- Gem::Specification.new do |s|
7
- s.name = 'fitgem_oauth2'
8
- s.version = FitgemOauth2::VERSION
9
- s.summary = 'Fitbit API client library'
10
- s.description = 'This gem allows requesting data from Fitbit API using OAuth2'
11
- s.authors = ['Ankit Gupta']
12
- s.email = 'ankit.gupta2801@gmail.com'
13
- s.files = %w(fitgem_oauth2.gemspec) + `git ls-files -z`.split("\x0").select { |f| f.start_with?('lib/') }
14
- s.homepage = 'http://rubygems.org/gems/fitgem_oauth2'
15
- s.license = 'MIT'
16
-
17
- s.add_runtime_dependency 'faraday', '~> 0.9'
18
-
19
- s.add_development_dependency 'rake', '~> 10.5'
20
- s.add_development_dependency 'rspec', '~> 3.4'
21
- s.add_development_dependency 'factory_girl', '~> 4.5'
22
- end
1
+ # encoding: utf-8
2
+
3
+ $:.unshift File.expand_path('../lib', __FILE__)
4
+ require 'fitgem_oauth2/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'fitgem_oauth2'
8
+ s.version = FitgemOauth2::VERSION
9
+ s.summary = 'Fitbit API client library'
10
+ s.description = 'This gem allows requesting data from Fitbit API using OAuth2'
11
+ s.authors = ['Ankit Gupta']
12
+ s.email = 'ankit.gupta2801@gmail.com'
13
+ s.files = %w(fitgem_oauth2.gemspec) + `git ls-files -z`.split("\x0").select { |f| f.start_with?('lib/') }
14
+ s.homepage = 'http://rubygems.org/gems/fitgem_oauth2'
15
+ s.license = 'MIT'
16
+
17
+ s.add_runtime_dependency 'faraday', '~> 0.9'
18
+
19
+ s.add_development_dependency 'rake', '~> 10.5'
20
+ s.add_development_dependency 'rspec', '~> 3.4'
21
+ s.add_development_dependency 'factory_girl', '~> 4.5'
22
+ end
data/lib/fitgem_oauth2.rb CHANGED
@@ -1,6 +1,6 @@
1
- require 'fitgem_oauth2/client.rb'
2
-
3
- # Top level module for the classes for this gem
4
- module FitgemOauth2
5
-
6
- end
1
+ require 'fitgem_oauth2/client.rb'
2
+
3
+ # Top level module for the classes for this gem
4
+ module FitgemOauth2
5
+
6
+ end
@@ -1,209 +1,209 @@
1
- module FitgemOauth2
2
- class Client
3
-
4
- ACTIVITY_RESOURCES = %w[
5
- calories caloriesBMR steps distance floors elevation minutesSedentary
6
- minutesLightlyActive minutesFairlyActive minutesVeryActive
7
- activityCaloriestracker/calories tracker/steps tracker/distance
8
- tracker/floors tracker/elevation tracker/minutesSedentary
9
- tracker/minutesLightlyActive tracker/minutesFairlyActive
10
- tracker/minutesVeryActive tracker/activityCalories
11
- ].freeze
12
-
13
- ACTIVITY_PERIODS = %w[1d 7d 30d 1w 1m 3m 6m 1y max].freeze
14
-
15
- # retrieves daily activity summary for a date
16
- # @param date the date for which the summary is retrieved
17
- def daily_activity_summary(date)
18
- get_call("user/#{user_id}/activities/date/#{format_date(date)}.json")
19
- end
20
-
21
- # ==================================
22
- # Activity Time Series
23
- # ==================================
24
-
25
- # retrieves activity time series, based on the arguments provided
26
- # @param resource the resource for which the series needs to be retrieved. one of ALLOWED_RESOURCES
27
- # @param start_date the start date for the series
28
- # @param end_date the end date for the series. If specifying end_date, do not specify period
29
- # @param period the period starting from start_date for which the series needs to be retrieved. If specifying period,
30
- # do not use end_date
31
- def activity_time_series(resource: nil, start_date: nil, end_date: nil, period: nil)
32
-
33
- unless resource && ACTIVITY_RESOURCES.include?(resource)
34
- raise FitgemOauth2::InvalidArgumentError, "Invalid resource: #{resource}. Valid resources are #{ACTIVITY_RESOURCES}."
35
- end
36
-
37
- unless start_date
38
- raise FitgemOauth2::InvalidArgumentError, 'Start date must be specified.'
39
- end
40
-
41
- if period && end_date
42
- raise FitgemOauth2::InvalidArgumentError, 'Both period and end_date are specified. Please specify only one.'
43
- end
44
-
45
- if period && !ACTIVITY_PERIODS.include?(period)
46
- raise FitgemOauth2::InvalidArgumentError, "Invalid period: #{period}. Valid periods are #{ACTIVITY_PERIODS}."
47
- end
48
-
49
- first = format_date(start_date)
50
- second = period || format_date(end_date)
51
- url = ['user', user_id, 'activities', resource, 'date', first, second].join('/')
52
- get_call(url + '.json')
53
- end
54
-
55
- # retrieves intraday activity time series.
56
- # @param resource (required) for which the intrady series is retrieved. one of 'calories', 'steps', 'distance', 'floors', 'elevation'
57
- # @param start_date (required) start date for the series
58
- # @param end_date (optional) end date for the series, if not specified, the series is for 1 day
59
- # @param detail_level (required) level of detail for the series
60
- # @param start_time (optional)start time for the series
61
- # @param end_time the (optional)end time for the series. specify both start_time and end_time, if using either
62
- def intraday_activity_time_series(resource: nil, start_date: nil, end_date: nil, detail_level: nil,
63
- start_time: nil, end_time: nil)
64
-
65
- # converting to symbol to allow developer to use either 'calories' or :calories
66
- resource = resource.to_sym
67
-
68
- unless %i[calories steps distance floors elevation].include?(resource)
69
- raise FitgemOauth2::InvalidArgumentError,
70
- 'Must specify resource to fetch intraday time series data for.'\
71
- ' One of (:calories, :steps, :distance, :floors, or :elevation) is required.'
72
- end
73
-
74
- unless start_date
75
- raise FitgemOauth2::InvalidArgumentError,
76
- 'Must specify the start_date to fetch intraday time series data'
77
- end
78
-
79
- end_date ||= '1d'
80
-
81
- unless detail_level && %w(1min 15min).include?(detail_level)
82
- raise FitgemOauth2::InvalidArgumentError,
83
- 'Must specify the data resolution to fetch intraday time series data for.'\
84
- ' One of (\"1d\" or \"15min\") is required.'
85
- end
86
-
87
- resource_path = [
88
- 'user', @user_id,
89
- 'activities', resource,
90
- 'date', format_date(start_date),
91
- end_date, detail_level
92
- ].join('/')
93
-
94
- if start_time || end_time
95
- resource_path =
96
- [resource_path, 'time', format_time(start_time), format_time(end_time)].join('/')
97
- end
98
- get_call("#{resource_path}.json")
99
- end
100
-
101
- # ======================================
102
- # Activity Logging Methods
103
- # ======================================
104
-
105
- # logs activity using the params.
106
- # @param params Hash to be posted. Refer https://dev.fitbit.com/docs/activity/#activity-logging for accepted
107
- # POST parameters
108
- def log_activity(params)
109
- post_call("user/#{user_id}/activities.json", params)
110
- end
111
-
112
- # deletes a logged activity
113
- # @param id id of the activity log to be deleted
114
- def delete_logged_activity(id)
115
- delete_call("user/#{user_id}/activities/#{id}.json")
116
- end
117
-
118
- # retrieves activity list for the user
119
- def activity_list(date, sort, limit)
120
- date_param = format_date(date)
121
- if sort == "asc"
122
- date_param = "afterDate=#{date_param}"
123
- elsif sort == "desc"
124
- date_param = "beforeDate=#{date_param}"
125
- else
126
- raise FitgemOauth2::InvalidArgumentError, "sort can either be asc or desc"
127
- end
128
- get_call("user/#{user_id}/activities/list.json?offset=0&limit=#{limit}&sort=#{sort}&#{date_param}")
129
- end
130
-
131
- # retrieves activity list in the tcx format
132
- def activity_tcx(id)
133
- get_call("user/#{user_id}/activities/#{id}.tcx")
134
- end
135
-
136
-
137
- # ======================================
138
- # Activity Types
139
- # ======================================
140
-
141
- # Get a tree of all valid Fitbit public activities from the activities catalog as well
142
- # as private custom activities the user created in the format requested. If the activity
143
- # has levels, also get a list of activity level details
144
- def activities
145
- get_call('activities.json')
146
- end
147
-
148
- # Returns the details of a specific activity in the Fitbit activities database in the format requested.
149
- # @param id id of the activity for which the details need to be retrieved
150
- def activity(id)
151
- get_call("activities/#{id}.json")
152
- end
153
-
154
- # gets frequent activities
155
- def frequent_activities
156
- get_call("user/#{user_id}/activities/frequent.json")
157
- end
158
-
159
- # gets recent activities
160
- def recent_activities
161
- get_call("user/#{user_id}/activities/recent.json")
162
- end
163
-
164
- # gets favorite activities
165
- def favorite_activities
166
- get_call("user/#{user_id}/activities/favorite.json")
167
- end
168
-
169
- # adds the activity with the given ID to user's list of favorite activities.
170
- # @param activity_id ID of the activity to be added to the list of favorite activities
171
- def add_favorite_activity(activity_id)
172
- post_call("user/#{user_id}/activities/log/favorite/#{activity_id}.json")
173
- end
174
-
175
- # removes the activity with given ID from list of favorite activities.
176
- # @param activity_id ID of the activity to be removed from favorite activity
177
- def remove_favorite_activity(activity_id)
178
- delete_call("user/#{user_id}/activities/log/favorite/#{activity_id}.json")
179
- end
180
-
181
- # ======================================
182
- # Activity Goals
183
- # ======================================
184
-
185
- # retrieve activity goals for a period
186
- # @period the period for which the goals need to be retrieved. either 'weekly' or 'daily'
187
- def goals(period)
188
- unless period && %w(daily weekly).include?(period)
189
- raise FitgemOauth2::InvalidArgumentError, "Goal period should either be 'daily' or 'weekly'"
190
- end
191
- get_call("user/#{user_id}/activities/goals/#{period}.json")
192
- end
193
-
194
- # update activity goals
195
- # @param period period for the goal ('weekly' or 'daily')
196
- # @param params the POST params for the request. Refer to Fitbit documentation for accepted format
197
- def update_activity_goals(period, params)
198
- unless period && %w(daily weekly).include?(period)
199
- raise FitgemOauth2::InvalidArgumentError, "Goal period should either be 'daily' or 'weekly'"
200
- end
201
- post_call("user/#{user_id}/activities/goals/#{period}.json", params)
202
- end
203
-
204
- # retrieves lifetime statistics for the user
205
- def lifetime_stats
206
- get_call("user/#{user_id}/activities.json")
207
- end
208
- end
209
- end
1
+ module FitgemOauth2
2
+ class Client
3
+
4
+ ACTIVITY_RESOURCES = %w[
5
+ calories caloriesBMR steps distance floors elevation minutesSedentary
6
+ minutesLightlyActive minutesFairlyActive minutesVeryActive
7
+ activityCaloriestracker/calories tracker/steps tracker/distance
8
+ tracker/floors tracker/elevation tracker/minutesSedentary
9
+ tracker/minutesLightlyActive tracker/minutesFairlyActive
10
+ tracker/minutesVeryActive tracker/activityCalories
11
+ ].freeze
12
+
13
+ ACTIVITY_PERIODS = %w[1d 7d 30d 1w 1m 3m 6m 1y max].freeze
14
+
15
+ # retrieves daily activity summary for a date
16
+ # @param date the date for which the summary is retrieved
17
+ def daily_activity_summary(date)
18
+ get_call("user/#{user_id}/activities/date/#{format_date(date)}.json")
19
+ end
20
+
21
+ # ==================================
22
+ # Activity Time Series
23
+ # ==================================
24
+
25
+ # retrieves activity time series, based on the arguments provided
26
+ # @param resource the resource for which the series needs to be retrieved. one of ALLOWED_RESOURCES
27
+ # @param start_date the start date for the series
28
+ # @param end_date the end date for the series. If specifying end_date, do not specify period
29
+ # @param period the period starting from start_date for which the series needs to be retrieved. If specifying period,
30
+ # do not use end_date
31
+ def activity_time_series(resource: nil, start_date: nil, end_date: nil, period: nil)
32
+
33
+ unless resource && ACTIVITY_RESOURCES.include?(resource)
34
+ raise FitgemOauth2::InvalidArgumentError, "Invalid resource: #{resource}. Valid resources are #{ACTIVITY_RESOURCES}."
35
+ end
36
+
37
+ unless start_date
38
+ raise FitgemOauth2::InvalidArgumentError, 'Start date must be specified.'
39
+ end
40
+
41
+ if period && end_date
42
+ raise FitgemOauth2::InvalidArgumentError, 'Both period and end_date are specified. Please specify only one.'
43
+ end
44
+
45
+ if period && !ACTIVITY_PERIODS.include?(period)
46
+ raise FitgemOauth2::InvalidArgumentError, "Invalid period: #{period}. Valid periods are #{ACTIVITY_PERIODS}."
47
+ end
48
+
49
+ first = format_date(start_date)
50
+ second = period || format_date(end_date)
51
+ url = ['user', user_id, 'activities', resource, 'date', first, second].join('/')
52
+ get_call(url + '.json')
53
+ end
54
+
55
+ # retrieves intraday activity time series.
56
+ # @param resource (required) for which the intrady series is retrieved. one of 'calories', 'steps', 'distance', 'floors', 'elevation'
57
+ # @param start_date (required) start date for the series
58
+ # @param end_date (optional) end date for the series, if not specified, the series is for 1 day
59
+ # @param detail_level (required) level of detail for the series
60
+ # @param start_time (optional)start time for the series
61
+ # @param end_time the (optional)end time for the series. specify both start_time and end_time, if using either
62
+ def intraday_activity_time_series(resource: nil, start_date: nil, end_date: nil, detail_level: nil,
63
+ start_time: nil, end_time: nil)
64
+
65
+ # converting to symbol to allow developer to use either 'calories' or :calories
66
+ resource = resource.to_sym
67
+
68
+ unless %i[calories steps distance floors elevation].include?(resource)
69
+ raise FitgemOauth2::InvalidArgumentError,
70
+ 'Must specify resource to fetch intraday time series data for.'\
71
+ ' One of (:calories, :steps, :distance, :floors, or :elevation) is required.'
72
+ end
73
+
74
+ unless start_date
75
+ raise FitgemOauth2::InvalidArgumentError,
76
+ 'Must specify the start_date to fetch intraday time series data'
77
+ end
78
+
79
+ end_date ||= '1d'
80
+
81
+ unless detail_level && %w(1min 15min).include?(detail_level)
82
+ raise FitgemOauth2::InvalidArgumentError,
83
+ 'Must specify the data resolution to fetch intraday time series data for.'\
84
+ ' One of (\"1d\" or \"15min\") is required.'
85
+ end
86
+
87
+ resource_path = [
88
+ 'user', @user_id,
89
+ 'activities', resource,
90
+ 'date', format_date(start_date),
91
+ end_date, detail_level
92
+ ].join('/')
93
+
94
+ if start_time || end_time
95
+ resource_path =
96
+ [resource_path, 'time', format_time(start_time), format_time(end_time)].join('/')
97
+ end
98
+ get_call("#{resource_path}.json")
99
+ end
100
+
101
+ # ======================================
102
+ # Activity Logging Methods
103
+ # ======================================
104
+
105
+ # logs activity using the params.
106
+ # @param params Hash to be posted. Refer https://dev.fitbit.com/docs/activity/#activity-logging for accepted
107
+ # POST parameters
108
+ def log_activity(params)
109
+ post_call("user/#{user_id}/activities.json", params)
110
+ end
111
+
112
+ # deletes a logged activity
113
+ # @param id id of the activity log to be deleted
114
+ def delete_logged_activity(id)
115
+ delete_call("user/#{user_id}/activities/#{id}.json")
116
+ end
117
+
118
+ # retrieves activity list for the user
119
+ def activity_list(date, sort, limit)
120
+ date_param = format_date(date)
121
+ if sort == "asc"
122
+ date_param = "afterDate=#{date_param}"
123
+ elsif sort == "desc"
124
+ date_param = "beforeDate=#{date_param}"
125
+ else
126
+ raise FitgemOauth2::InvalidArgumentError, "sort can either be asc or desc"
127
+ end
128
+ get_call("user/#{user_id}/activities/list.json?offset=0&limit=#{limit}&sort=#{sort}&#{date_param}")
129
+ end
130
+
131
+ # retrieves activity list in the tcx format
132
+ def activity_tcx(id)
133
+ get_call("user/#{user_id}/activities/#{id}.tcx")
134
+ end
135
+
136
+
137
+ # ======================================
138
+ # Activity Types
139
+ # ======================================
140
+
141
+ # Get a tree of all valid Fitbit public activities from the activities catalog as well
142
+ # as private custom activities the user created in the format requested. If the activity
143
+ # has levels, also get a list of activity level details
144
+ def activities
145
+ get_call('activities.json')
146
+ end
147
+
148
+ # Returns the details of a specific activity in the Fitbit activities database in the format requested.
149
+ # @param id id of the activity for which the details need to be retrieved
150
+ def activity(id)
151
+ get_call("activities/#{id}.json")
152
+ end
153
+
154
+ # gets frequent activities
155
+ def frequent_activities
156
+ get_call("user/#{user_id}/activities/frequent.json")
157
+ end
158
+
159
+ # gets recent activities
160
+ def recent_activities
161
+ get_call("user/#{user_id}/activities/recent.json")
162
+ end
163
+
164
+ # gets favorite activities
165
+ def favorite_activities
166
+ get_call("user/#{user_id}/activities/favorite.json")
167
+ end
168
+
169
+ # adds the activity with the given ID to user's list of favorite activities.
170
+ # @param activity_id ID of the activity to be added to the list of favorite activities
171
+ def add_favorite_activity(activity_id)
172
+ post_call("user/#{user_id}/activities/log/favorite/#{activity_id}.json")
173
+ end
174
+
175
+ # removes the activity with given ID from list of favorite activities.
176
+ # @param activity_id ID of the activity to be removed from favorite activity
177
+ def remove_favorite_activity(activity_id)
178
+ delete_call("user/#{user_id}/activities/log/favorite/#{activity_id}.json")
179
+ end
180
+
181
+ # ======================================
182
+ # Activity Goals
183
+ # ======================================
184
+
185
+ # retrieve activity goals for a period
186
+ # @period the period for which the goals need to be retrieved. either 'weekly' or 'daily'
187
+ def goals(period)
188
+ unless period && %w(daily weekly).include?(period)
189
+ raise FitgemOauth2::InvalidArgumentError, "Goal period should either be 'daily' or 'weekly'"
190
+ end
191
+ get_call("user/#{user_id}/activities/goals/#{period}.json")
192
+ end
193
+
194
+ # update activity goals
195
+ # @param period period for the goal ('weekly' or 'daily')
196
+ # @param params the POST params for the request. Refer to Fitbit documentation for accepted format
197
+ def update_activity_goals(period, params)
198
+ unless period && %w(daily weekly).include?(period)
199
+ raise FitgemOauth2::InvalidArgumentError, "Goal period should either be 'daily' or 'weekly'"
200
+ end
201
+ post_call("user/#{user_id}/activities/goals/#{period}.json", params)
202
+ end
203
+
204
+ # retrieves lifetime statistics for the user
205
+ def lifetime_stats
206
+ get_call("user/#{user_id}/activities.json")
207
+ end
208
+ end
209
+ end