🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

fitbit_api

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fitbit_api

1.1.0
Rubygems
Version published
Maintainers
1
Created
Source

FitbitAPI

Gem Version CI Workflow

FitbitAPI provides a Ruby interface to the Fitbit Web API.

Installation

To install the latest release:

$ gem install fitbit_api

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

gem 'fitbit_api'

Getting Started

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.

Rails

You can reference the fitbit_api_rails repo as a simple example of how to use this gem within a Rails project.

Quickstart

If you already have a user's token data and Fitbit user_id:

client = FitbitAPI::Client.new(client_id: 'XXXXXX',
                               client_secret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
                               access_token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
                               refresh_token: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
                               expires_at: 1234567890,
                               user_id: 'XXXXXX')

OAuth 2.0 Authorization Flow

  • 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 authorization code 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.

Interacting with the API

Once a valid token has been generated, you're able to make API calls via the client object:

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:

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:

# options with snake_cased keys
client.log_activity activity_id: 12345, duration_millis: '50000'
# options with camelCased keys
client.log_activity activityId: 54321, durationMillis: '44100'
# options with mixed snake and camel cased keys
client.log_activity activity_id: 12345, durationMillis: '683300'

Options

When initializing a FitbitAPI::Client instance, you're given access to a handful of options:

OptionDescriptionDefault
api_versionAPI version to be used when making requests"1"
unit_systemThe measurement unit system to use for response values"en_US"
localeThe locale to use for response values"en_US"
scopeA list of permissions being requested (array or space-delimited string)%w[activity nutrition profile settings sleep social weight heartrate respiratory_rate oxygen_saturation cardio_fitness temperature electrocardiogram irregular_rhythm_notifications]
snake_case_keysTransform response payload's keys to snake case formatfalse
symbolize_keysTransform response payload's keys to symbolsfalse
auto_refresh_tokenAutomatically refreshes the access token once expiredtrue
on_token_refreshA callback to be invoked whenever the access token is refreshednil

If using this library in Rails, you can configure these options globally in an initializer:

# config/initializers/fitbit_api.rb

FitbitAPI.configure do |config|
  config.client_id       = 'XXXX'
  config.client_secret   = 'xxxx'
  config.snake_case_keys = true
  config.symbolize_keys  = true
end

License

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

FAQs

Package last updated on 02 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts