Class: FitbitAPI::Client

Inherits:
Object
  • Object
show all
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,
lib/fitbit_api/irregular_rhythm_notifications.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

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



32
33
34
35
36
37
# File 'lib/fitbit_api/client.rb', line 32

def initialize(opts = {})
  validate_args(opts)
  assign_attrs(opts)
  set_client
  establish_token(opts)
end

Instance Attribute Details

#api_versionObject

Returns the value of attribute api_version.



28
29
30
# File 'lib/fitbit_api/client.rb', line 28

def api_version
  @api_version
end

#auto_refresh_tokenObject

Returns the value of attribute auto_refresh_token.



28
29
30
# File 'lib/fitbit_api/client.rb', line 28

def auto_refresh_token
  @auto_refresh_token
end

#localeObject

Returns the value of attribute locale.



28
29
30
# File 'lib/fitbit_api/client.rb', line 28

def locale
  @locale
end

#on_token_refreshObject

Returns the value of attribute on_token_refresh.



28
29
30
# File 'lib/fitbit_api/client.rb', line 28

def on_token_refresh
  @on_token_refresh
end

#scopeObject

Returns the value of attribute scope.



28
29
30
# File 'lib/fitbit_api/client.rb', line 28

def scope
  @scope
end

#snake_case_keysObject

Returns the value of attribute snake_case_keys.



28
29
30
# File 'lib/fitbit_api/client.rb', line 28

def snake_case_keys
  @snake_case_keys
end

#symbolize_keysObject

Returns the value of attribute symbolize_keys.



28
29
30
# File 'lib/fitbit_api/client.rb', line 28

def symbolize_keys
  @symbolize_keys
end

#tokenObject (readonly)

Returns the value of attribute token.



30
31
32
# File 'lib/fitbit_api/client.rb', line 30

def token
  @token
end

#unit_systemObject

Returns the value of attribute unit_system.



28
29
30
# File 'lib/fitbit_api/client.rb', line 28

def unit_system
  @unit_system
end

#user_idObject (readonly)

Returns the value of attribute user_id.



30
31
32
# File 'lib/fitbit_api/client.rb', line 30

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.

Parameters:

  • opts (Hash) (defaults to: {})

    The request parameters

Options Hash (opts):

  • :date (Date)

    The date for which to retrieve the data

  • :detail_level (String)

    Number of data poins to include

  • :start_time (String)

    The time in the format HH:mm

  • :end_time (String)

    The time in the format HH:mm



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fitbit_api/active_zone_minutes.rb', line 49

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

  detail_levels = %(1min 5min 15min)

  unless detail_levels.include? detail_level
    raise FitbitAPI::InvalidArgumentError,
          "Invalid detail_level: \"#{detail_level}\". Please provide one of the following: #{detail_levels}."
  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'))

Parameters:

  • opts (Hash) (defaults to: {})

    The request parameters

Options Hash (opts):

  • :start_date (Date)

    The start of the date range

  • :end_date (Date)

    The end of the date range

  • :period (String)

    The range for which data will be returned

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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.

Parameters:

  • activity_id (Integer, String)

    The ID of the desired activity to retrieve



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)

Parameters:

  • params (Hash) (defaults to: {})

    The request parameters

Options Hash (params):

  • :before_date (Date)

    Specify when filtering entries that occured before the given date

  • :after_date (Date)

    Specify when filtering entries that occured after the given date

  • :sort (String)

    The Sort order of entries by date (asc or desc)

  • :offset (Integer)

    The offset number of entries. Must always be 0

  • :limit (Integer)

    The max of the number of entries returned (max: 20)



63
64
65
66
# File 'lib/fitbit_api/activities.rb', line 63

