Sha256: 07b0526018ffdf5a3558345fd1003b1a7e8a8b4686253e3a5444f0f88a182af9

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

require "methadone"
require "fitgem"

module Howami
  class Authentication
    include Methadone::CLILogging

    def initialize
      @client = Fitgem::Client.new({:consumer_key => CONSUMER_KEY, :consumer_secret => CONSUMER_SECRET})
    end

    def fetch_request_token!
      @request_token = @client.request_token
    end

    def do_interactive_auth
      fetch_request_token!

      auth_url = "http://www.fitbit.com/oauth/authorize?oauth_token=#{@request_token.token}"
      puts "Go to #{auth_url} and then enter the verifier code below and hit Enter"
      verifier = gets.chomp
      debug "Parsed user verifier as: #{verifier}"

      access_token = self.get_access_token_with_verifier(verifier)
      debug "Received user token is:    "+access_token.token
      debug "Received user secret is:   "+access_token.secret

      Configuration.store_credentials(access_token.token, access_token.secret)
    end

    def get_access_token_with_verifier(verifier)
      @client.authorize(@request_token.token, @request_token.secret, { :oauth_verifier => verifier })
    end

    def self.authenticated_client
      raise "No valid credentials stored!" unless Configuration.has_valid_credentials?
      @auth_client ||= Fitgem::Client.new({
        :consumer_key => CONSUMER_KEY,
        :consumer_secret => CONSUMER_SECRET,
        :token => Configuration.get_credentials[:user_token],
        :secret => Configuration.get_credentials[:user_secret],
        :user_id => "-"
        })
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
howami-0.0.2 lib/howami/authentication.rb
howami-0.0.1 lib/howami/authentication.rb