Class: Fitgem::Client
- Inherits:
-
Object
- Object
- Fitgem::Client
- Defined in:
- lib/fitgem/foods.rb,
lib/fitgem/sleep.rb,
lib/fitgem/users.rb,
lib/fitgem/water.rb,
lib/fitgem/alarms.rb,
lib/fitgem/badges.rb,
lib/fitgem/client.rb,
lib/fitgem/devices.rb,
lib/fitgem/friends.rb,
lib/fitgem/glucose.rb,
lib/fitgem/helpers.rb,
lib/fitgem/activities.rb,
lib/fitgem/heart_rate.rb,
lib/fitgem/time_range.rb,
lib/fitgem/notifications.rb,
lib/fitgem/blood_pressure.rb,
lib/fitgem/body_measurements.rb
Constant Summary collapse
- API_VERSION =
'1'
- EMPTY_BODY =
''
- SUBSCRIBABLE_TYPES =
[:sleep, :body, :activities, :foods, :all]
Instance Attribute Summary collapse
-
#api_unit_system ⇒ String
Sets or gets the api unit system to be used in API calls.
-
#api_version ⇒ String
Sets or gets the api_version to be used in API calls “.
-
#user_id ⇒ String
Sets or gets the user id to be used in API calls.
Class Method Summary collapse
-
.symbolize_keys(obj) ⇒ Object
Recursively turns arrays and hashes into symbol-key based structures.
Instance Method Summary collapse
-
#accept_invite(requestor_id) ⇒ Hash
Accept a friend invite.
-
#activities ⇒ Hash
Get a list of all activities.
-
#activities_on_date(date) ⇒ Hash
Get all of the logged activities on the supplied date.
-
#activity(id) ⇒ Hash
Get the detailed information for a single activity.
-
#activity_on_date_range(activity, start, finish) ⇒ Hash
Get activity information for a date range.
-
#activity_statistics ⇒ Hash
Get the activity statistics for the current user.
-
#add_alarm(opts) ⇒ Hash
Add a silent alarm to your profile.
-
#add_favorite_activity(activity_id) ⇒ Hash
Mark an activity as a favorite.
-
#add_favorite_food(food_id) ⇒ Hash
Mark a food as a favorite.
-
#authentication_request_token(opts = {}) ⇒ OAuth::RequestToken
Get an authentication request token.
-
#authorize(token, secret, opts = {}) ⇒ OAuth::AccessToken
Finalize authentication and retrieve an oauth access token.
-
#badges ⇒ Hash
Get a list of all badges earned.
-
#blood_pressure_on_date(date) ⇒ Hash
Get blood pressure log entries for the supplied date.
-
#body_fat(opts = {}) ⇒ Hash
Get a list of logged body fat entries for the specified period.
-
#body_fat_goal ⇒ Hash
Retrieve the body fat goal of the current user.
-
#body_measurements_on_date(date) ⇒ Hash
Get the body measurements logged on a specified date.
-
#body_weight(opts = {}) ⇒ Hash
Get a list of logged body weight entries for the specified period.
-
#body_weight_goal ⇒ Hash
Retrieve the body weight goal of the current user.
-
#connected? ⇒ Boolean
Get the current state of the client.
-
#construct_date_range_fragment(options) ⇒ Object
protected.
-
#create_food(opts) ⇒ Hash
Create a new food and add it to fitbit.
-
#create_or_update_body_fat_goal(fatPercentageGoal) ⇒ Object
Create or update a user’s body fat percentage goal.
-
#create_or_update_body_weight_goal(startDate, startWeight, goalWeight) ⇒ Object
Create or update a user’s weight goal.
-
#create_or_update_daily_goal(opts) ⇒ Object
Create or update a user’s daily goal.
-
#create_or_update_weekly_goal(opts) ⇒ Object
Create or update a user’s weekly goal.
-
#create_subscription(opts) ⇒ Integer, Hash
Creates a notification subscription.
-
#daily_goals ⇒ Hash
Get the daily activity goals for the current user.
-
#data_by_time_range(resource_path, opts) ⇒ Hash
Gets historical resource data in the time range specified by options hash param.
-
#decline_invite(requestor_id) ⇒ Hash
Decline a friend invite.
-
#delete_alarm(alarm_id, device_id) ⇒ Hash
Remove a silent alarm from your profile.
-
#delete_blood_pressure_log(blood_pressure_log_id) ⇒ Hash
Delete logged blood pressure information.
-
#delete_body_fat_log(logId) ⇒ Object
Delete user’s body fat log entry with the given id.
-
#delete_body_weight_log(logId) ⇒ Object
Delete user’s body weight log entry with the given id.
-
#delete_heart_rate_log(heart_rate_log_id) ⇒ Hash
Delete logged heart rate information.
-
#delete_logged_activity(activity_log_id) ⇒ Hash
Delete a logged activity.
-
#delete_logged_food(food_log_id) ⇒ Hash
Remove a logged food entry.
-
#delete_sleep_log(sleep_log_id) ⇒ Hash
Delete logged sleep data.
-
#delete_water_log(water_log_id) ⇒ Hash
Delete logged water consumption.
-
#device_info(device_id) ⇒ Hash
Get the details about a specific device.
-
#devices ⇒ Array
Get a list of devices for the current account.
-
#favorite_activities ⇒ Array
Get a list of activities marked as favorite by the user.
-
#favorite_foods ⇒ Array
Get a list of the favorite logged foods.
-
#find_food(query) ⇒ Array
Search the foods database.
-
#food_info(food_id) ⇒ Hash
Get detailed information for a food.
-
#foods_on_date(date) ⇒ Array
Get all foods logged on the supplied date.
-
#foods_units ⇒ Array
Get a list of all of the available food units.
-
#format_date(date) ⇒ String
Format any of a variety of date types into the formatted string required when using the fitbit API.
-
#format_time(time, opts = {}) ⇒ String
Format any of a variety of time-related types into the formatted string required when using the fitbit API.
-
#frequent_activities ⇒ Array
Get a list of the frequently logged activities.
-
#frequent_foods ⇒ Array
Get a list of the frequently logged foods.
-
#friends ⇒ Array
Get a list of the current user’s friends.
-
#get_alarms(device_id) ⇒ Hash
Retrieve the silent alarms of your device.
-
#glucose_on_date(date) ⇒ Hash
Get glucose log entries for the supplied date.
-
#goals ⇒ Hash
deprecated
Deprecated.
Use the daily_goals method; in v1.0.0 this method will be removed
-
#heart_rate_on_date(date) ⇒ Hash
Get heart rate log entries for the supplied date.
-
#initialize(opts) ⇒ Client
constructor
Creates a client object to communicate with the fitbit API.
-
#intraday_time_series(opts) ⇒ Hash
Get intraday time series data for the specified resource.
-
#invite_friend(opts) ⇒ Hash
Create an invite for a user to connect with the current user as a friend.
-
#label_for_measurement(measurement_type, respect_user_unit_preferences = true) ⇒ String
Fetch the correct label for the desired measurement unit.
-
#leaderboard ⇒ Hash
Get the leaderboard of friends’ weekly activities.
-
#log_activity(opts) ⇒ Hash
Log an activity to fitbit.
-
#log_blood_pressure(opts) ⇒ Hash
Log blood pressure information to fitbit.
-
#log_body_fat(fatPercentage, date, opts = {}) ⇒ Object
Log body fat percentage.
-
#log_body_measurements(opts) ⇒ Hash
Log body measurements to fitbit for the current user.
-
#log_food(opts) ⇒ Hash
Log food to fitbit for the current user.
-
#log_glucose(opts) ⇒ Hash
Log glucose information to fitbit.
-
#log_heart_rate(opts) ⇒ Hash
Log heart rate information to fitbit.
-
#log_sleep(opts) ⇒ Hash
Log sleep data to fitbit for current user.
-
#log_water(opts) ⇒ Hash
Log water consumption to fitbit.
-
#log_weight(weight, date, opts = {}) ⇒ Hash
Log weight to fitbit for the current user.
-
#make_headers(opts) ⇒ Hash
protected
Create the headers hash for subscription management calls.
-
#make_subscription_url(opts) ⇒ String
protected
Create the subscription management API url.
-
#meals ⇒ Hash
Get a hash containing the user’s meal informarion.
-
#monthly_leaderboard ⇒ Hash
deprecated
Deprecated.
Monthly leaderboards are no longer available from Fitbit. Please update to use #leaderboard
-
#recent_activities ⇒ Array
Get a list of recently logged activities.
-
#recent_foods ⇒ Array
Get a list of the recently logged foods.
-
#reconnect(token, secret) ⇒ OAuth::AccessToken
Reconnect to the fitbit API with a stored oauth token and oauth secret.
-
#remove_favorite_activity(activity_id) ⇒ Hash
Unmark an activity as a favorite.
-
#remove_favorite_food(food_id) ⇒ Hash
Unmark a food as a favorite.
-
#remove_subscription(opts) ⇒ Integer, Hash
Removes a notification subscription.
-
#request_token(opts = {}) ⇒ OAuth::RequestToken
Get an oauth request token.
-
#sleep_on_date(date) ⇒ Array
Get sleep data for specified date.
-
#subscriptions(opts) ⇒ Hash
Get a list of all subscriptions.
-
#unit_mappings ⇒ Object
protected
Defined mappings for unit measurements to labels.
-
#update_alarm(alarm_id, device_id, opts) ⇒ Hash
Update an existing alarm.
-
#update_user_info(opts) ⇒ Hash
Update profile information for current user.
-
#user_info(opts = {}) ⇒ Hash
Get information aobut current user.
-
#validate_subscription_type(subscription_type) ⇒ Boolean
protected
Ensures that the type supplied is valid.
-
#water_on_date(date) ⇒ Hash
Get water log entries for the supplied date.
-
#weekly_goals ⇒ Hash
Get the weekly activity goals for the current user.
-
#weekly_leaderboard ⇒ Hash
deprecated
Deprecated.
Please update to use #leaderboard
Constructor Details
#initialize(opts) ⇒ Client
Creates a client object to communicate with the fitbit API
There are two primary ways to create a client: one if the current fitbit user has not authenticated through fitbit.com, and another if they have already authenticated and you have a stored token/secret returned by fitbit after the user authenticated and authorized your application.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/fitgem/client.rb', line 93 def initialize(opts) missing = [:consumer_key, :consumer_secret] - opts.keys if missing.size > 0 raise Fitgem::InvalidArgumentError, "Missing required options: #{missing.join(',')}" end @consumer_key = opts[:consumer_key] @consumer_secret = opts[:consumer_secret] @token = opts[:token] @secret = opts[:secret] @proxy = opts[:proxy] if opts[:proxy] @user_id = opts[:user_id] || '-' @api_unit_system = opts[:unit_system] || Fitgem::ApiUnitSystem.US @api_version = API_VERSION end |
Instance Attribute Details
#api_unit_system ⇒ String
Sets or gets the api unit system to be used in API calls
47 48 49 |
# File 'lib/fitgem/client.rb', line 47 def api_unit_system @api_unit_system end |
#api_version ⇒ String
Sets or gets the api_version to be used in API calls “
31 32 33 |
# File 'lib/fitgem/client.rb', line 31 def api_version @api_version end |
#user_id ⇒ String
Sets or gets the user id to be used in API calls
52 53 54 |
# File 'lib/fitgem/client.rb', line 52 def user_id @user_id end |
Class Method Details
.symbolize_keys(obj) ⇒ Object
Recursively turns arrays and hashes into symbol-key based structures.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/fitgem/helpers.rb', line 142 def self.symbolize_keys(obj) case obj when Array obj.inject([]){|res, val| res << case val when Hash, Array symbolize_keys(val) else val end res } when Hash obj.inject({}){|res, (key, val)| nkey = case key when String key.to_sym else key end nval = case val when Hash, Array symbolize_keys(val) else val end res[nkey] = nval res } else obj end end |
Instance Method Details
#accept_invite(requestor_id) ⇒ Hash
Accept a friend invite
69 70 71 |
# File 'lib/fitgem/friends.rb', line 69 def accept_invite(requestor_id) respond_to_invite(requestor_id, true) end |
#activities ⇒ Hash
Get a list of all activities
The returned list includes all public activities plus any activities that the current user created and logged
13 14 15 |
# File 'lib/fitgem/activities.rb', line 13 def activities get("/activities.json") end |
#activities_on_date(date) ⇒ Hash
Get all of the logged activities on the supplied date
21 22 23 |
# File 'lib/fitgem/activities.rb', line 21 def activities_on_date(date) get("/user/#{@user_id}/activities/date/#{format_date(date)}.json") end |
#activity(id) ⇒ Hash
Get the detailed information for a single activity
64 65 66 |
# File 'lib/fitgem/activities.rb', line 64 def activity(id) get("/activities/#{id}.json") end |
#activity_on_date_range(activity, start, finish) ⇒ Hash
Get activity information for a date range
31 32 33 |
# File 'lib/fitgem/activities.rb', line 31 def activity_on_date_range(activity, start, finish) get("/user/#{@user_id}/activities/#{activity}/date/#{format_date(start)}/#{format_date(finish)}.json") end |
#activity_statistics ⇒ Hash
Get the activity statistics for the current user
72 73 74 |
# File 'lib/fitgem/activities.rb', line 72 def activity_statistics get("/user/#{@user_id}/activities.json") end |
#add_alarm(opts) ⇒ Hash
Add a silent alarm to your profile
19 20 21 22 |
# File 'lib/fitgem/alarms.rb', line 19 def add_alarm(opts) device_id = opts[:device_id] post("/user/#{@user_id}/devices/tracker/#{device_id}/alarms.json", opts) end |
#add_favorite_activity(activity_id) ⇒ Hash
Mark an activity as a favorite
152 153 154 |
# File 'lib/fitgem/activities.rb', line 152 def add_favorite_activity(activity_id) post("/user/#{@user_id}/activities/log/favorite/#{activity_id}.json") end |
#add_favorite_food(food_id) ⇒ Hash
Mark a food as a favorite
113 114 115 |
# File 'lib/fitgem/foods.rb', line 113 def add_favorite_food(food_id) post("/user/#{@user_id}/foods/log/favorite/#{food_id}.json") end |
#authentication_request_token(opts = {}) ⇒ OAuth::RequestToken
Get an authentication request token
174 175 176 177 |
# File 'lib/fitgem/client.rb', line 174 def authentication_request_token(opts={}) consumer.[:authorize_path] = '/oauth/authenticate' request_token(opts) end |
#authorize(token, secret, opts = {}) ⇒ OAuth::AccessToken
Finalize authentication and retrieve an oauth access token
125 126 127 128 129 130 131 |
# File 'lib/fitgem/client.rb', line 125 def (token, secret, opts={}) request_token = OAuth::RequestToken.new(consumer, token, secret) @access_token = request_token.get_access_token(opts) @token = @access_token.token @secret = @access_token.secret @access_token end |
#badges ⇒ Hash
Get a list of all badges earned
7 8 9 |
# File 'lib/fitgem/badges.rb', line 7 def badges get("/user/#{@user_id}/badges.json") end |
#blood_pressure_on_date(date) ⇒ Hash
Get blood pressure log entries for the supplied date
12 13 14 |
# File 'lib/fitgem/blood_pressure.rb', line 12 def blood_pressure_on_date(date) get("/user/#{@user_id}/bp/date/#{format_date(date)}.json") end |
#body_fat(opts = {}) ⇒ Hash
Get a list of logged body fat entries for the specified period
58 59 60 |
# File 'lib/fitgem/body_measurements.rb', line 58 def body_fat(opts = {}) get determine_body_uri("/user/#{@user_id}/body/log/fat", opts) end |
#body_fat_goal ⇒ Hash
Retrieve the body fat goal of the current user
67 68 69 |
# File 'lib/fitgem/body_measurements.rb', line 67 def body_fat_goal get("/user/#{@user_id}/body/log/fat/goal.json") end |
#body_measurements_on_date(date) ⇒ Hash
Get the body measurements logged on a specified date
14 15 16 |
# File 'lib/fitgem/body_measurements.rb', line 14 def body_measurements_on_date(date) get("/user/#{@user_id}/body/date/#{format_date(date)}.json") end |
#body_weight(opts = {}) ⇒ Hash
Get a list of logged body weight entries for the specified period
31 32 33 |
# File 'lib/fitgem/body_measurements.rb', line 31 def body_weight(opts = {}) get determine_body_uri("/user/#{@user_id}/body/log/weight", opts) end |
#body_weight_goal ⇒ Hash
Retrieve the body weight goal of the current user
41 42 43 |
# File 'lib/fitgem/body_measurements.rb', line 41 def body_weight_goal get("/user/#{@user_id}/body/log/weight/goal.json") end |
#connected? ⇒ Boolean
Get the current state of the client
157 158 159 |
# File 'lib/fitgem/client.rb', line 157 def connected? !@access_token.nil? end |
#construct_date_range_fragment(options) ⇒ Object
protected
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/fitgem/time_range.rb', line 73 def construct_date_range_fragment() range_str = "date/" if [:base_date] && [:period] range_str += "#{format_date([:base_date])}/#{[:period]}" elsif [:base_date] && [:end_date] range_str += "#{format_date([:base_date])}/#{format_date([:end_date])}" else raise Fitgem::InvalidTimeRange, "Must supply either base_date and period OR base_date and end_date" end range_str end |
#create_food(opts) ⇒ Hash
Create a new food and add it to fitbit
160 161 162 |
# File 'lib/fitgem/foods.rb', line 160 def create_food(opts) post("/foods.json", opts) end |
#create_or_update_body_fat_goal(fatPercentageGoal) ⇒ Object
Create or update a user’s body fat percentage goal
165 166 167 168 |
# File 'lib/fitgem/body_measurements.rb', line 165 def create_or_update_body_fat_goal(fatPercentageGoal) opts = {fat: fatPercentageGoal} post("/user/#{@user_id}/body/log/fat/goal.json", opts) end |
#create_or_update_body_weight_goal(startDate, startWeight, goalWeight) ⇒ Object
Create or update a user’s weight goal
154 155 156 157 |
# File 'lib/fitgem/body_measurements.rb', line 154 def create_or_update_body_weight_goal(startDate, startWeight, goalWeight) opts = {startDate: format_date(startDate), startWeight: startWeight, weight: goalWeight} post("/user/#{@user_id}/body/log/weight/goal.json", opts) end |
#create_or_update_daily_goal(opts) ⇒ Object
Create or update a user’s daily goal
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/fitgem/activities.rb', line 170 def create_or_update_daily_goal(opts) unless opts[:type] && [:steps, :distance, :floors, :caloriesOut].include?(opts[:type]) raise InvalidArgumentError, 'Must specify type in order to create or update a daily goal. One of (:steps, :distance, :floors, or :caloriesOut) is required.' end unless opts[:value] raise InvalidArgumentError, 'Must specify value of the daily goal to be created or updated.' end post("/user/#{@user_id}/activities/goals/daily.json", opts) end |
#create_or_update_weekly_goal(opts) ⇒ Object
Create or update a user’s weekly goal
192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/fitgem/activities.rb', line 192 def create_or_update_weekly_goal(opts) unless opts[:type] && [:steps, :distance, :floors].include?(opts[:type]) raise InvalidArgumentError, 'Must specify type in order to create or update a weekly goal. One of (:steps, :distance, or :floors) is required.' end unless opts[:value] raise InvalidArgumentError, 'Must specify value of the weekly goal to be created or updated.' end post("/user/#{@user_id}/activities/goals/weekly.json", opts) end |
#create_subscription(opts) ⇒ Integer, Hash
You must check the HTTP response code to check the status of the request to add a subscription. See https://wiki.fitbit.com/display/API/Fitbit+Subscriptions+API for information about what the codes mean.
Creates a notification subscription
29 30 31 32 |
# File 'lib/fitgem/notifications.rb', line 29 def create_subscription(opts) resp = raw_post make_subscription_url(opts.merge({:use_subscription_id => true})), EMPTY_BODY, make_headers(opts) [resp.code, extract_response_body(resp)] end |
#daily_goals ⇒ Hash
Get the daily activity goals for the current user
97 98 99 |
# File 'lib/fitgem/activities.rb', line 97 def daily_goals goals end |
#data_by_time_range(resource_path, opts) ⇒ Hash
Gets historical resource data in the time range specified by options hash param. The time range can either be specified by :base_date and :end_date OR by using :base_date and a :period (supported periods are 1d, 7d, 30d, 1w, 1m, 3m, 6m, 1y, max)
Supported values for resource_path
are:
Food:
/foods/log/caloriesIn
/foods/log/water
Activity:
/activities/log/calories
/activities/log/steps
/activities/log/distance
/activities/log/floors
/activities/log/elevation
/activities/log/minutesSedentary
/activities/log/minutesLightlyActive
/activities/log/minutesFairlyActive
/activities/log/minutesVeryActive
/activities/log/activeScore
/activities/log/activityCalories
Raw Tracker Activity:
/activities/tracker/calories
/activities/tracker/steps
/activities/tracker/distance
/activities/tracker/floors
/activities/tracker/elevation
/activities/tracker/activeScore
Sleep:
/sleep/startTime
/sleep/timeInBed
/sleep/minutesAsleep
/sleep/awakeningsCount
/sleep/minutesAwake
/sleep/minutesToFallAsleep
/sleep/minutesAfterWakeup
/sleep/efficiency
Body:
/body/weight
/body/bmi
/body/fat
66 67 68 69 |
# File 'lib/fitgem/time_range.rb', line 66 def data_by_time_range(resource_path, opts) range = construct_date_range_fragment(opts) get("/user/#{@user_id}#{resource_path}/#{range}.json") end |
#decline_invite(requestor_id) ⇒ Hash
Decline a friend invite
78 79 80 |
# File 'lib/fitgem/friends.rb', line 78 def decline_invite(requestor_id) respond_to_invite(requestor_id, false) end |
#delete_alarm(alarm_id, device_id) ⇒ Hash
Remove a silent alarm from your profile
41 42 43 |
# File 'lib/fitgem/alarms.rb', line 41 def delete_alarm(alarm_id, device_id) delete("/user/#{@user_id}/devices/tracker/#{device_id}/alarms/#{alarm_id}.json") end |
#delete_blood_pressure_log(blood_pressure_log_id) ⇒ Hash
Delete logged blood pressure information
44 45 46 |
# File 'lib/fitgem/blood_pressure.rb', line 44 def delete_blood_pressure_log(blood_pressure_log_id) delete("/user/-/bp/#{blood_pressure_log_id}.json") end |
#delete_body_fat_log(logId) ⇒ Object
Delete user’s body fat log entry with the given id
188 189 190 |
# File 'lib/fitgem/body_measurements.rb', line 188 def delete_body_fat_log(logId) delete("/user/#{@user_id}/body/log/fat/#{logId}.json") end |
#delete_body_weight_log(logId) ⇒ Object
Delete user’s body weight log entry with the given id
179 180 181 |
# File 'lib/fitgem/body_measurements.rb', line 179 def delete_body_weight_log(logId) delete("/user/#{@user_id}/body/log/weight/#{logId}.json") end |
#delete_heart_rate_log(heart_rate_log_id) ⇒ Hash
Delete logged heart rate information
46 47 48 |
# File 'lib/fitgem/heart_rate.rb', line 46 def delete_heart_rate_log(heart_rate_log_id) delete("/user/-/heart/#{heart_rate_log_id}.json") end |
#delete_logged_activity(activity_log_id) ⇒ Hash
Delete a logged activity
215 216 217 |
# File 'lib/fitgem/activities.rb', line 215 def delete_logged_activity(activity_log_id) delete("/user/#{@user_id}/activities/#{activity_log_id}.json") end |
#delete_logged_food(food_log_id) ⇒ Hash
Remove a logged food entry
126 127 128 |
# File 'lib/fitgem/foods.rb', line 126 def delete_logged_food(food_log_id) delete("/user/#{@user_id}/foods/log/#{food_log_id}.json") end |
#delete_sleep_log(sleep_log_id) ⇒ Hash
Delete logged sleep data
The sleep log id is the one returned when sleep data is recorded via #log_sleep.
46 47 48 |
# File 'lib/fitgem/sleep.rb', line 46 def delete_sleep_log(sleep_log_id) delete("/user/#{@user_id}/sleep/#{sleep_log_id}.json") end |
#delete_water_log(water_log_id) ⇒ Hash
Delete logged water consumption
46 47 48 |
# File 'lib/fitgem/water.rb', line 46 def delete_water_log(water_log_id) delete("/user/-/foods/log/water/#{water_log_id}.json") end |
#device_info(device_id) ⇒ Hash
Get the details about a specific device
The ID required for this call could be found by getting the list of devices with a call to #devices.
23 24 25 |
# File 'lib/fitgem/devices.rb', line 23 def device_info(device_id) get("/user/#{@user_id}/devices/#{device_id}.json") end |
#devices ⇒ Array
Get a list of devices for the current account
11 12 13 |
# File 'lib/fitgem/devices.rb', line 11 def devices get("/user/#{@user_id}/devices.json") end |
#favorite_activities ⇒ Array
Get a list of activities marked as favorite by the user
52 53 54 |
# File 'lib/fitgem/activities.rb', line 52 def favorite_activities get("/user/#{@user_id}/activities/favorite.json") end |
#favorite_foods ⇒ Array
Get a list of the favorite logged foods
36 37 38 |
# File 'lib/fitgem/foods.rb', line 36 def favorite_foods get("/user/#{@user_id}/foods/log/favorite.json") end |
#find_food(query) ⇒ Array
Search the foods database
65 66 67 |
# File 'lib/fitgem/foods.rb', line 65 def find_food(query) get("/foods/search.json?query=#{URI.escape(query)}") end |
#food_info(food_id) ⇒ Hash
Get detailed information for a food
73 74 75 |
# File 'lib/fitgem/foods.rb', line 73 def food_info(food_id) get("/foods/#{food_id}.json") end |
#foods_on_date(date) ⇒ Array
Get all foods logged on the supplied date
12 13 14 |
# File 'lib/fitgem/foods.rb', line 12 def foods_on_date(date) get("/user/#{@user_id}/foods/log/date/#{format_date(date)}.json") end |
#foods_units ⇒ Array
Get a list of all of the available food units
43 44 45 |
# File 'lib/fitgem/foods.rb', line 43 def foods_units get('/foods/units.json') end |
#format_date(date) ⇒ String
Format any of a variety of date types into the formatted string required when using the fitbit API.
The date parameter can take several different kind of objects: a DateTime object, a Date object, a Time object or a String object. Furthermore, the string object may be either the date in a preformatted string (“yyyy-MM-dd”), or it may be the string values “today” or “tomorrow”.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/fitgem/helpers.rb', line 17 def format_date(date) if date.is_a? String case date when 'today' return Date.today.strftime("%Y-%m-%d") when 'yesterday' return (Date.today-1).strftime("%Y-%m-%d") else unless date =~ /\d{4}\-\d{2}\-\d{2}/ raise Fitgem::InvalidDateArgument, "Invalid date (#{date}), must be in yyyy-MM-dd format" end return date end elsif Date === date || Time === date || DateTime === date return date.strftime("%Y-%m-%d") else raise Fitgem::InvalidDateArgument, "Date used must be a date/time object or a string in the format YYYY-MM-DD; supplied argument is a #{date.class}" end end |
#format_time(time, opts = {}) ⇒ String
Format any of a variety of time-related types into the formatted string required when using the fitbit API.
The time parameter can take several different kind of objects: a DateTime object, a Time object, or a String Object. Furthermore, the string object may be either the date in a preformatted string (“HH:mm”), or it may be the string value “now” to indicate that the time value used is the current localtime.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/fitgem/helpers.rb', line 51 def format_time(time, opts = {}) format = opts[:include_timezone] ? "%H:%M%:z" : "%H:%M" if time.is_a? String case time when 'now' return DateTime.now.strftime format else unless time =~ /^\d{2}\:\d{2}$/ raise Fitgem::InvalidTimeArgument, "Invalid time (#{time}), must be in HH:mm format" end timezone = DateTime.now.strftime("%:z") return opts[:include_timezone] ? [ time, timezone ].join : time end elsif DateTime === time || Time === time return time.strftime format else raise Fitgem::InvalidTimeArgument, "Date used must be a valid time object or a string in the format HH:mm; supplied argument is a #{time.class}" end end |
#frequent_activities ⇒ Array
Get a list of the frequently logged activities
38 39 40 |
# File 'lib/fitgem/activities.rb', line 38 def frequent_activities get("/user/#{@user_id}/activities/frequent.json") end |
#frequent_foods ⇒ Array
Get a list of the frequently logged foods
28 29 30 |
# File 'lib/fitgem/foods.rb', line 28 def frequent_foods get("/user/#{@user_id}/foods/log/frequent.json") end |
#friends ⇒ Array
Get a list of the current user’s friends
11 12 13 |
# File 'lib/fitgem/friends.rb', line 11 def friends get("/user/#{@user_id}/friends.json") end |
#get_alarms(device_id) ⇒ Hash
Retrieve the silent alarms of your device
30 31 32 |
# File 'lib/fitgem/alarms.rb', line 30 def get_alarms(device_id) get("/user/#{@user_id}/devices/tracker/#{device_id}/alarms.json") end |
#glucose_on_date(date) ⇒ Hash
Get glucose log entries for the supplied date
12 13 14 |
# File 'lib/fitgem/glucose.rb', line 12 def glucose_on_date(date) get("/user/#{@user_id}/glucose/date/#{format_date(date)}.json") end |
#goals ⇒ Hash
Use the daily_goals method; in v1.0.0 this method will be removed
Get the daily activity goals for the current user
87 88 89 |
# File 'lib/fitgem/activities.rb', line 87 def goals get("/user/#{@user_id}/activities/goals/daily.json") end |
#heart_rate_on_date(date) ⇒ Hash
Get heart rate log entries for the supplied date
12 13 14 |
# File 'lib/fitgem/heart_rate.rb', line 12 def heart_rate_on_date(date) get("/user/#{@user_id}/heart/date/#{format_date(date)}.json") end |
#intraday_time_series(opts) ⇒ Hash
Get intraday time series data for the specified resource
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/fitgem/activities.rb', line 245 def intraday_time_series(opts) unless opts[:resource] && [:calories, :steps, :distance, :floors, :elevation].include?(opts[:resource]) raise Fitgem::InvalidArgumentError, 'Must specify resource to fetch intraday time series data for. One of (:calories, :steps, :distance, :floors, or :elevation) is required.' end unless opts[:date] raise Fitgem::InvalidArgumentError, 'Must specify the date to fetch intraday time series data for.' end unless opts[:detailLevel] && %w(1min 15min).include?(opts[:detailLevel]) raise Fitgem::InvalidArgumentError, 'Must specify the data resolution to fetch intraday time series data for. One of (\"1d\" or \"15min\") is required.' end resource = opts.delete(:resource) date = format_date(opts.delete(:date)) detail_level = opts.delete(:detailLevel) time_window_specified = opts[:startTime] || opts[:endTime] resource_path = "/user/#{@user_id}/activities/" if time_window_specified start_time = format_time(opts.delete(:startTime)) end_time = format_time(opts.delete(:endTime)) resource_path += "#{resource}/date/#{date}/1d/#{detail_level}/time/#{start_time}/#{end_time}.json" else resource_path += "#{resource}/date/#{date}/1d/#{detail_level}.json" end get(resource_path, opts) end |
#invite_friend(opts) ⇒ Hash
Create an invite for a user to connect with the current user as a friend
In order to invite a user, either an :email or a valid :userId must be supplied in the opts
param hash.
54 55 56 57 58 59 60 61 62 |
# File 'lib/fitgem/friends.rb', line 54 def invite_friend(opts) unless opts[:email] || opts[:user_id] raise InvalidArgumentError.new "invite_friend hash argument must include :email or :user_id" end = {} [:invitedUserEmail] = opts[:email] if opts[:email] [:invitedUserId] = opts[:user_id] if opts[:user_id] post("/user/#{@user_id}/friends/invitations.json", ) end |
#label_for_measurement(measurement_type, respect_user_unit_preferences = true) ⇒ String
Fetch the correct label for the desired measurement unit.
The general use case for this method is that you are using the client for a specific user, and wish to get the correct labels for the unit measurements returned for that user.
A secondary use case is that you wish to get the label for a measurement given a unit system that you supply (by setting the Fitgem::Client.api_unit_system attribute).
In order for this method to get the correct value for the current user’s preferences, the client must have the ability to make API calls. If you respect_user_unit_preferences is passed as ‘true’ (or left as the default value) and the client cannot make API calls then an error will be raised by the method.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/fitgem/helpers.rb', line 96 def label_for_measurement(measurement_type, respect_user_unit_preferences=true) unless [:duration, :distance, :elevation, :height, :weight, :measurements, :liquids, :blood_glucose].include?(measurement_type) raise InvalidMeasurementType, "Supplied measurement_type parameter must be one of [:duration, :distance, :elevation, :height, :weight, :measurements, :liquids, :blood_glucose], current value is :#{measurement_type}" end selected_unit_system = api_unit_system if respect_user_unit_preferences unless connected? raise ConnectionRequiredError, "No connection to Fitbit API; one is required when passing respect_user_unit_preferences=true" end # Cache the unit systems for the current user @unit_systems ||= self.user_info['user'].select {|key, value| key =~ /Unit$/ } case measurement_type when :distance selected_unit_system = @unit_systems["distanceUnit"] when :height selected_unit_system = @unit_systems["heightUnit"] when :liquids selected_unit_system = @unit_systems["waterUnit"] when :weight selected_unit_system = @unit_systems["weightUnit"] when :blood_glucose selected_unit_system = @unit_systems["glucoseUnit"] else selected_unit_system = api_unit_system end end # Fix the METRIC system difference selected_unit_system = Fitgem::ApiUnitSystem.METRIC if selected_unit_system == "METRIC" # Ensure the target unit system is one that we know about unless [ApiUnitSystem.US, ApiUnitSystem.UK, ApiUnitSystem.METRIC].include?(selected_unit_system) raise InvalidUnitSystem, "The select unit system must be one of [ApiUnitSystem.US, ApiUnitSystem.UK, ApiUnitSystem.METRIC], current value is #{selected_unit_system}" end unit_mappings[selected_unit_system][measurement_type] end |
#leaderboard ⇒ Hash
Get the leaderboard of friends’ weekly activities
18 19 20 |
# File 'lib/fitgem/friends.rb', line 18 def leaderboard get("/user/#{@user_id}/friends/leaderboard.json") end |
#log_activity(opts) ⇒ Hash
Log an activity to fitbit
140 141 142 |
# File 'lib/fitgem/activities.rb', line 140 def log_activity(opts) post("/user/#{@user_id}/activities.json", opts) end |
#log_blood_pressure(opts) ⇒ Hash
Log blood pressure information to fitbit
29 30 31 32 33 34 35 36 37 |
# File 'lib/fitgem/blood_pressure.rb', line 29 def log_blood_pressure(opts) unless opts[:systolic] && opts[:diastolic] && opts[:date] raise Fitgem::InvalidArgumentError, "Must include :systolic, :diastolic, and :date in order to log blood pressure data" end opts[:date] = format_date(opts[:date]) opts[:time] = format_time(opts[:time]) if opts[:time] post("/user/#{@user_id}/bp.json", opts) end |
#log_body_fat(fatPercentage, date, opts = {}) ⇒ Object
Log body fat percentage
137 138 139 140 141 142 |
# File 'lib/fitgem/body_measurements.rb', line 137 def log_body_fat(fatPercentage, date, opts={}) opts[:fat] = fatPercentage opts[:date] = format_date(date) opts[:time] = format_time(opts[:time]) if opts[:time] post("/user/#{@user_id}/body/fat.json", opts) end |
#log_body_measurements(opts) ⇒ Hash
Log body measurements to fitbit for the current user
At least ONE measurement item is REQUIRED in the call, as well as the date. All measurement values to be logged for the user must be either an Integer, a Decimal value, or a String in “X.XX” format. The measurement units used for the supplied measurements are based on which ApiUnitSystem is set in #api_unit_system.
102 103 104 105 106 |
# File 'lib/fitgem/body_measurements.rb', line 102 def log_body_measurements(opts) # Update the date (if exists) opts[:date] = format_date(opts[:date]) if opts[:date] post("/user/#{@user_id}/body.json", opts) end |
#log_food(opts) ⇒ Hash
Log food to fitbit for the current user
To log a food, either a foodId
or foodName
is REQUIRED.
105 106 107 |
# File 'lib/fitgem/foods.rb', line 105 def log_food(opts) post("/user/#{@user_id}/foods/log.json", opts) end |
#log_glucose(opts) ⇒ Hash
Log glucose information to fitbit
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/fitgem/glucose.rb', line 31 def log_glucose(opts) unless opts[:tracker] || opts[:hba1c] raise Fitgem::InvalidArgumentError, "Must include :tracker or :hba1c in order to log glucose data" end if opts[:tracker] && opts[:hba1c].nil? && opts[:glucose].nil? raise Fitgem::InvalidArgumentError, "Must include :glucose if using :tracker with no :hba1c value in order to log glucose data" end opts[:date] = format_date(opts[:date]) opts[:time] = format_time(opts[:time]) if opts[:time] post("/user/#{@user_id}/glucose.json", opts) end |
#log_heart_rate(opts) ⇒ Hash
Log heart rate information to fitbit
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/fitgem/heart_rate.rb', line 30 def log_heart_rate(opts) unless opts[:tracker] && opts[:heart_rate] && opts[:date] raise Fitgem::InvalidArgumentError, "Must include :tracker, :heart_rate, and :date in order to lof heart rate data" end opts[:heartRate] = opts.delete :heart_rate opts[:date] = format_date(opts[:date]) opts[:time] = format_time(opts[:time]) if opts[:time] post("/user/#{@user_id}/heart.json", opts) end |
#log_sleep(opts) ⇒ Hash
Log sleep data to fitbit for current user
All aspects of the options hash are REQUIRED.
33 34 35 |
# File 'lib/fitgem/sleep.rb', line 33 def log_sleep(opts) post("/user/#{@user_id}/sleep.json", opts) end |
#log_water(opts) ⇒ Hash
Log water consumption to fitbit
32 33 34 35 36 37 38 39 |
# File 'lib/fitgem/water.rb', line 32 def log_water(opts) unless opts[:amount] && opts[:date] raise Fitgem::InvalidArgumentError, "Must include both an :amount and :date to log water" end opts[:date] = format_date(opts[:date]) post("/user/#{@user_id}/foods/log/water.json",opts) end |
#log_weight(weight, date, opts = {}) ⇒ Hash
Log weight to fitbit for the current user
121 122 123 124 |
# File 'lib/fitgem/body_measurements.rb', line 121 def log_weight(weight, date, opts={}) opts[:time] = format_time(opts[:time]) if opts[:time] post("/user/#{@user_id}/body/log/weight.json", opts.merge(:weight => weight, :date => format_date(date))) end |
#make_headers(opts) ⇒ Hash (protected)
Create the headers hash for subscription management calls
This method both adds approriate headers given what is in the options hash, as well as removes extraneous hash entries that are not needed in the headers of the request sent to the API.
83 84 85 86 87 88 89 |
# File 'lib/fitgem/notifications.rb', line 83 def make_headers(opts) headers = {} if opts[:subscriber_id] headers['X-Fitbit-Subscriber-Id'] = opts[:subscriber_id] end headers end |
#make_subscription_url(opts) ⇒ String (protected)
Create the subscription management API url
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/fitgem/notifications.rb', line 103 def make_subscription_url(opts) validate_subscription_type opts[:type] path = if opts[:type] == :all "" else "/"+opts[:type].to_s end url = "/user/#{@user_id}#{path}/apiSubscriptions" if opts[:use_subscription_id] unless opts[:subscription_id] raise Fitgem::InvalidArgumentError, "Must include options[:subscription_id]" end url += "/#{opts[:subscription_id]}" end url += ".json" end |
#meals ⇒ Hash
Get a hash containing the user’s meal informarion
52 53 54 |
# File 'lib/fitgem/foods.rb', line 52 def meals get("/user/#{@user_id}/meals.json") end |
#monthly_leaderboard ⇒ Hash
Monthly leaderboards are no longer available from Fitbit. Please update to use #leaderboard
Get the leaderboard of friends’ weekly activities
27 28 29 |
# File 'lib/fitgem/friends.rb', line 27 def monthly_leaderboard raise DeprecatedApiError, 'Fitbit no longer exposes a monthly leaderboard. See https://wiki.fitbit.com/display/API/API-Get-Friends-Leaderboard for more information. Use #leaderboard() instead.' end |
#recent_activities ⇒ Array
Get a list of recently logged activities
45 46 47 |
# File 'lib/fitgem/activities.rb', line 45 def recent_activities get("/user/#{@user_id}/activities/recent.json") end |
#recent_foods ⇒ Array
Get a list of the recently logged foods
20 21 22 |
# File 'lib/fitgem/foods.rb', line 20 def recent_foods get("/user/#{@user_id}/foods/log/recent.json") end |
#reconnect(token, secret) ⇒ OAuth::AccessToken
Reconnect to the fitbit API with a stored oauth token and oauth secret
This method should be used if you have previously directed a user through the OAuth process and received a token and secret that were stored for later use. Using reconnect
you can ‘reconstitute’ the access_token required for API calls without needing the user to go through the OAuth process again.
148 149 150 151 152 |
# File 'lib/fitgem/client.rb', line 148 def reconnect(token, secret) @token = token @secret = secret access_token end |
#remove_favorite_activity(activity_id) ⇒ Hash
Unmark an activity as a favorite
224 225 226 |
# File 'lib/fitgem/activities.rb', line 224 def remove_favorite_activity(activity_id) delete("/user/#{@user_id}/activities/log/favorite/#{activity_id}.json") end |
#remove_favorite_food(food_id) ⇒ Hash
Unmark a food as a favorite
134 135 136 |
# File 'lib/fitgem/foods.rb', line 134 def remove_favorite_food(food_id) delete("/user/#{@user_id}/foods/favorite/#{food_id}.json") end |
#remove_subscription(opts) ⇒ Integer, Hash
You must check the HTTP response code to check the status of the request to remove a subscription. See https://wiki.fitbit.com/display/API/Fitbit+Subscriptions+API for information about what the codes mean.
Removes a notification subscription
50 51 52 53 |
# File 'lib/fitgem/notifications.rb', line 50 def remove_subscription(opts) resp = raw_delete make_subscription_url(opts.merge({:use_subscription_id => true})), make_headers(opts) [resp.code, extract_response_body(resp)] end |
#request_token(opts = {}) ⇒ OAuth::RequestToken
Get an oauth request token
166 167 168 |
# File 'lib/fitgem/client.rb', line 166 def request_token(opts={}) consumer.get_request_token(opts) end |
#sleep_on_date(date) ⇒ Array
Get sleep data for specified date
11 12 13 |
# File 'lib/fitgem/sleep.rb', line 11 def sleep_on_date(date) get("/user/#{@user_id}/sleep/date/#{format_date(date)}.json") end |
#subscriptions(opts) ⇒ Hash
Get a list of all subscriptions
13 14 15 |
# File 'lib/fitgem/notifications.rb', line 13 def subscriptions(opts) get make_subscription_url(opts), make_headers(opts) end |
#unit_mappings ⇒ Object (protected)
Defined mappings for unit measurements to labels
179 180 181 182 183 184 185 |
# File 'lib/fitgem/helpers.rb', line 179 def unit_mappings { ApiUnitSystem.US => { :duration => "milliseconds", :distance => "miles", :elevation => "feet", :height => "inches", :weight => "pounds", :measurements => "inches", :liquids => "fl oz", :blood_glucose => "mg/dL" }, ApiUnitSystem.UK => { :duration => "milliseconds", :distance => "kilometers", :elevation => "meters", :height => "centimeters", :weight => "stone", :measurements => "centimeters", :liquids => "mL", :blood_glucose => "mmol/l" }, ApiUnitSystem.METRIC => { :duration => "milliseconds", :distance => "kilometers", :elevation => "meters", :height => "centimeters", :weight => "kilograms", :measurements => "centimeters", :liquids => "mL", :blood_glucose => "mmol/l" } } end |
#update_alarm(alarm_id, device_id, opts) ⇒ Hash
Update an existing alarm
61 62 63 64 |
# File 'lib/fitgem/alarms.rb', line 61 def update_alarm(alarm_id, device_id, opts) opts[:time] = format_time opts[:time], include_timezone: true post("/user/#{@user_id}/devices/tracker/#{device_id}/alarms/#{alarm_id}.json", opts) end |
#update_user_info(opts) ⇒ Hash
Update profile information for current user
46 47 48 49 |
# File 'lib/fitgem/users.rb', line 46 def update_user_info(opts) opts[:birthday] = format_date(opts[:birthday]) if opts[:birthday] post("/user/#{@user_id}/profile.json", opts) end |
#user_info(opts = {}) ⇒ Hash
Get information aobut current user
7 8 9 |
# File 'lib/fitgem/users.rb', line 7 def user_info(opts={}) get("/user/#{@user_id}/profile.json", opts) end |
#validate_subscription_type(subscription_type) ⇒ Boolean (protected)
Ensures that the type supplied is valid
65 66 67 68 69 70 |
# File 'lib/fitgem/notifications.rb', line 65 def validate_subscription_type(subscription_type) unless subscription_type && SUBSCRIBABLE_TYPES.include?(subscription_type) raise Fitgem::InvalidArgumentError, "Invalid subscription type (valid values are #{SUBSCRIBABLE_TYPES.join(', ')})" end true end |
#water_on_date(date) ⇒ Hash
Get water log entries for the supplied date
12 13 14 |
# File 'lib/fitgem/water.rb', line 12 def water_on_date(date) get("/user/#{@user_id}/foods/log/water/date/#{format_date(date)}.json") end |
#weekly_goals ⇒ Hash
Get the weekly activity goals for the current user
105 106 107 |
# File 'lib/fitgem/activities.rb', line 105 def weekly_goals get("/user/#{@user_id}/activities/goals/weekly.json") end |
#weekly_leaderboard ⇒ Hash
Please update to use #leaderboard
Get the leaderboard of friends’ weekly activities
36 37 38 |
# File 'lib/fitgem/friends.rb', line 36 def weekly_leaderboard raise DeprecatedApiError, 'Fitbit now only exposes a weekly leaderboard, Use #leaderboard() instead.' end |