def activity_logs_list(params = {})
  default_params = { before_date: Date.today, 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")

Parameters:

  • tracker_id (Integer)

    The ID of the tracker for which the alarm is created

  • body (Hash) (defaults to: {})

    The POST request body

Options Hash (body):

  • :time (String)

    Time of day that the alarm vibrates with a UTC timezone offset, e.g. 07:15-08:00

  • :enabled (Boolean)

    If false, alarm does not vibrate until enabled is set to true

  • :recurring (Boolean)

    If false, the alarm is a single event

  • :week_days (String)

    Comma separated list of days on which the alarm vibrates (e.g. MONDAY,TUESDAY)



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.

Parameters:

  • activity_id (Integer)

    The activity ID



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.

Parameters:

  • tracker_id (Integer)

    The ID of the tracker for which the data is returned



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_activitiesObject

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_urlObject

Returns the authorize endpoint URL of the OAuth2 provider.



41
42
43
# File 'lib/fitbit_api/client.rb', line 41

def auth_url
  @client.auth_code.authorize_url(redirect_uri: @redirect_uri, scope: format_scope(@scope))
end

#badgesObject

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_goalObject

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'))

Parameters:

  • params (Hash)

    The request parameters

Raises:



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'))

Parameters:

  • params (Hash)

    The request parameters

Raises:



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'))

Parameters:

  • params (Hash)

    The request parameters

Raises:



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'))

Parameters:

  • params (Hash)

    The request parameters

Raises:



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

Parameters:

  • body (Hash)

    The POST request body



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

Parameters:

  • body (Hash)

    The POST request body



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

Parameters:

  • body (Hash)

    The POST request body



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.

Parameters:

  • subscription_id (Integer)

    The unique ID of the subscription created by the API client application

  • collection_path (String) (defaults to: nil)

    Collection of data to retrieve notifications



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_goalsObject

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.

Parameters:

  • date (Date) (defaults to: Date.today)

    The date for which to retrieve the activity data.



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

#delete(path, params = {}, opts = {}, &block) ⇒ Object

Performs an authorized DELETE request to the configured API namespace.

Parameters:

  • path (String)

    The request path

  • params (Hash) (defaults to: {})

    The query parameters

  • opts (Hash) (defaults to: {})

    Additional request options (e.g. headers)



106
107
108
# File 'lib/fitbit_api/client.rb', line 106

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.

Parameters:

  • activity_log_id (Integer)

    The ID of the activity log entry



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)

Parameters:

  • tracker_id (Integer)

    The ID of the tracker for which the alarm is to be deleted

  • alarm_id (Integer)

    The ID of the alarm to be deleted



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.

Parameters:

  • activity_id (Integer)

    The ID of the activity to be removed



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

Parameters:

  • food_id (Integer)

    The ID of the food to delete from the user’s favorites



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

Parameters:

  • food_id (Integer)

    The ID of the food to be deleted



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

Parameters:

  • food_log_id (Integer)

    The id of the food log entry



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

Parameters:

  • meal_id (Integer)

    The ID of the meal



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.

Parameters:

  • subscription_id (Integer)

    The unique ID of the subscription created by the API client application

  • collection_path (String) (defaults to: nil)

    Collection of data to retrieve notifications



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

Parameters:

  • water_log_id (Integer)

    The id of the water log entry



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

#devicesObject

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)

Parameters:

  • params (Hash) (defaults to: {})

    The request parameters

Options Hash (params):

  • :before_date (Date)

    Specify when filtering entries that occured before the given date

  • :after_date (Date)

    Specify when filtering entries that occured after the given date

  • :sort (String)

    the Sort order of entries by date (asc or desc)

  • :offset (Integer)

    The offset number of entries. Must always be 0

  • :limit (Integer)

    The max of the number of entries returned (max: 10)



18
19
20
21
# File 'lib/fitbit_api/electrocardiogram.rb', line 18

def ecg_logs_list(params = {})
  default_params = { before_date: Date.today, sort: 'desc', limit: 10, offset: 0 }
  get("user/#{user_id}/ecg/list.json", default_params.merge(params))
end

#favorite_activitiesObject

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_foodsObject



27
28
29
# File 'lib/fitbit_api/food.rb', line 27

def favorite_foods
  get("user/#{user_id}/foods/log/favorite.json")
end

#food_goalsObject

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_localesObject

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_unitsObject

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

#frequent_activitiesObject

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_foodsObject



23
24
25
# File 'lib/fitbit_api/food.rb', line 23

