Module: FitbitClient::Resources::Sleep

Included in:
FitbitClient::Resources
Defined in:
lib/fitbit_client/resources/sleep.rb

Instance Method Summary collapse

Instance Method Details

#delete_sleep_log(log_id) ⇒ Object

The Delete Sleep Log endpoint deletes a user’s sleep log entry with the given ID.



118
119
120
# File 'lib/fitbit_client/resources/sleep.rb', line 118

def delete_sleep_log(log_id)
  successful_delete?(delete(path_user_version("/sleep/#{log_id}")))
end

#log_sleep(start_time, duration_milliseconds, date, options = {}) ⇒ Object

It requires read & write access



110
111
112
113
114
# File 'lib/fitbit_client/resources/sleep.rb', line 110

def log_sleep(start_time, duration_milliseconds, date, options = {})
  params = { 'date' => iso_date(date), 'startTime' => start_time.strftime('%H:%M'), 'duration' => duration_milliseconds }
  sleep_default_version!(options)
  post_json(path_user_version('/sleep'), params)
end

#sleep_goalsObject

The Get Sleep Goal endpoint returns a user’s current sleep goal using unit in the unit system that corresponds to the Accept-Language header provided in the format requested.



125
126
127
# File 'lib/fitbit_client/resources/sleep.rb', line 125

def sleep_goals
  get_json(path_user_version('/sleep/goal'))
end

#sleep_logs_by_date(date, options = {}) ⇒ Object

The Get Sleep Logs by Date endpoint returns a summary and list of a user’s sleep log entries (including naps) as well as detailed sleep entry data for a given day.

This endpoint supports two kinds of sleep data:

  • stages: Levels data is returned with 30-second granularity. ‘Sleep Stages’ levels include deep, light, rem, and wake.

  • classic: Levels data returned with 60-second granularity. ‘Sleep Pattern’ levels include asleep, restless, and awake.

The response could be a mix of classic and stages sleep logs.

Note: shortData is only included for stages sleep logs and includes wake periods that are 3 minutes or less in duration.

This distinction is to simplify graphically distinguishing short wakes from longer wakes, but they are physiologically equivalent.



45
46
47
48
49
# File 'lib/fitbit_client/resources/sleep.rb', line 45

def sleep_logs_by_date(date, options = {})
  path = "/sleep/date/#{iso_date(date)}"
  sleep_default_version!(options)
  get_json(path_user_version(path, options))
end

#sleep_logs_by_date_range(start_date, end_date, options = {}) ⇒ Object

The Get Sleep Logs by Date Range endpoint returns a list of a user’s sleep log entries (including naps) as well as detailed sleep entry data for a given date range (inclusive of start and end dates).

This endpoint supports two kinds of sleep data:

  • stages : Levels data is returned with 30-second granularity. ‘Sleep Stages’ levels include deep, light, rem, and wake.

  • classic : Levels data returned with 60-second granularity. ‘Sleep Pattern’ levels include asleep, restless, and awake.

The response could be a mix of classic and stages sleep logs.

Note: shortData is only included for stages sleep logs and includes wake periods that are 3 minutes or less in duration.

This distinction is to simplify graphically distinguishing short wakes from longer wakes, but they are physiologically equivalent.



66
67
68
69
70
# File 'lib/fitbit_client/resources/sleep.rb', line 66

def sleep_logs_by_date_range(start_date, end_date, options = {})
  path = "/sleep/date/#{iso_date(start_date)}/#{iso_date(end_date)}"
  sleep_default_version!(options)
  get_json(path_user_version(path, options))
end

#sleep_logs_list(before_date, after_date, sort, limit, options = {}) ⇒ Object

The Get Sleep Logs List endpoint returns a list of a user’s sleep logs (including naps) before or after a given day with offset, limit, and sort order.

This endpoint supports two kinds of sleep data:

  • stages : Levels data is returned with 30-second granularity. ‘Sleep Stages’ levels include deep, light, rem, and wake.

  • classic : Levels data returned with 60-second granularity. ‘Sleep Pattern’ levels include asleep, restless, and awake.

The response could be a mix of classic and stages sleep logs.

Note: shortData is only included for stages sleep logs and includes wake periods that are 3 minutes or less in duration.

This distinction is to simplify graphically distinguishing short wakes from longer wakes, but they are physiologically equivalent.

before_date : Either beforeDate or afterDate must be specified.
              Set sort to desc when using beforeDate.
after_date  : Either beforeDate or afterDate must be specified.
              Set sort to asc when using afterDate.


91
92
93
94
95
96
97
98
99
100
# File 'lib/fitbit_client/resources/sleep.rb', line 91

def sleep_logs_list(before_date, after_date, sort, limit, options = {})
  raise 'Before date and Atfer date cannot both be specified' if before_date && after_date
  if before_date
    params = { 'beforeDate' => iso_date(before_date), 'sort' => sort, 'limit' => limit, 'offset' => 0 }
  elsif after_date
    params = { 'afterDate' => iso_date(after_date), 'sort' => sort, 'limit' => limit, 'offset' => 0 }
  end
  sleep_default_version!(options)
  get_json(path_user_version('/sleep/list', options), params)
end

#sleep_time_series(resource, date, period_or_end_date, options = {}) ⇒ Object

The Get Sleep Time Series endpoint returns time series data in the specified range for a given resource in the format requested using units in the unit system that corresponds to the Accept-Language header provided.

API Version: 1

Note: This API has been deprecated with the introduction of version 1.2 of the Sleep APIs described above. Sleep Stages data cannot be retrieved with this API. If your application requires data consistency while you transition over to the version 1.2 Sleep APIs, you can get this data through the version 1 Get Sleep Logs by Date endpoint.

resource           : startTime | timeInBed | minutesAsleep |
                     awakeningsCount | minutesAwake | minutesToFallAsleep
                     minutesAfterWakeup | efficiency
date               : The start date for a range or end date of the period specified
period_or_end_date : One of  1d, 7d, 30d, 1w, 1m, 3m, 6m, 1y, max or the end date of the range


24
25
26
27
28
# File 'lib/fitbit_client/resources/sleep.rb', line 24

def sleep_time_series(resource, date, period_or_end_date, options = {})
  end_limit = period_or_date_param(period_or_end_date)
  path = time_series_path('sleep', resource, date, end_limit)
  get_json(path_user_version(path, options))
end

#update_sleep_goals(min_duration_minutes) ⇒ Object

Creates or updates a user’s sleep goal and get a response in the in the format requested.

Access Type: Read & Write

min_duration_minutes : The target sleep duration is in minutes


135
136
137
# File 'lib/fitbit_client/resources/sleep.rb', line 135

def update_sleep_goals(min_duration_minutes)
  post_json(path_user_version('/sleep/goal'), 'minDuration' => min_duration_minutes)
end