Sha256: 6221affcba63f5319c374347c053a7303c767840f06fc140866a334bb6467373

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

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

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

    def body_fat_logs(date=Date.today)
      get("user/-/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

1 entries across 1 versions & 1 rubygems

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