def frequent_foods
  get("user/#{user_id}/foods/log/frequent.json")
end

#friendsObject

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_leaderboardObject

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.

Parameters:

  • path (String)

    The request path

  • params (Hash) (defaults to: {})

    The query parameters

  • opts (Hash) (defaults to: {})

    Additional request options (e.g. headers)



86
87
88
# File 'lib/fitbit_api/client.rb', line 86

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.

Parameters:

  • auth_code (String)

    An authorization code



49
50
51
52
53
54
55
56
57
# File 'lib/fitbit_api/client.rb', line 49

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'))

Parameters:

  • params (Hash)

    The request parameters

Raises:



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'))

Parameters:

  • params (Hash)

    The request parameters

Raises:



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

#irn_alerts_list(params = {}) ⇒ Object

Retrieves a paginated list of Irregular Rhythm Notifications (IRN) alerts, as well as all of the alert tachograms.

irn_alerts_list(before_date: Date.parse('2021-05-24'), limit: 5)

Parameters:

  • params (Hash) (defaults to: {})

    The request parameters

Options Hash (params):

  • :before_date (Date)

    Specify when filtering entries that occured before the given date

  • :after_date (Date)

    Specify when filtering entries that occured after the given date

  • :sort (String)

    The Sort order of entries by date (asc or desc)

  • :offset (Integer)

    The offset number of entries. Must always be 0

  • :limit (Integer)

    The max of the number of entries returned (max: 10)



18
19
20
21
# File 'lib/fitbit_api/irregular_rhythm_notifications.rb', line 18

def irn_alerts_list(params = {})
  default_params = { before_date: Date.today, sort: 'desc', limit: 10, offset: 0 }
  get("user/#{user_id}/irn/alerts/list.json", default_params.merge(params))
end

#irn_profileObject

Retrieves the user state for Irregular Rhythm Notifications (IRN).



25
26
27
# File 'lib/fitbit_api/irregular_rhythm_notifications.rb', line 25

def irn_profile
  get("user/#{user_id}/irn/profile.json")
end

#lifetime_statsObject

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)

Parameters:

  • body (Hash)

    The POST request body

Options Hash (body):

  • :activity_id (Integer, String)

    The activity ID

  • :activity_name (String)

    Custom activity name. Either activity ID or activity_name must be provided

  • :manual_calories (Integer)

    Calories burned, specified manually. Required if activity_name is given

  • :start_time (String)

    Activity start time; formatted in HH:mm:ss

  • :duration_millis (Integer)

    Duration in milliseconds

  • :date (String)

    Log entry date; formatted in yyyy-MM-dd

  • :distance (Integer)

    Distance; required for logging directory activity

  • :distance_unit (String)

    Distance measurement unit



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

Parameters:

  • body (Hash)

    The POST request body for creating the 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

Parameters:

  • meal_id (Integer)

    The ID of the meal



15
16
17
# File 'lib/fitbit_api/meals.rb', line 15

def meal(meal_id)
  get("user/#{user_id}/meals/#{meal_id}.json")
end

#mealsObject

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'))

Parameters:

  • params (Hash)

    The request parameters

Raises:



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'))

Parameters:

  • params (Hash)

    The request parameters

Raises:



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.

Parameters:

  • path (String)

    The request path

  • body (Hash) (defaults to: {})

    The request body

  • opts (Hash) (defaults to: {})

    Additional request options (e.g. headers)



96
97
98
# File 'lib/fitbit_api/client.rb', line 96

def post(path, body = {}, opts = {}, &block)
  request(:post, path, opts.merge(body: body), &block)
end

#profileObject

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_activitiesObject

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_foodsObject



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.



61
62
63
64
65
66
67
# File 'lib/fitbit_api/client.rb', line 61

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.



71
72
73
74
75
76
77
78
# File 'lib/fitbit_api/client.rb', line 71

def revoke_token!
  body = { token: token.token }
  headers = default_request_headers.merge(auth_headers)
  response = token.post('oauth2/revoke', { headers: headers, body: body }).response
  response_body = JSON.parse(response.body) unless response.body.empty?

  process_keys!(response_body)
