Created
August 30, 2019 18:09
-
-
Save elorest/690c054d4343cab1d83c81128efadc7c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Services::FitbitController < ApplicationController | |
access user: :all | |
def callback | |
logger.info params | |
logger.info "Hello********************************************************************************" | |
if params[:code] | |
current_user.services.update_all(expired: true) | |
current_user.services.create(code: params[:code]) | |
redirect_to root_path, notice: "Succesfully connected to Fitbit" | |
else | |
redirect_to "https://www.fitbit.com/oauth2/authorize?client_id=#{Rails.application.secrets.fitbit_id}&response_type=code&scope=profile%20activity%20weight&expires_in=31536000&redirect_uri=#{services_fitbit_url(protocol: 'https')}" | |
end | |
end | |
def destroy_all | |
respond_to do |format| | |
if current_user.services.destroy_all #current_user.services.destroy_all | |
puts "Unlink Fitbit account: Destroying services for user id #{current_user.id}" | |
@fitbit_msg = "Fitbit account unlinked." | |
format.js { render :update_fitbit_sync } | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# == Schema Information | |
# | |
# Table name: services | |
# | |
# id :integer not null, primary key | |
# user_id :integer | |
# token :string(255) | |
# expired :boolean default(FALSE) | |
# type :string(255) | |
# created_at :datetime not null | |
# updated_at :datetime not null | |
# refresh_token :string(255) | |
# code :string(255) | |
# expires_at :datetime | |
# | |
class Service < ActiveRecord::Base | |
FITBIT64 = Base64.strict_encode64("#{Rails.application.secrets.fitbit_id}:#{Rails.application.secrets.fitbit_secret}") | |
#Callback = https://cappahealth.com/services/nokia | |
NOKIA64 = Base64.strict_encode64("#{Rails.application.secrets.nokia_id}:#{Rails.application.secrets.nokia_secret}") | |
belongs_to :user | |
before_save do | |
get_token unless refresh_token | |
end | |
def token | |
if updated_at <= 59.minutes.ago | |
get_token! | |
end | |
super | |
end | |
def get_token | |
body = {client_id: Rails.application.secrets.fitbit_id} | |
if refresh_token | |
body[:grant_type] = "refresh_token" | |
body[:refresh_token] = refresh_token | |
else | |
body[:grant_type] = "authorization_code" | |
body[:code] = code | |
end | |
resp = Typhoeus::Request.post("https://api.fitbit.com/oauth2/token?redirect_uri=#{URI.join(CappaHealth.configuration.brand_url, "/services/fitbit")}", | |
headers: {Authorization: "Basic #{FITBIT64}", 'Content-Type' => "application/x-www-form-urlencoded; charset=utf-8"}, | |
body: body) | |
self.response = resp.inspect | |
json_response_body = JSON.parse(resp.options[:response_body]) | |
if resp.success? # and the access and refresh token exist in the response | |
if json_response_body["access_token"].size > 0 && json_response_body["refresh_token"].size > 0 | |
self.token, self.refresh_token = json_response_body.values_at("access_token", "refresh_token") | |
else | |
self.expired = true | |
end | |
else | |
error_msgs = json_response_body['errors'].map{|error| error["message"]}.join(', ') | |
puts "Fitbit error(s) for #{self.user.email} in get_token:" | |
puts "#{error_msgs}" | |
return | |
end | |
end | |
def get_token! | |
get_token | |
save | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment