FitBit on Rails

Upon my return to the United States last week, I was blown away be a few things which I'm sure seemed perfectly normal to me before my departure. For example, during 6 months in Southeast Asia I saw less than 10 people wearing fitness activity trackers; they were always adorned by tourists or expats, never locals. Last week, I was astounded by the proliferation of Fitbits and Jawbones on people's wrists, signaling a rising consciousness in the fitness realm and a wealth of newly collected activity data.

After one of my friends showed me the updated Fitbit app and I did some research on their newly announced products which will begin to collect Heart Rate and GPS location data, I decided to dig into their API the following day and figure how to connect it to a rails application. Fitbit has a well-documented API that is supported by their Developer Pages, but I had to parse through a couple of tutorials and documentation resources to get everything set up. Hopefully this post clarifies anyone trying to connect their Rails app to Fitbit's API in the future. 

 

Dev Center 

Start off by creating an application on Fitbit's developer portal, ensuring to save the 'consumer-key' and 'consumer-secret' as we'll be including those in our application soon enough. 

 

New App

Set up a new application by running 

rails new FitbitApp

 

Gemfile 

The next step is to bundle in two gems that will enable connectivity to Fitbit. Just like similar gems to connect to Twitter, Facebook, and other services, Omniauth-Fitbit allows you to initialize a FitBit connection. FitGem is a useful Ruby library that will allow us to return a JSON object with a user's Fitbit information. You can learn more about FitGem and it's other available methods at fitbitclient.com.

gem 'omniauth-fitbit'
gem 'fitgem'

 

Initializer

Next, we'll need to establish a connection to the Fitbit provider using an initializer file. In the terminal, create the file using: 

touch app/config/initializers/omniauth.rb

Then in the newly created file, we'll add in our application's consumer key and consumer secret: 

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :fitbit, 'YOUR_CONSUMER_KEY', 'YOUR_CONSUMER_SECRET'
end

If you're planning on pushing the application's code to Github, I highly recommend storing your application's secure information using environmental variables. The figaro gem makes this pain-free. 

 

Controller 

As this is will just be a basic application that receives a user's information, the controller is going to handle the bulk of the code. Let's dive into what we'll need to establish a connection, and I'll comment out what's going on along the way. I called my controller fitbit_auth, but you can call it whatever you want. 

controllercode.jpg

 

View

Next, we'll have to build a very simple view to interact with our application in the browser. This is literally the only code I wrote in the entire views folder (in app/views/fitbit_auth/index.html.erb)

<%= link_to "Connect to Fitbit!!", auth_fitbit_path %>

 

Routes

Finally, let's not forget to connect our views and methods to their corresponding routes that will interact with both the browser and the Fitbit Omniauth log-in page. 

Rails.application.routes.draw do
  root 'fitbit_auth#index'
  post "/auth/fitbit" => "fitbit_auth#make_request"
  get "/auth/fitbit/callback" => "fitbit_auth#get_response"
end 

At this point, if you start a server using rails server, you should be able to see our simple view, login to Fitbit, and get your user information callback rendered a JSON object. The final result should look something like this: 

JSONrender.jpg

If you have a Fitbit account, you can interact with my example application and give it a try for yourself right here, or if something didn't work check out the source code. Once you successfully connect to Fitbit, definitely check out the Fitgem client library and see what other methods you can leverage to build apps using the Fitbit API!