Sha256: 4641d3fed25a5133df8bfefb1b9e295a7c4a17ed82a3e48786c7b54df08245ed

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

module FitbitAPI
  class Client
    SLEEP_RESOURCES = %w(startTime timeInBed minutesAsleep awakeningsCount
                         minutesAwake minutesToFallAsleep minutesAfterWakeup efficiency)

    def sleep_logs(date=Date.today)
      get("user/#{user_id}/sleep/date/#{format_date(date)}.json")
    end

    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

      if [period, start_date].none?
        raise FitbitAPI::InvalidArgumentError, 'A start_date or period is required.'
      end

      if period && !PERIODS.include?(period)
        raise FitbitAPI::InvalidArgumentError, "Invalid period: \"#{period}\". Please provide one of the following: #{PERIODS}."
      end

      if period
        result = get("user/#{user_id}/sleep/#{resource}/date/#{format_date(end_date)}/#{period}.json")
      else
        result = get("user/#{user_id}/sleep/#{resource}/date/#{format_date(start_date)}/#{format_date(end_date)}.json")
      end

      # remove root key from response
      result.values[0]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fitbit_api-0.11.0 lib/fitbit_api/sleep.rb