FitbitAPI provides a Ruby interface to the Fitbit Web API.
To install the latest release:
$ gem install fitbit_api
To include in a Rails project, add it to the Gemfile:
gem 'fitbit_api'
To use the Fitbit API, you must register your application at dev.fitbit.com. After registering, you should have access to the CLIENT ID and CLIENT SECRET values for use in instantiating a FitbitAPI::Client object.
You can reference the fitbit_api_rails repo as a simple example of how to use this gem within a Rails project.
If you already have access to a user's stored refresh token, you can instantiate a client instance like so:
client = FitbitAPI::Client.new(client_id: 'XXXXXX',
client_secret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
refresh_token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
- Create a client instance (ensure that
redirect_uri
is passed in):
client = FitbitAPI::Client.new(client_id: 'XXXXXX',
client_secret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
redirect_uri: 'http://example.com/handle/callback')
- Generate a link for your app's Fitbit authorization page:
client.auth_url
# => https://fitbit.com/oauth2/authorize?client_id=123XYZ&redirect_uri=...
- Follow the generated link to Fitbit's authorization page. After granting permission for your app, you're sent to the
redirect_uri
, with an appended authorizationcode
param, which you'll exchange for an access token:
client.get_token(auth_code)
You're now authorized and can make calls to Fitbit's API.
Once a valid token has been generated, you're able to make API calls from the client object, like so:
client.food_logs Date.today
# => { "foods" => [{ "isFavorite" => true, "logDate" => "2015-06-26", "logId" => 1820, "loggedFood" => { "accessLevel" => "PUBLIC", "amount" => 132.57, "brand" => "", "calories" => 752, ...}] }
To make responses more easily suited for attribute-assignment, they can be parsed to return a hash whose keys are in snake_case format. This can be done by setting the client's snake_case_keys
option to true
, like so:
client.snake_case_keys = true
client.food_logs Date.today
# => { "foods" => [{ "is_favorite" => true, "log_date" => "2015-06-26", "log_id" => 1820, "logged_food" => { "access_level" => "PUBLIC", "amount" => 132.57, "brand" => "", "calories" => 752, ...}] }
Similarly, all arguments passed in through a POST request are automatically converted to camelCase before they hit Fitbit's API, making it easy to keep your codebase stylistically consistent. For example, all of the following would result in valid API calls:
client.log_activity activity_id: 12345, duration_millis: '50000'
client.log_activity activityId: 54321, durationMillis: '44100'
# You can even mix snake and camel case like in the example below,
# FitbitAPI will make sure the result is a validly formatted request
client.log_activity activity_id: 12345, durationMillis: '683300'
When initializing a FitbitAPI::Client
instance, you're given access to a handful of options:
-
:api_version
- API version to be used when making requests (default: "1") -
:unit_system
- The measurement unit system to use for response values (default: "en_US" | available: "en_US", "en_GB", and "any" for metric) -
:locale
- The locale to use for response values (default: "en_US" | available: "en_US", "fr_FR", "de_DE", "es_ES", "en_GB", "en_AU", "en_NZ" and "ja_JP") -
:scope
- A space-delimited list of the permissions you are requesting (default: "activity nutrition profile settings sleep social weight heartrate" | available: "activity", "heartrate", "location", "nutrition", "profile", "settings" "sleep", "social" and "weight") -
:snake_case_keys
- Transform returned object's keys to snake case format (default: false) -
:symbolize_keys
- Transform returned object's keys to symbols (default: false)
This gem is available as open source under the terms of the MIT License.