end

#search_foods(params) ⇒ Object

Retrieves a list of public foods from the Fitbit foods database and private foods the user created

Parameters:

  • query (String)

    The search query



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'))

Parameters:

  • params (Hash)

    The request parameters

Raises:



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_goalObject

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.

Parameters:

  • date (Date, String) (defaults to: Date.today)

    The date for the sleep log to be returned in the format yyyy-MM-dd



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.

Parameters:

  • opts (Hash) (defaults to: {})

    The request options

Options Hash (opts):

  • :start_date (Date)

    The start of the date range

  • :end_date (Date)

    The end of the date range

Raises:



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)

Parameters:

  • params (Hash) (defaults to: {})

    The request parameters

Options Hash (params):

  • :before_date (Date)

    Specify when filtering entries that occured before the given date

  • :after_date (Date)

    Specify when filtering entries that occured after the given date

  • :sort (String)

    the Sort order of entries by date (asc or desc)

  • :offset (Integer)

    The offset number of entries. Must always be 0

  • :limit (Integer)

    The max of the number of entries returned (max: 100)



52
53
54
55
# File 'lib/fitbit_api/sleep.rb', line 52

def sleep_logs_list(params = {})
  default_params = { before_date: Date.today, 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

#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.

Parameters:

  • collection_path (String) (defaults to: nil)

    Collection of data to retrieve notifications



11
12
13
# File 'lib/fitbit_api/subscriptions.rb', line 11

def subscriptions(collection_path = nil)
  get("#{subscriptions_path(collection_path)}.json")
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")

Parameters:

  • tracker_id (Integer)

    The ID of the tracker for which the alarm is created

  • alarm_id (Integer)

    The ID of the alarm to be updated

  • body (Hash) (defaults to: {})

    The POST request body.

Options Hash (body):

  • :time (String)

    Time of day that the alarm vibrates with a UTC timezone offset, e.g. 07:15-08:00

  • :enabled (Boolean)

    If false, alarm does not vibrate until enabled is set to true

  • :recurring (Boolean)

    If false, the alarm is a single event

  • :week_days (String)

    Comma separated list of days on which the alarm vibrates (e.g. MONDAY,TUESDAY)

  • :snooze_length (Integer)

    Minutes between alarms

  • :snooze_count (Integer)

    Maximum snooze count

  • :label (String)

    Label for alarm

  • :vibe (String)

    Vibe pattern; only one value for now (DEFAULT)



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.

Parameters:

  • body (Hash)

    the POST request body



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)

Parameters:

  • body (Hash) (defaults to: {})

    The POST request body

Options Hash (body):

  • :calories_out (Integer)

    Calories output goal value

  • :active_minutes (Integer)

    Active minutes goal value

  • :floors (Integer)

    Floor goal value

  • :distance (Integer, Float)

    Distance goal value

  • :steps (Integer)

    Steps goal value



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.

Parameters:

  • body (Hash)

    the POST request body



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

Parameters:

  • food_log_id (Integer)

    The ID of the food log to edit

  • body (Hash)

    The POST request body



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

Parameters:

  • meal_id (Integer)

    The ID of the meal

  • body (Hash)

    The POST request body



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.

Parameters:

  • body (Hash)

    The POST request body



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.

Parameters:

  • body (Hash)

    the POST request body



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.

Parameters:

  • body (Hash)

    the POST request body



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

Parameters:

  • water_log_id (Integer)

    The ID of the water log to be updated

  • body (Hash)

    The POST request body for updating the water log



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)

Parameters:

  • body (Hash) (defaults to: {})

    The POST request body

Options Hash (body):

  • :calories_out (Integer)

    Calories output goal value

  • :active_minutes (Integer)

    Active minutes goal value

  • :floors (Integer)

    Floor goal value

  • :distance (Integer, Float)

    Distance goal value

  • :steps (Integer)

    Steps goal value



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.

Parameters:

  • body (Hash)

    the POST request body



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_goalObject

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

Parameters:

  • date (Date) (defaults to: Date.today)

    The date for which entries are to be returned



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_goalsObject

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_goalObject

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