Sha256: d4aa6627655c88b8337fc19263489e96465457394da55a5d6d1ea182c50ad959

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

module FitbitAPI
  class Client
    FOOD_RESOURCES = %w(caloriesIn water)

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

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

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

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

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

    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

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