Fitbyte

Gem Version Build Status

Fitbyte provides a Ruby interface to the Fitbit Web API.

Installation

To install the latest release:

$ gem install fitbyte

To include in a Rails project, add it to the Gemfile:

gem 'fitbyte'

Usage

To use the Fitbit API, you must register your application at dev.fitbit.com. After registering, you should have access to CLIENT ID, CLIENT SECRET, and REDIRECT URI (Callback URL) values for use in instantiating a Fitbyte::Client object.

Rails

Please reference the fitbyte-rails repo as an example of how to use this gem within Rails.

Standalone

  • Create a client instance:
client = Fitbyte::Client.new(client_id: 'XXXXXX',
                             client_secret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
                             redirect_uri: 'http://example.com/handle/callback')
  • Generate a link for the 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 approving your app, you're sent to the redirect_uri, with an appended authorization code param, which you'll exchange for an access token:
client.get_token(auth_code)

You're now authenticated and can make calls to Fitbit's API:

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 the response more easily suited for attribute-assignment, it 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'
# If for some reason you had to mix snake and camel case like below,
# Fitbyte would make sure the result is a validly formatted request
client.log_activity activity_id: 12345, durationMillis: '683300'

Options

When initializing a Fitbyte::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)

License

This gem is available as open source under the terms of the MIT License.