Class: FitbitAPI::Client
- Inherits:
-
Object
- Object
- FitbitAPI::Client
- Defined in:
- lib/fitbit_api/body.rb,
lib/fitbit_api/food.rb,
lib/fitbit_api/user.rb,
lib/fitbit_api/goals.rb,
lib/fitbit_api/meals.rb,
lib/fitbit_api/sleep.rb,
lib/fitbit_api/water.rb,
lib/fitbit_api/alarms.rb,
lib/fitbit_api/client.rb,
lib/fitbit_api/devices.rb,
lib/fitbit_api/friends.rb,
lib/fitbit_api/activities.rb,
lib/fitbit_api/heart_rate.rb,
lib/fitbit_api/temperature.rb,
lib/fitbit_api/cardio_score.rb,
lib/fitbit_api/helpers/utils.rb,
lib/fitbit_api/subscriptions.rb,
lib/fitbit_api/breathing_rate.rb,
lib/fitbit_api/electrocardiogram.rb,
lib/fitbit_api/oxygen_saturation.rb,
lib/fitbit_api/active_zone_minutes.rb,
lib/fitbit_api/heart_rate_variability.rb
Constant Summary collapse
- BODY_RESOURCES =
%w[bmi fat weight].freeze
- FOOD_RESOURCES =
%w[caloriesIn water].freeze
- SLEEP_RESOURCES =
%w[startTime timeInBed minutesAsleep awakeningsCount minutesAwake minutesToFallAsleep minutesAfterWakeup efficiency].freeze
- ACTIVITY_RESOURCES =
%w[calories caloriesBMR steps distance floors elevation minutesSedentary minutesLightlyActive minutesFairlyActive minutesVeryActive activityCalories tracker/calories tracker/steps tracker/distance tracker/floors tracker/elevation tracker/minutesSedentary tracker/minutesLightlyActive tracker/minutesFairlyActive tracker/minutesVeryActive tracker/activityCalories].freeze
- ACTIVITY_INTRADAY_RESOURCES =
%w[calories steps distance floors elevation].freeze
- PERIODS =
%w[1d 7d 30d 1w 1m 3m 6m 1y max].freeze
Instance Attribute Summary collapse
-
#api_version ⇒ Object
Returns the value of attribute api_version.
-
#auto_refresh_token ⇒ Object
Returns the value of attribute auto_refresh_token.
-
#locale ⇒ Object
Returns the value of attribute locale.
-
#on_token_refresh ⇒ Object
Returns the value of attribute on_token_refresh.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#snake_case_keys ⇒ Object
Returns the value of attribute snake_case_keys.
-
#symbolize_keys ⇒ Object
Returns the value of attribute symbolize_keys.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
-
#unit_system ⇒ Object
Returns the value of attribute unit_system.
-
#user_id ⇒ Object
readonly
Returns the value of attribute user_id.
Instance Method Summary collapse
-
#active_zone_minutes_intraday_time_series(opts = {}) ⇒ Object
Retrieves the Active Zone Minutes (AZM) intraday time series data for a specific date or 24 hour period.
-
#active_zone_minutes_time_series(opts = {}) ⇒ Object
Returns the daily summary Active Zone Minutes (AZM) values over a specified date range or period.
-
#activity(activity_id) ⇒ Object
Returns the details of a specific activity in the Fitbit activities database in the format requested.
- #activity_intraday_time_series(resource, opts = {}) ⇒ Object
-
#activity_logs_list(params = {}) ⇒ Object
Retrieves a list of a user’s activity log entries before or after a given day with offset and limit using units in the unit system which corresponds to the Accept-Language header provided.
- #activity_time_series(resource, opts = {}) ⇒ Object
-
#add_alarm(tracker_id, body = {}) ⇒ Object
Adds the alarm settings to a given ID for a given device.
-
#add_favorite_activity(activity_id) ⇒ Object
Adds the activity with the given ID to user’s list of favorite activities.
-
#add_favorite_food(food_id) ⇒ Object
Adds a food with the given ID to the user’s list of favorite foods.
-
#alarms(tracker_id) ⇒ Object
Returns a list of the set alarms connected to a user’s account.
-
#all_activities ⇒ Object
Gets a list of all valid Fitbit public activities from the activities catalog as well as private custom activities the user created.
-
#auth_url ⇒ Object
Returns the authorize endpoint URL of the OAuth2 provider.
-
#badges ⇒ Object
Retrieves a list of the user’s badges.
-
#body_fat_goal ⇒ Object
Retrieves a user’s current body fat percentage goal.
- #body_fat_logs(date = Date.today) ⇒ Object
- #body_time_series(resource, opts = {}) ⇒ Object
-
#breathing_rate_intraday(opts = {}) ⇒ Object
Returns the intraday breathing rate data for a given date or date range.
-
#breathing_rate_summary(opts = {}) ⇒ Object
Returns the average breathing rate data for a given date or date range.
-
#cardio_score_summary(opts = {}) ⇒ Object
Returns the cardio fitness score data for a given date or date range.
-
#core_temperature_summary(opts = {}) ⇒ Object
Returns the core temperature data for a given date or date range.
-
#create_food(body) ⇒ Object
Creates a new private food for a user.
-
#create_food_log(body) ⇒ Object
Creates a food log entry.
-
#create_meal(body) ⇒ Object
Creates a meal with the given food.
-
#create_sleep_log(body) ⇒ Object
Creates a log entry for a sleep event.
-
#create_subscription(subscription_id, collection_path = nil) ⇒ Object
Creates a subscription to notify the application when a user has new data available.
-
#daily_activity_goals ⇒ Object
Retrieves a user’s current daily activity goals.
-
#daily_activity_summary(date = Date.today) ⇒ Object
Retrieves a summary and list of a user’s activities and activity log entries for a given day.
- #deep_keys_to_camel_case!(object) ⇒ Object
- #deep_keys_to_snake_case!(object) ⇒ Object
- #deep_symbolize_keys!(object) ⇒ Object
-
#deep_transform_keys!(object, &block) ⇒ Object
Inspired by ActiveSupport’s implementation.
-
#delete(path, params = {}, opts = {}, &block) ⇒ Object
Performs an authorized DELETE request to the configured API namespace.
-
#delete_activity(activity_log_id) ⇒ Object
Deletes a user’s activity log entry with the given ID.
-
#delete_alarm(tracker_id, alarm_id) ⇒ Object
Deletes the user’s device alarm entry with the given ID for a given device.
- #delete_body_fat_log(body_fat_log_id) ⇒ Object
-
#delete_favorite_activity(activity_id) ⇒ Object
Removes the activity with the given ID from a user’s list of favorite activities.
-
#delete_favorite_food(food_id) ⇒ Object
Deletes a food with the given ID from the user’s list of favorite foods.
-
#delete_food(food_id) ⇒ Object
Deletes a custom food created by the user.
-
#delete_food_log(food_log_id) ⇒ Object
Deletes a user’s food log entry using the given log ID.
-
#delete_meal(meal_id) ⇒ Object
Deletes an existing meal of the given meal ID.
-
#delete_sleep_log(sleep_log_id) ⇒ Object
Deletes a sleep log with the given log ID.
-
#delete_subscription(subscription_id, collection_path = nil) ⇒ Object
Deletes a subscription for a specific user.
-
#delete_water_log(water_log_id) ⇒ Object
Deleted a user’s water log entry using the given log ID.
- #delete_weight_log(weight_log_id) ⇒ Object
-
#devices ⇒ Object
Retrieves a list of Fitbit devices paired to a user’s account.
-
#ecg_logs_list(params = {}) ⇒ Object
This endpoint retrieves a list of the user’s Electrocardiogram (ECG) log entries before or after a given day.
-
#favorite_activities ⇒ Object
Returns a list of a user’s favorite activities.
- #favorite_foods ⇒ Object
-
#food_goals ⇒ Object
Retrieves the user’s current daily calorie consumption goal and/or food plan.
-
#food_locales ⇒ Object
Retrieves the food locales used to search, log or create food.
- #food_logs(date = Date.today) ⇒ Object
- #food_time_series(resource, opts = {}) ⇒ Object
-
#food_units ⇒ Object
Retrieves a list of all valid Fitbit food units.
- #format_date(date) ⇒ Object
- #format_scope(scope) ⇒ Object
- #format_time(time) ⇒ Object
-
#frequent_activities ⇒ Object
Retrieves a list of a user’s frequent activities.
- #frequent_foods ⇒ Object
-
#friends ⇒ Object
Retrieves a list of the Fitbit user’s friends.
-
#friends_leaderboard ⇒ Object
Retrieves the user’s friends leaderboard.
-
#get(path, params = {}, opts = {}, &block) ⇒ Object
Performs an authorized GET request to the configured API namespace.
-
#get_token(auth_code) ⇒ Object
Returns an OAuth2::AccessToken instance obtained from the given authorization code.
- #heart_rate_intraday_time_series(opts = {}) ⇒ Object
- #heart_rate_time_series(opts = {}) ⇒ Object
-
#heart_rate_variability_intraday(opts = {}) ⇒ Object
Returns the heart rate variability intraday data for a given date or date range.
-
#heart_rate_variability_summary(opts = {}) ⇒ Object
Returns the heart rate variability data for a given date or date range.
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
-
#lifetime_stats ⇒ Object
Retrieves the user’s activity statistics in the format requested using units in the unit system which corresponds to the Accept-Language header provided.
-
#log_activity(body) ⇒ Object
Creates log entry for an activity or user’s private custom activity using units in the unit system which corresponds to the Accept-Language header provided.
- #log_body_fat(body) ⇒ Object
-
#log_water(body) ⇒ Object
Create a user’s water log entry.
- #log_weight(body) ⇒ Object
-
#meal(meal_id) ⇒ Object
Retrieves a single meal created by the user from their food log given the meal id.
-
#meals ⇒ Object
Retrieves a list of meals created by the user from their food log.
-
#oxygen_saturation_intraday(opts = {}) ⇒ Object
Returns the oxygen saturation intraday data for a given date or date range.
-
#oxygen_saturation_summary(opts = {}) ⇒ Object
Returns the oxygen saturation summary data for a given date or date range.
-
#post(path, body = {}, opts = {}, &block) ⇒ Object
Performs an authorized POST request to the configured API namespace.
-
#profile ⇒ Object
Retrieves the user’s profile data.
-
#recent_activities ⇒ Object
Retrieves a list of a user’s recent activities types logged with some details of the last activity log of that type.
- #recent_foods ⇒ Object
-
#refresh_token! ⇒ Object
Refreshes the current Access Token.
-
#revoke_token! ⇒ Object
Revokes the user’s authorizations and all associated tokens.
-
#search_foods(params) ⇒ Object
Retrieves a list of public foods from the Fitbit foods database and private foods the user created.
-
#skin_temperature_summary(opts = {}) ⇒ Object
Returns the skin temperature data for a given date or date range.
-
#sleep_goal ⇒ Object
Retrieves a user’s current sleep goal.
-
#sleep_logs(date = Date.today) ⇒ Object
Returns a list of a user’s sleep log entries for a given date.
-
#sleep_logs_by_date_range(opts = {}) ⇒ Object
Returns a list of a user’s sleep log entries for a given date range.
-
#sleep_logs_list(params = {}) ⇒ Object
Returns a list of a user’s sleep log entries before or after a given date, and specifying offset, limit and sort order.
- #sleep_time_series(resource, opts = {}) ⇒ Object
- #strip_root_key(object) ⇒ Object
-
#subscriptions(collection_path = nil) ⇒ Object
Retrieves a list of subscriptions created by your application for a specific user.
- #to_camel_case(word, opts = {}) ⇒ Object
- #to_snake_case(word, opts = {}) ⇒ Object
-
#update_alarm(tracker_id, alarm_id, body = {}) ⇒ Object
Updates the alarm entry with a given ID for a given device.
-
#update_body_fat_goal(body) ⇒ Object
Creates or updates a user’s body fat goal.
-
#update_daily_activity_goals(body = {}) ⇒ Object
Creates or updates a user’s daily activity goals and returns a response using units in the unit system which corresponds to the Accept-Language header provided.
-
#update_food_goals(body) ⇒ Object
Creates or updates a user’s daily calorie consumption or food plan goals.
-
#update_food_log(food_log_id, body) ⇒ Object
Updates the quantity or calories consumed for a user’s food log entry with the given Food Log ID.
-
#update_meal(meal_id, body) ⇒ Object
Updates an existing meal with the contents of the request.
-
#update_profile(body) ⇒ Object
Modifies a user’s profile data.
-
#update_sleep_goal(body) ⇒ Object
Create or update a user’s sleep goal.
-
#update_water_goal(body) ⇒ Object
Creates or updates a user’s daily water consumption goal.
-
#update_water_log(water_log_id, body) ⇒ Object
Updates the quantity consumed for a user’s water log entry with the given log ID.
-
#update_weekly_activity_goals(body = {}) ⇒ Object
Creates or updates a user’s weekly activity goals and returns a response using units in the unit system which corresponds to the Accept-Language header provided.
-
#update_weight_goal(body) ⇒ Object
Creates or updates a user’s weight goal.
-
#water_goal ⇒ Object
Retrieves a user’s daily water consumption goal.
-
#water_logs(date = Date.today) ⇒ Object
Retrieves a summary and list of a user’s water log entries for a given day.
-
#weekly_activity_goals ⇒ Object
Retrieves a user’s current weekly activity goals.
-
#weight_goal ⇒ Object
Retrieves a user’s current weight goal.
- #weight_logs(date = Date.today) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Client
Returns a new instance of Client.
31 32 33 34 35 36 |
# File 'lib/fitbit_api/client.rb', line 31 def initialize(opts = {}) validate_args(opts) assign_attrs(opts) set_client establish_token(opts) end |
Instance Attribute Details
#api_version ⇒ Object
Returns the value of attribute api_version.
27 28 29 |
# File 'lib/fitbit_api/client.rb', line 27 def api_version @api_version end |
#auto_refresh_token ⇒ Object
Returns the value of attribute auto_refresh_token.
27 28 29 |
# File 'lib/fitbit_api/client.rb', line 27 def auto_refresh_token @auto_refresh_token end |
#locale ⇒ Object
Returns the value of attribute locale.
27 28 29 |
# File 'lib/fitbit_api/client.rb', line 27 def locale @locale end |
#on_token_refresh ⇒ Object
Returns the value of attribute on_token_refresh.
27 28 29 |
# File 'lib/fitbit_api/client.rb', line 27 def on_token_refresh @on_token_refresh end |
#scope ⇒ Object
Returns the value of attribute scope.
27 28 29 |
# File 'lib/fitbit_api/client.rb', line 27 def scope @scope end |
#snake_case_keys ⇒ Object
Returns the value of attribute snake_case_keys.
27 28 29 |
# File 'lib/fitbit_api/client.rb', line 27 def snake_case_keys @snake_case_keys end |
#symbolize_keys ⇒ Object
Returns the value of attribute symbolize_keys.
27 28 29 |
# File 'lib/fitbit_api/client.rb', line 27 def symbolize_keys @symbolize_keys end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
29 30 31 |
# File 'lib/fitbit_api/client.rb', line 29 def token @token end |
#unit_system ⇒ Object
Returns the value of attribute unit_system.
27 28 29 |
# File 'lib/fitbit_api/client.rb', line 27 def unit_system @unit_system end |
#user_id ⇒ Object (readonly)
Returns the value of attribute user_id.
29 30 31 |
# File 'lib/fitbit_api/client.rb', line 29 def user_id @user_id end |
Instance Method Details
#active_zone_minutes_intraday_time_series(opts = {}) ⇒ Object
Retrieves the Active Zone Minutes (AZM) intraday time series data for a specific date or 24 hour period.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/fitbit_api/active_zone_minutes.rb', line 46 def active_zone_minutes_intraday_time_series(opts = {}) date = opts[:date] || Date.today detail_level = opts[:detail_level] start_time = opts[:start_time] end_time = opts[:end_time] if [date, detail_level].any?(&:nil?) raise FitbitAPI::InvalidArgumentError, 'A date and detail_level are required.' end unless %(1min 5min 15min).include? detail_level raise FitbitAPI::InvalidArgumentError, "Invalid detail_level: \"#{detail_level}\". Please provide one of the following: \"1min\", \"5min\" or \"15min\"." end if (start_time || end_time) && !(start_time && end_time) raise FitbitAPI::InvalidArgumentError, 'Both start_time and end_time are required if time is being specified.' end path = "user/#{user_id}/activities/active-zone-minutes/date/#{format_date(date)}/1d/#{detail_level}" path += "/time/#{format_time(start_time)}/#{format_time(end_time)}" if start_time && end_time get("#{path}.json") end |
#active_zone_minutes_time_series(opts = {}) ⇒ Object
Returns the daily summary Active Zone Minutes (AZM) values over a specified date range or period.
active_zone_minutes_time_series(start_date: Date.parse('2021-04-16'), period: '7d')
active_zone_minutes_time_series(start_date: Date.parse('2021-05-18'), end_date: Date.parse('2021-05-24'))
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/fitbit_api/active_zone_minutes.rb', line 16 def active_zone_minutes_time_series(opts = {}) start_date = opts[:start_date] end_date = opts[:end_date] || Date.today period = opts[:period] raise FitbitAPI::InvalidArgumentError, 'A start_date or period is required.' if [period, start_date].none? if period && !PERIODS.include?(period) raise FitbitAPI::InvalidArgumentError, "Invalid period: \"#{period}\". Please provide one of the following: #{PERIODS}." end result = if period get("user/#{user_id}/activities/active-zone-minutes/date/#{format_date(end_date)}/#{period}.json") else get("user/#{user_id}/activities/active-zone-minutes/date/#{format_date(start_date)}/#{format_date(end_date)}.json") end strip_root_key(result) end |
#activity(activity_id) ⇒ Object
Returns the details of a specific activity in the Fitbit activities database in the format requested. If activity has levels, also returns a list of activity level details.
73 74 75 |
# File 'lib/fitbit_api/activities.rb', line 73 def activity(activity_id) get("activities/#{activity_id}.json") end |
#activity_intraday_time_series(resource, opts = {}) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/fitbit_api/activities.rb', line 113 def activity_intraday_time_series(resource, opts = {}) date = opts[:date] || Date.today detail_level = opts[:detail_level] start_time = opts[:start_time] end_time = opts[:end_time] unless ACTIVITY_INTRADAY_RESOURCES.include?(resource) raise FitbitAPI::InvalidArgumentError, "Invalid resource: \"#{resource}\". Please provide one of the following: #{ACTIVITY_RESOURCES}." end if [date, detail_level].any?(&:nil?) raise FitbitAPI::InvalidArgumentError, 'A date and detail_level are required.' end unless %(1min 15min).include? detail_level raise FitbitAPI::InvalidArgumentError, "Invalid detail_level: \"#{detail_level}\". Please provide one of the following: \"1min\" or \"15min\"." end if (start_time || end_time) && !(start_time && end_time) raise FitbitAPI::InvalidArgumentError, 'Both start_time and end_time are required if time is being specified.' end path = "user/#{user_id}/activities/#{resource}/date/#{format_date(date)}/1d/#{detail_level}" path += "/time/#{format_time(start_time)}/#{format_time(end_time)}" if start_time && end_time get("#{path}.json") end |
#activity_logs_list(params = {}) ⇒ Object
Retrieves a list of a user’s activity log entries before or after a given day with offset and limit using units in the unit system which corresponds to the Accept-Language header provided.
activity_logs_list(before_date: Date.parse('2021-05-24'), limit: 5)
63 64 65 66 |
# File 'lib/fitbit_api/activities.rb', line 63 def activity_logs_list(params = {}) default_params = { before_date: Date.today, after_date: nil, sort: 'desc', limit: 20, offset: 0 } get("user/#{user_id}/activities/list.json", default_params.merge(params)) end |
#activity_time_series(resource, opts = {}) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/fitbit_api/activities.rb', line 86 def activity_time_series(resource, opts = {}) start_date = opts[:start_date] end_date = opts[:end_date] || Date.today period = opts[:period] unless ACTIVITY_RESOURCES.include?(resource) raise FitbitAPI::InvalidArgumentError, "Invalid resource: \"#{resource}\". Please provide one of the following: #{ACTIVITY_RESOURCES}." end raise FitbitAPI::InvalidArgumentError, 'A start_date or period is required.' if [start_date, period].none? if period && !PERIODS.include?(period) raise FitbitAPI::InvalidArgumentError, "Invalid period: \"#{period}\". Please provide one of the following: #{PERIODS}." end path = if period "user/#{user_id}/activities/#{resource}/date/#{format_date(end_date)}/#{period}.json" else "user/#{user_id}/activities/#{resource}/date/#{format_date(start_date)}/#{format_date(end_date)}.json" end result = get(path) strip_root_key(result) end |
#add_alarm(tracker_id, body = {}) ⇒ Object
Adds the alarm settings to a given ID for a given device.
add_alarm(123, time: "07:15-08:00", recurring: true, week_days: "MONDAY,FRIDAY,SATURDAY")
25 26 27 |
# File 'lib/fitbit_api/alarms.rb', line 25 def add_alarm(tracker_id, body = {}) post("user/#{user_id}/devices/tracker/#{tracker_id}/alarms.json", body) end |
#add_favorite_activity(activity_id) ⇒ Object
Adds the activity with the given ID to user’s list of favorite activities.
167 168 169 |
# File 'lib/fitbit_api/activities.rb', line 167 def add_favorite_activity(activity_id) post("user/#{user_id}/activities/favorite/#{activity_id}.json") end |
#add_favorite_food(food_id) ⇒ Object
Adds a food with the given ID to the user’s list of favorite foods
74 75 76 |
# File 'lib/fitbit_api/food.rb', line 74 def add_favorite_food(food_id) post("user/#{user_id}/foods/log/favorite/#{food_id}.json") end |
#alarms(tracker_id) ⇒ Object
Returns a list of the set alarms connected to a user’s account.
9 10 11 |
# File 'lib/fitbit_api/alarms.rb', line 9 def alarms(tracker_id) get("user/#{user_id}/devices/tracker/#{tracker_id}/alarms.json") end |
#all_activities ⇒ Object
Gets a list of all valid Fitbit public activities from the activities catalog as well as private custom activities the user created.
45 46 47 |
# File 'lib/fitbit_api/activities.rb', line 45 def all_activities get('activities.json') end |
#auth_url ⇒ Object
Returns the authorize endpoint URL of the OAuth2 provider.
40 41 42 |
# File 'lib/fitbit_api/client.rb', line 40 def auth_url @client.auth_code.(redirect_uri: @redirect_uri, scope: format_scope(@scope)) end |
#badges ⇒ Object
Retrieves a list of the user’s badges.
13 14 15 |
# File 'lib/fitbit_api/user.rb', line 13 def badges get("user/#{user_id}/badges.json") end |
#body_fat_goal ⇒ Object
Retrieves a user’s current body fat percentage goal.
13 14 15 |
# File 'lib/fitbit_api/goals.rb', line 13 def body_fat_goal get("user/#{user_id}/body/log/fat/goal.json") end |
#body_fat_logs(date = Date.today) ⇒ Object
11 12 13 |
# File 'lib/fitbit_api/body.rb', line 11 def body_fat_logs(date = Date.today) get("user/#{user_id}/body/log/fat/date/#{format_date(date)}.json") end |
#body_time_series(resource, opts = {}) ⇒ Object
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 |
# File 'lib/fitbit_api/body.rb', line 15 def body_time_series(resource, opts = {}) start_date = opts[:start_date] end_date = opts[:end_date] || Date.today period = opts[:period] unless BODY_RESOURCES.include?(resource) raise FitbitAPI::InvalidArgumentError, "Invalid resource: \"#{resource}\". Please provide one of the following: #{BODY_RESOURCES}." end raise FitbitAPI::InvalidArgumentError, 'A start_date or period is required.' if [period, start_date].none? if period && !PERIODS.include?(period) raise FitbitAPI::InvalidArgumentError, "Invalid period: \"#{period}\". Please provide one of the following: #{PERIODS}." end result = if period get("user/#{user_id}/body/#{resource}/date/#{format_date(end_date)}/#{period}.json") else get("user/#{user_id}/body/#{resource}/date/#{format_date(start_date)}/#{format_date(end_date)}.json") end strip_root_key(result) end |
#breathing_rate_intraday(opts = {}) ⇒ Object
Returns the intraday breathing rate data for a given date or date range. If both a date and a date range are given, the date range takes precedence.
breathing_rate_intraday(date: Date.parse('2021-04-16'))
breathing_rate_intraday(start_date: Date.parse('2021-05-18'), end_date: Date.parse('2021-05-24'))
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/fitbit_api/breathing_rate.rb', line 47 def breathing_rate_intraday(opts = {}) date = opts[:date] || Date.today start_date = opts[:start_date] end_date = opts[:end_date] end_date = Date.today if start_date && !end_date raise FitbitAPI::InvalidArgumentError, 'A date or start_date and end_date are required.' unless date || start_date result = if start_date get("user/#{user_id}/br/date/#{format_date(start_date)}/#{format_date(end_date)}/all.json") else get("user/#{user_id}/br/date/#{format_date(date)}/all.json") end strip_root_key(result) end |
#breathing_rate_summary(opts = {}) ⇒ Object
Returns the average breathing rate data for a given date or date range. If both a date and a date range are given, the date range takes precedence.
breathing_rate_summary(date: Date.parse('2021-04-16'))
breathing_rate_summary(start_date: Date.parse('2021-05-18'), end_date: Date.parse('2021-05-24'))
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/fitbit_api/breathing_rate.rb', line 17 def breathing_rate_summary(opts = {}) date = opts[:date] || Date.today start_date = opts[:start_date] end_date = opts[:end_date] end_date = Date.today if start_date && !end_date raise FitbitAPI::InvalidArgumentError, 'A date or start_date and end_date are required.' unless date || start_date result = if start_date get("user/#{user_id}/br/date/#{format_date(start_date)}/#{format_date(end_date)}.json") else get("user/#{user_id}/br/date/#{format_date(date)}.json") end strip_root_key(result) end |
#cardio_score_summary(opts = {}) ⇒ Object
Returns the cardio fitness score data for a given date or date range. If both a date and a date range are given, the date range takes precedence.
cardio_score_summary(date: Date.parse('2021-04-16'))
cardio_score_summary(start_date: Date.parse('2021-05-18'), end_date: Date.parse('2021-05-24'))
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/fitbit_api/cardio_score.rb', line 17 def cardio_score_summary(opts = {}) date = opts[:date] || Date.today start_date = opts[:start_date] end_date = opts[:end_date] end_date = Date.today if start_date && !end_date raise FitbitAPI::InvalidArgumentError, 'A date or start_date and end_date are required.' unless date || start_date result = if start_date get("user/#{user_id}/cardioscore/date/#{format_date(start_date)}/#{format_date(end_date)}.json") else get("user/#{user_id}/cardioscore/date/#{format_date(date)}.json") end strip_root_key(result) end |
#core_temperature_summary(opts = {}) ⇒ Object
Returns the core temperature data for a given date or date range. If both a date and a date range are given, the date range takes precedence.
core_temperature_summary(date: Date.parse('2021-04-16'))
core_temperature_summary(start_date: Date.parse('2021-05-18'), end_date: Date.parse('2021-05-24'))
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/fitbit_api/temperature.rb', line 17 def core_temperature_summary(opts = {}) date = opts[:date] || Date.today start_date = opts[:start_date] end_date = opts[:end_date] end_date = Date.today if start_date && !end_date raise FitbitAPI::InvalidArgumentError, 'A date or start_date and end_date are required.' unless date || start_date result = if start_date get("user/#{user_id}/temp/core/date/#{format_date(start_date)}/#{format_date(end_date)}.json") else get("user/#{user_id}/temp/core/date/#{format_date(date)}.json") end strip_root_key(result) end |
#create_food(body) ⇒ Object
Creates a new private food for a user
35 36 37 |
# File 'lib/fitbit_api/food.rb', line 35 def create_food(body) post("user/#{user_id}/foods.json", body) end |
#create_food_log(body) ⇒ Object
Creates a food log entry
51 52 53 |
# File 'lib/fitbit_api/food.rb', line 51 def create_food_log(body) post("user/#{user_id}/foods/log.json", body) end |
#create_meal(body) ⇒ Object
Creates a meal with the given food
23 24 25 |
# File 'lib/fitbit_api/meals.rb', line 23 def create_meal(body) post("user/#{user_id}/meals.json", body) end |
#create_sleep_log(body) ⇒ Object
Creates a log entry for a sleep event
59 60 61 |
# File 'lib/fitbit_api/sleep.rb', line 59 def create_sleep_log(body) post("user/#{user_id}/sleep.json", body) end |
#create_subscription(subscription_id, collection_path = nil) ⇒ Object
Creates a subscription to notify the application when a user has new data available.
20 21 22 |
# File 'lib/fitbit_api/subscriptions.rb', line 20 def create_subscription(subscription_id, collection_path = nil) post("#{subscriptions_path(collection_path)}/#{subscription_id}.json") end |
#daily_activity_goals ⇒ Object
Retrieves a user’s current daily activity goals.
19 20 21 |
# File 'lib/fitbit_api/goals.rb', line 19 def daily_activity_goals get("user/#{user_id}/activities/goals/daily.json") end |
#daily_activity_summary(date = Date.today) ⇒ Object
Retrieves a summary and list of a user’s activities and activity log entries for a given day.
19 20 21 |
# File 'lib/fitbit_api/activities.rb', line 19 def daily_activity_summary(date = Date.today) get("user/#{user_id}/activities/date/#{format_date(date)}.json") end |
#deep_keys_to_camel_case!(object) ⇒ Object
53 54 55 |
# File 'lib/fitbit_api/helpers/utils.rb', line 53 def deep_keys_to_camel_case!(object) deep_transform_keys!(object) { |key| to_camel_case(key, lower: true) } end |
#deep_keys_to_snake_case!(object) ⇒ Object
49 50 51 |
# File 'lib/fitbit_api/helpers/utils.rb', line 49 def deep_keys_to_snake_case!(object) deep_transform_keys!(object) { |key| to_snake_case(key, replace_dashes: true) } end |
#deep_symbolize_keys!(object) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/fitbit_api/helpers/utils.rb', line 57 def deep_symbolize_keys!(object) deep_transform_keys!(object) do |key| key.to_sym rescue StandardError key end end |
#deep_transform_keys!(object, &block) ⇒ Object
Inspired by ActiveSupport’s implementation
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/fitbit_api/helpers/utils.rb', line 66 def deep_transform_keys!(object, &block) case object when Hash keys = object.keys keys.each do |key| value = object.delete(key) object[yield(key)] = deep_transform_keys!(value, &block) end object when Array object.map! { |e| deep_transform_keys!(e, &block) } else object end end |
#delete(path, params = {}, opts = {}, &block) ⇒ Object
Performs an authorized DELETE request to the configured API namespace.
104 105 106 |
# File 'lib/fitbit_api/client.rb', line 104 def delete(path, params = {}, opts = {}, &block) request(:delete, path, opts.merge(params: params), &block) end |
#delete_activity(activity_log_id) ⇒ Object
Deletes a user’s activity log entry with the given ID.
175 176 177 |
# File 'lib/fitbit_api/activities.rb', line 175 def delete_activity(activity_log_id) delete("user/#{user_id}/activities/#{activity_log_id}.json") end |
#delete_alarm(tracker_id, alarm_id) ⇒ Object
Deletes the user’s device alarm entry with the given ID for a given device.
delete_alarm(123, 987)
57 58 59 |
# File 'lib/fitbit_api/alarms.rb', line 57 def delete_alarm(tracker_id, alarm_id) delete("user/#{user_id}/devices/tracker/#{tracker_id}/alarms/#{alarm_id}.json") end |
#delete_body_fat_log(body_fat_log_id) ⇒ Object
53 54 55 |
# File 'lib/fitbit_api/body.rb', line 53 def delete_body_fat_log(body_fat_log_id) delete("user/#{user_id}/body/log/fat/#{body_fat_log_id}.json") end |
#delete_favorite_activity(activity_id) ⇒ Object
Removes the activity with the given ID from a user’s list of favorite activities.
183 184 185 |
# File 'lib/fitbit_api/activities.rb', line 183 def delete_favorite_activity(activity_id) delete("user/#{user_id}/activities/favorite/#{activity_id}.json") end |
#delete_favorite_food(food_id) ⇒ Object
Deletes a food with the given ID from the user’s list of favorite foods
82 83 84 |
# File 'lib/fitbit_api/food.rb', line 82 def delete_favorite_food(food_id) delete("user/#{user_id}/foods/log/favorite/#{food_id}.json") end |
#delete_food(food_id) ⇒ Object
Deletes a custom food created by the user
43 44 45 |
# File 'lib/fitbit_api/food.rb', line 43 def delete_food(food_id) delete("user/#{user_id}/foods/#{food_id}.json") end |
#delete_food_log(food_log_id) ⇒ Object
Deletes a user’s food log entry using the given log ID
68 69 70 |
# File 'lib/fitbit_api/food.rb', line 68 def delete_food_log(food_log_id) delete("user/#{user_id}/foods/log/#{food_log_id}.json") end |
#delete_meal(meal_id) ⇒ Object
Deletes an existing meal of the given meal ID
40 41 42 |
# File 'lib/fitbit_api/meals.rb', line 40 def delete_meal(meal_id) delete("user/#{user_id}/meals/#{meal_id}.json") end |
#delete_sleep_log(sleep_log_id) ⇒ Object
Deletes a sleep log with the given log ID
64 65 66 |
# File 'lib/fitbit_api/sleep.rb', line 64 def delete_sleep_log(sleep_log_id) delete("user/#{user_id}/sleep/#{sleep_log_id}.json") end |
#delete_subscription(subscription_id, collection_path = nil) ⇒ Object
Deletes a subscription for a specific user.
29 30 31 |
# File 'lib/fitbit_api/subscriptions.rb', line 29 def delete_subscription(subscription_id, collection_path = nil) delete("#{subscriptions_path(collection_path)}/#{subscription_id}.json") end |
#delete_water_log(water_log_id) ⇒ Object
Deleted a user’s water log entry using the given log ID
34 35 36 |
# File 'lib/fitbit_api/water.rb', line 34 def delete_water_log(water_log_id) delete("user/#{user_id}/foods/log/water/#{water_log_id}.json") end |
#delete_weight_log(weight_log_id) ⇒ Object
45 46 47 |
# File 'lib/fitbit_api/body.rb', line 45 def delete_weight_log(weight_log_id) delete("user/#{user_id}/body/log/weight/#{weight_log_id}.json") end |
#devices ⇒ Object
Retrieves a list of Fitbit devices paired to a user’s account.
7 8 9 |
# File 'lib/fitbit_api/devices.rb', line 7 def devices get("user/#{user_id}/devices.json") end |
#ecg_logs_list(params = {}) ⇒ Object
This endpoint retrieves a list of the user’s Electrocardiogram (ECG) log entries before or after a given day.
ecg_logs_list(before_date: Date.parse('2021-05-24'), limit: 5)
18 19 20 21 |
# File 'lib/fitbit_api/electrocardiogram.rb', line 18 def ecg_logs_list(params = {}) default_params = { before_date: Date.today, after_date: nil, sort: 'desc', limit: 10, offset: 0 } get("user/#{user_id}/ecg/list.json", default_params.merge(params)) end |
#favorite_activities ⇒ Object
Returns a list of a user’s favorite activities.
38 39 40 |
# File 'lib/fitbit_api/activities.rb', line 38 def favorite_activities get("user/#{user_id}/activities/favorite.json") end |
#favorite_foods ⇒ Object
27 28 29 |
# File 'lib/fitbit_api/food.rb', line 27 def favorite_foods get("user/#{user_id}/foods/log/favorite.json") end |
#food_goals ⇒ Object
Retrieves the user’s current daily calorie consumption goal and/or food plan.
37 38 39 |
# File 'lib/fitbit_api/goals.rb', line 37 def food_goals get("user/#{user_id}/foods/log/goal.json") end |
#food_locales ⇒ Object
Retrieves the food locales used to search, log or create food
115 116 117 |
# File 'lib/fitbit_api/food.rb', line 115 def food_locales get('foods/locales.json') end |
#food_logs(date = Date.today) ⇒ Object
7 8 9 |
# File 'lib/fitbit_api/food.rb', line 7 def food_logs(date = Date.today) get("user/#{user_id}/foods/log/date/#{format_date(date)}.json") end |
#food_time_series(resource, opts = {}) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/fitbit_api/food.rb', line 86 def food_time_series(resource, opts = {}) start_date = opts[:start_date] end_date = opts[:end_date] || Date.today period = opts[:period] unless FOOD_RESOURCES.include?(resource) raise FitbitAPI::InvalidArgumentError, "Invalid resource: \"#{resource}\". Please provide one of the following: #{FOOD_RESOURCES}." end raise FitbitAPI::InvalidArgumentError, 'A start_date or period is required.' if [period, start_date].none? if period && !PERIODS.include?(period) raise FitbitAPI::InvalidArgumentError, "Invalid period: \"#{period}\". Please provide one of the following: #{PERIODS}." end path = if period "user/#{user_id}/foods/log/#{resource}/date/#{format_date(end_date)}/#{period}.json" else "user/#{user_id}/foods/log/#{resource}/date/#{format_date(start_date)}/#{format_date(end_date)}.json" end result = get(path) strip_root_key(result) end |
#food_units ⇒ Object
Retrieves a list of all valid Fitbit food units
121 122 123 |
# File 'lib/fitbit_api/food.rb', line 121 def food_units get('foods/units.json') end |
#format_date(date) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/fitbit_api/helpers/utils.rb', line 7 def format_date(date) if [Date, Time, DateTime].include?(date.class) date.strftime('%Y-%m-%d') elsif date.is_a? String if date =~ /\d{4}-\d{2}-\d{2}/ date else raise FitbitAPI::InvalidArgumentError, "Invalid argument [\"#{date}\"] - string must follow yyyy-MM-dd format." end else raise FitbitAPI::InvalidArgumentError, "Invalid type [#{date.class}] - provide a Date/Time/DateTime or a String(yyyy-MM-dd format)." end end |
#format_scope(scope) ⇒ Object
39 40 41 |
# File 'lib/fitbit_api/helpers/utils.rb', line 39 def format_scope(scope) scope.is_a?(Array) ? scope.join(' ') : scope end |
#format_time(time) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/fitbit_api/helpers/utils.rb', line 23 def format_time(time) if [Time, DateTime].include?(time.class) time.strftime('%H:%M') elsif time.is_a? String unless time =~ /\d{2}:\d{2}/ raise FitbitAPI::InvalidArgumentError, "Invalid argument [\"#{time}\"] - string must follow HH:mm format." end time else raise FitbitAPI::InvalidArgumentError, "Invalid type [#{time.class}] - provide a Time/DateTime or a String(HH:mm format)." end end |
#frequent_activities ⇒ Object
Retrieves a list of a user’s frequent activities.
25 26 27 |
# File 'lib/fitbit_api/activities.rb', line 25 def frequent_activities get("user/#{user_id}/activities/frequent.json") end |
#frequent_foods ⇒ Object
23 24 25 |
# File 'lib/fitbit_api/food.rb', line 23 def frequent_foods get("user/#{user_id}/foods/log/frequent.json") end |
#friends ⇒ Object
Retrieves a list of the Fitbit user’s friends.
7 8 9 |
# File 'lib/fitbit_api/friends.rb', line 7 def friends get("user/#{user_id}/friends.json") end |
#friends_leaderboard ⇒ Object
Retrieves the user’s friends leaderboard.
13 14 15 |
# File 'lib/fitbit_api/friends.rb', line 13 def friends_leaderboard get("user/#{user_id}/friends/leaderboard.json") end |
#get(path, params = {}, opts = {}, &block) ⇒ Object
Performs an authorized GET request to the configured API namespace.
84 85 86 |
# File 'lib/fitbit_api/client.rb', line 84 def get(path, params = {}, opts = {}, &block) request(:get, path, opts.merge(params: params), &block) end |
#get_token(auth_code) ⇒ Object
Returns an OAuth2::AccessToken instance obtained from the given authorization code.
48 49 50 51 52 53 54 55 56 |
# File 'lib/fitbit_api/client.rb', line 48 def get_token(auth_code) @token = @client.auth_code.get_token( auth_code, redirect_uri: @redirect_uri, headers: auth_headers ) @user_id = @token.params['user_id'] @token end |
#heart_rate_intraday_time_series(opts = {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/fitbit_api/heart_rate.rb', line 26 def heart_rate_intraday_time_series(opts = {}) date = opts[:date] || Date.today detail_level = opts[:detail_level] start_time = opts[:start_time] end_time = opts[:end_time] if [date, detail_level].any?(&:nil?) raise FitbitAPI::InvalidArgumentError, 'A date and detail_level are required.' end unless %(1sec 1min).include? detail_level raise FitbitAPI::InvalidArgumentError, "Invalid detail_level: \"#{detail_level}\". Please provide one of the following: \"1sec\" or \"1min\"." end if (start_time || end_time) && !(start_time && end_time) raise FitbitAPI::InvalidArgumentError, 'Both start_time and end_time are required if time is being specified.' end path = "user/#{user_id}/activities/heart/date/#{format_date(date)}/1d/#{detail_level}" path += "/time/#{format_time(start_time)}/#{format_time(end_time)}" if start_time && end_time get("#{path}.json") end |
#heart_rate_time_series(opts = {}) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/fitbit_api/heart_rate.rb', line 5 def heart_rate_time_series(opts = {}) start_date = opts[:start_date] end_date = opts[:end_date] || Date.today period = opts[:period] raise FitbitAPI::InvalidArgumentError, 'A start_date or period is required.' if [period, start_date].none? if period && !PERIODS.include?(period) raise FitbitAPI::InvalidArgumentError, "Invalid period: \"#{period}\". Please provide one of the following: #{PERIODS}." end result = if period get("user/#{user_id}/activities/heart/date/#{format_date(end_date)}/#{period}.json") else get("user/#{user_id}/activities/heart/date/#{format_date(start_date)}/#{format_date(end_date)}.json") end strip_root_key(result) end |
#heart_rate_variability_intraday(opts = {}) ⇒ Object
Returns the heart rate variability intraday data for a given date or date range. If both a date and a date range are given, the date range takes precedence.
heart_rate_variability_intraday(date: Date.parse('2021-04-16'))
heart_rate_variability_intraday(start_date: Date.parse('2021-05-18'), end_date: Date.parse('2021-05-24'))
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/fitbit_api/heart_rate_variability.rb', line 47 def heart_rate_variability_intraday(opts = {}) date = opts[:date] || Date.today start_date = opts[:start_date] end_date = opts[:end_date] end_date = Date.today if start_date && !end_date raise FitbitAPI::InvalidArgumentError, 'A date or start_date and end_date are required.' unless date || start_date result = if start_date get("user/#{user_id}/hrv/date/#{format_date(start_date)}/#{format_date(end_date)}/all.json") else get("user/#{user_id}/hrv/date/#{format_date(date)}/all.json") end strip_root_key(result) end |
#heart_rate_variability_summary(opts = {}) ⇒ Object
Returns the heart rate variability data for a given date or date range. If both a date and a date range are given, the date range takes precedence.
heart_rate_variability_summary(date: Date.parse('2021-04-16'))
heart_rate_variability_summary(start_date: Date.parse('2021-05-18'), end_date: Date.parse('2021-05-24'))
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/fitbit_api/heart_rate_variability.rb', line 17 def heart_rate_variability_summary(opts = {}) date = opts[:date] || Date.today start_date = opts[:start_date] end_date = opts[:end_date] end_date = Date.today if start_date && !end_date raise FitbitAPI::InvalidArgumentError, 'A date or start_date and end_date are required.' unless date || start_date result = if start_date get("user/#{user_id}/hrv/date/#{format_date(start_date)}/#{format_date(end_date)}.json") else get("user/#{user_id}/hrv/date/#{format_date(date)}.json") end strip_root_key(result) end |
#lifetime_stats ⇒ Object
Retrieves the user’s activity statistics in the format requested using units in the unit system which corresponds to the Accept-Language header provided. Activity statistics includes Lifetime and Best achievement values from the My Achievements tile on the website dashboard.
82 83 84 |
# File 'lib/fitbit_api/activities.rb', line 82 def lifetime_stats get("user/#{user_id}/activities.json") end |
#log_activity(body) ⇒ Object
Creates log entry for an activity or user’s private custom activity using units in the unit system which corresponds to the Accept-Language header provided.
log_activity(activity_id: 90013, manual_calories: 300, duration_millis: 6000000)
159 160 161 |
# File 'lib/fitbit_api/activities.rb', line 159 def log_activity(body) post("user/#{user_id}/activities.json", body) end |
#log_body_fat(body) ⇒ Object
49 50 51 |
# File 'lib/fitbit_api/body.rb', line 49 def log_body_fat(body) post("user/#{user_id}/body/log/fat.json", body) end |
#log_water(body) ⇒ Object
Create a user’s water log entry
17 18 19 |
# File 'lib/fitbit_api/water.rb', line 17 def log_water(body) post("user/#{user_id}/foods/log/water.json", body) end |
#log_weight(body) ⇒ Object
41 42 43 |
# File 'lib/fitbit_api/body.rb', line 41 def log_weight(body) post("user/#{user_id}/body/log/weight.json", body) end |
#meal(meal_id) ⇒ Object
Retrieves a single meal created by the user from their food log given the meal id
15 16 17 |
# File 'lib/fitbit_api/meals.rb', line 15 def meal(meal_id) get("user/#{user_id}/meals/#{meal_id}.json") end |
#meals ⇒ Object
Retrieves a list of meals created by the user from their food log
7 8 9 |
# File 'lib/fitbit_api/meals.rb', line 7 def meals get("user/#{user_id}/meals.json") end |
#oxygen_saturation_intraday(opts = {}) ⇒ Object
Returns the oxygen saturation intraday data for a given date or date range. If both a date and a date range are given, the date range takes precedence.
oxygen_saturation_intraday(date: Date.parse('2021-04-16'))
oxygen_saturation_intraday(start_date: Date.parse('2021-05-18'), end_date: Date.parse('2021-05-24'))
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/fitbit_api/oxygen_saturation.rb', line 45 def oxygen_saturation_intraday(opts = {}) date = opts[:date] || Date.today start_date = opts[:start_date] end_date = opts[:end_date] end_date = Date.today if start_date && !end_date raise FitbitAPI::InvalidArgumentError, 'A date or start_date and end_date are required.' unless date || start_date if start_date get("user/#{user_id}/spo2/date/#{format_date(start_date)}/#{format_date(end_date)}/all.json") else get("user/#{user_id}/spo2/date/#{format_date(date)}/all.json") end end |
#oxygen_saturation_summary(opts = {}) ⇒ Object
Returns the oxygen saturation summary data for a given date or date range. If both a date and a date range are given, the date range takes precedence.
oxygen_saturation_summary(date: Date.parse('2021-04-16'))
oxygen_saturation_summary(start_date: Date.parse('2021-05-18'), end_date: Date.parse('2021-05-24'))
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/fitbit_api/oxygen_saturation.rb', line 17 def oxygen_saturation_summary(opts = {}) date = opts[:date] || Date.today start_date = opts[:start_date] end_date = opts[:end_date] end_date = Date.today if start_date && !end_date raise FitbitAPI::InvalidArgumentError, 'A date or start_date and end_date are required.' unless date || start_date if start_date get("user/#{user_id}/spo2/date/#{format_date(start_date)}/#{format_date(end_date)}.json") else get("user/#{user_id}/spo2/date/#{format_date(date)}.json") end end |
#post(path, body = {}, opts = {}, &block) ⇒ Object
Performs an authorized POST request to the configured API namespace.
94 95 96 |
# File 'lib/fitbit_api/client.rb', line 94 def post(path, body = {}, opts = {}, &block) request(:post, path, opts.merge(body: body), &block) end |
#profile ⇒ Object
Retrieves the user’s profile data.
7 8 9 |
# File 'lib/fitbit_api/user.rb', line 7 def profile get("user/#{user_id}/profile.json") end |
#recent_activities ⇒ Object
Retrieves a list of a user’s recent activities types logged with some details of the last activity log of that type.
32 33 34 |
# File 'lib/fitbit_api/activities.rb', line 32 def recent_activities get("user/#{user_id}/activities/recent.json") end |
#recent_foods ⇒ Object
19 20 21 |
# File 'lib/fitbit_api/food.rb', line 19 def recent_foods get("user/#{user_id}/foods/log/recent.json") end |
#refresh_token! ⇒ Object
Refreshes the current Access Token.
60 61 62 63 64 65 66 |
# File 'lib/fitbit_api/client.rb', line 60 def refresh_token! @token = @token.refresh!(headers: auth_headers) @user_id ||= @token.params['user_id'] on_token_refresh.call(@token) if on_token_refresh.respond_to?(:call) @token end |
#revoke_token! ⇒ Object
Revokes the user’s authorizations and all associated tokens.
70 71 72 73 74 75 76 |
# File 'lib/fitbit_api/client.rb', line 70 def revoke_token! body = { token: token.token } headers = default_request_headers.merge(auth_headers) response = token.post('oauth2/revoke', { headers: headers, body: body }).response process_keys!(MultiJson.load(response.body)) end |
#search_foods(params) ⇒ Object
Retrieves a list of public foods from the Fitbit foods database and private foods the user created
15 16 17 |
# File 'lib/fitbit_api/food.rb', line 15 def search_foods(params) get('foods/search.json', params) end |
#skin_temperature_summary(opts = {}) ⇒ Object
Returns the skin temperature data for a given date or date range. If both a date and a date range are given, the date range takes precedence.
skin_temperature_summary(date: Date.parse('2021-04-16'))
skin_temperature_summary(start_date: Date.parse('2021-05-18'), end_date: Date.parse('2021-05-24'))
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/fitbit_api/temperature.rb', line 47 def skin_temperature_summary(opts = {}) date = opts[:date] || Date.today start_date = opts[:start_date] end_date = opts[:end_date] end_date = Date.today if start_date && !end_date raise FitbitAPI::InvalidArgumentError, 'A date or start_date and end_date are required.' unless date || start_date result = if start_date get("user/#{user_id}/temp/skin/date/#{format_date(start_date)}/#{format_date(end_date)}.json") else get("user/#{user_id}/temp/skin/date/#{format_date(date)}.json") end strip_root_key(result) end |
#sleep_goal ⇒ Object
Retrieves a user’s current sleep goal.
31 32 33 |
# File 'lib/fitbit_api/goals.rb', line 31 def sleep_goal get("user/#{user_id}/sleep/goal.json") end |
#sleep_logs(date = Date.today) ⇒ Object
Returns a list of a user’s sleep log entries for a given date. The data returned can include sleep periods that began on the previous date. For example, if you request a Sleep Log for 2021-12-22, it may return a log entry that began the previous night on 2021-12-21, but ended on 2021-12-22.
14 15 16 |
# File 'lib/fitbit_api/sleep.rb', line 14 def sleep_logs(date = Date.today) get("user/#{user_id}/sleep/date/#{format_date(date)}.json") end |
#sleep_logs_by_date_range(opts = {}) ⇒ Object
Returns a list of a user’s sleep log entries for a given date range. The data returned for either date can include a sleep period that ended that date but began on the previous date. For example, if you request a Sleep Log between 2021-12-22 and 2021-12-26, it may return log entries that span 2021-12-21 and 2021-12-22, as well as 2021-12-25 and 2021-12-26.
28 29 30 31 32 33 34 35 |
# File 'lib/fitbit_api/sleep.rb', line 28 def sleep_logs_by_date_range(opts = {}) start_date = opts[:start_date] end_date = opts[:end_date] raise FitbitAPI::InvalidArgumentError, 'A start_date and end_date are required.' unless start_date && end_date get("user/#{user_id}/sleep/date/#{format_date(start_date)}/#{format_date(end_date)}.json") end |
#sleep_logs_list(params = {}) ⇒ Object
Returns a list of a user’s sleep log entries before or after a given date, and specifying offset, limit and sort order. The data returned for different dates can include sleep periods that began on the previous date. For example, a sleep log entry for 2018-10-21 may have ended that day but started the previous night on 2018-10-20.
sleep_logs_list(before_date: Date.parse('2021-05-24'), limit: 5)
52 53 54 55 |
# File 'lib/fitbit_api/sleep.rb', line 52 def sleep_logs_list(params = {}) default_params = { before_date: Date.today, after_date: nil, sort: 'desc', limit: 20, offset: 0 } get("user/#{user_id}/sleep/list.json", default_params.merge(params)) end |
#sleep_time_series(resource, opts = {}) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/fitbit_api/sleep.rb', line 68 def sleep_time_series(resource, opts = {}) start_date = opts[:start_date] end_date = opts[:end_date] || Date.today period = opts[:period] unless SLEEP_RESOURCES.include?(resource) raise FitbitAPI::InvalidArgumentError, "Invalid resource: \"#{resource}\". Please provide one of the following: #{SLEEP_RESOURCES}." end raise FitbitAPI::InvalidArgumentError, 'A start_date or period is required.' if [period, start_date].none? if period && !PERIODS.include?(period) raise FitbitAPI::InvalidArgumentError, "Invalid period: \"#{period}\". Please provide one of the following: #{PERIODS}." end result = if period get("user/#{user_id}/sleep/#{resource}/date/#{format_date(end_date)}/#{period}.json") else get("user/#{user_id}/sleep/#{resource}/date/#{format_date(start_date)}/#{format_date(end_date)}.json") end strip_root_key(result) end |
#strip_root_key(object) ⇒ Object
43 44 45 46 47 |
# File 'lib/fitbit_api/helpers/utils.rb', line 43 def strip_root_key(object) return object unless object.is_a?(Hash) && object.keys.length == 1 object.values[0] end |
#subscriptions(collection_path = nil) ⇒ Object
Retrieves a list of subscriptions created by your application for a specific user. You can either fetch subscriptions for a specific collection or the entire list of subscriptions for the user.
11 12 13 |
# File 'lib/fitbit_api/subscriptions.rb', line 11 def subscriptions(collection_path = nil) get("#{subscriptions_path(collection_path)}.json") end |
#to_camel_case(word, opts = {}) ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/fitbit_api/helpers/utils.rb', line 92 def to_camel_case(word, opts = {}) string = word.to_s if string.match(/[A-Z]|[a-z]([A-Z0-9]*[a-z][a-z0-9]*[A-Z]|[a-z0-9]*[A-Z][A-Z0-9]*[a-z])[A-Za-z0-9]*/) return string end string = word.to_s.split('_').collect(&:capitalize).join string.gsub!(/^\w{1}/, &:downcase) if opts[:lower] string end |
#to_snake_case(word, opts = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/fitbit_api/helpers/utils.rb', line 82 def to_snake_case(word, opts = {}) string = word.to_s.dup return string.downcase if string.match(/\A[A-Z]+\z/) string.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2') string.gsub!(/([a-z])([A-Z])/, '\1_\2') string.gsub!('-', '_') if opts[:replace_dashes] string.downcase end |
#update_alarm(tracker_id, alarm_id, body = {}) ⇒ Object
Updates the alarm entry with a given ID for a given device.
update_alarm(123, 987, week_days: "TUESDAY,SUNDAY")
46 47 48 |
# File 'lib/fitbit_api/alarms.rb', line 46 def update_alarm(tracker_id, alarm_id, body = {}) post("user/#{user_id}/devices/tracker/#{tracker_id}/alarms/#{alarm_id}.json", body) end |
#update_body_fat_goal(body) ⇒ Object
Creates or updates a user’s body fat goal.
92 93 94 |
# File 'lib/fitbit_api/goals.rb', line 92 def update_body_fat_goal(body) post("user/#{user_id}/body/log/fat/goal.json", body) end |
#update_daily_activity_goals(body = {}) ⇒ Object
Creates or updates a user’s daily activity goals and returns a response using units in the unit system which corresponds to the Accept-Language header provided.
update_daily_activity_goals(calories_out: 2000, active_minutes: 90, floors: 5)
60 61 62 |
# File 'lib/fitbit_api/goals.rb', line 60 def update_daily_activity_goals(body = {}) post("user/#{user_id}/activities/goals/daily.json", body) end |
#update_food_goals(body) ⇒ Object
Creates or updates a user’s daily calorie consumption or food plan goals.
108 109 110 |
# File 'lib/fitbit_api/goals.rb', line 108 def update_food_goals(body) post("user/#{user_id}/foods/log/goal.json", body) end |
#update_food_log(food_log_id, body) ⇒ Object
Updates the quantity or calories consumed for a user’s food log entry with the given Food Log ID
60 61 62 |
# File 'lib/fitbit_api/food.rb', line 60 def update_food_log(food_log_id, body) post("user/#{user_id}/foods/log/#{food_log_id}.json", body) end |
#update_meal(meal_id, body) ⇒ Object
Updates an existing meal with the contents of the request
32 33 34 |
# File 'lib/fitbit_api/meals.rb', line 32 def update_meal(meal_id, body) post("user/#{user_id}/meals/#{meal_id}.json", body) end |
#update_profile(body) ⇒ Object
Modifies a user’s profile data.
21 22 23 |
# File 'lib/fitbit_api/user.rb', line 21 def update_profile(body) post("user/#{user_id}/profile.json", body) end |
#update_sleep_goal(body) ⇒ Object
Create or update a user’s sleep goal.
100 101 102 |
# File 'lib/fitbit_api/goals.rb', line 100 def update_sleep_goal(body) post("user/#{user_id}/sleep/goal.json", body) end |
#update_water_goal(body) ⇒ Object
Creates or updates a user’s daily water consumption goal.
116 117 118 |
# File 'lib/fitbit_api/goals.rb', line 116 def update_water_goal(body) post("user/#{user_id}/foods/log/water/goal.json", body) end |
#update_water_log(water_log_id, body) ⇒ Object
Updates the quantity consumed for a user’s water log entry with the given log ID
26 27 28 |
# File 'lib/fitbit_api/water.rb', line 26 def update_water_log(water_log_id, body) post("user/#{user_id}/foods/log/water/#{water_log_id}.json", body) end |
#update_weekly_activity_goals(body = {}) ⇒ Object
Creates or updates a user’s weekly activity goals and returns a response using units in the unit system which corresponds to the Accept-Language header provided.
update_weekly_activity_goals(active_minutes: 300, floors: 20)
77 78 79 |
# File 'lib/fitbit_api/goals.rb', line 77 def update_weekly_activity_goals(body = {}) post("user/#{user_id}/activities/goals/weekly.json", body) end |
#update_weight_goal(body) ⇒ Object
Creates or updates a user’s weight goal.
84 85 86 |
# File 'lib/fitbit_api/goals.rb', line 84 def update_weight_goal(body) post("user/#{user_id}/body/log/weight/goal.json", body) end |
#water_goal ⇒ Object
Retrieves a user’s daily water consumption goal.
43 44 45 |
# File 'lib/fitbit_api/goals.rb', line 43 def water_goal get("user/#{user_id}/foods/log/water/goal.json") end |
#water_logs(date = Date.today) ⇒ Object
Retrieves a summary and list of a user’s water log entries for a given day
9 10 11 |
# File 'lib/fitbit_api/water.rb', line 9 def water_logs(date = Date.today) get("user/#{user_id}/foods/log/water/date/#{format_date(date)}.json") end |
#weekly_activity_goals ⇒ Object
Retrieves a user’s current weekly activity goals.
25 26 27 |
# File 'lib/fitbit_api/goals.rb', line 25 def weekly_activity_goals get("user/#{user_id}/activities/goals/weekly.json") end |
#weight_goal ⇒ Object
Retrieves a user’s current weight goal.
7 8 9 |
# File 'lib/fitbit_api/goals.rb', line 7 def weight_goal get("user/#{user_id}/body/log/weight/goal.json") end |
#weight_logs(date = Date.today) ⇒ Object
7 8 9 |
# File 'lib/fitbit_api/body.rb', line 7 def weight_logs(date = Date.today) get("user/#{user_id}/body/log/weight/date/#{format_date(date)}.json") end |