Sha256: 93b25b216a9c24ee7909de2dc059fd578c99fb502b44352ce517c9bcc5a66d1e

Contents?: true

Size: 1.72 KB

Versions: 7

Compression:

Stored size: 1.72 KB

Contents

module FitbitAPI
  class Client
    BODY_RESOURCES = %w(bmi fat weight)

    def weight_logs(date=Date.today)
      get("user/#{user_id}/body/log/weight/date/#{format_date(date)}.json")
    end

    def body_fat_logs(date=Date.today)
      get("user/#{user_id}/body/log/fat/date/#{format_date(date)}.json")
    end

    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

      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}/body/#{resource}/date/#{format_date(end_date)}/#{period}.json")
      else
        result = get("user/#{user_id}/body/#{resource}/date/#{format_date(start_date)}/#{format_date(end_date)}.json")
      end

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

    def log_weight(body)
      post("user/#{user_id}/body/log/weight.json", body)
    end

    def delete_weight_log(weight_log_id)
      delete("user/#{user_id}/body/log/weight/#{weight_log_id}.json")
    end

    def log_body_fat(body)
      post("user/#{user_id}/body/log/fat.json", body)
    end

    def delete_body_fat_log(body_fat_log_id)
      delete("user/#{user_id}/body/log/fat/#{body_fat_log_id}.json")
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
fitbit_api-0.14.2 lib/fitbit_api/body.rb
fitbit_api-0.14.1 lib/fitbit_api/body.rb
fitbit_api-0.14.0 lib/fitbit_api/body.rb
fitbit_api-0.13.0 lib/fitbit_api/body.rb
fitbit_api-0.12.2 lib/fitbit_api/body.rb
fitbit_api-0.12.1 lib/fitbit_api/body.rb
fitbit_api-0.12.0 lib/fitbit_api/body.rb