class Fitbark::Auth
Provides oauth2 methods
Attributes
client_id[R]
client_secret[R]
code[RW]
redirect_uri[R]
token[W]
token_data[R]
token_info[R]
uri[RW]
Public Class Methods
new(client_id: nil, client_secret: nil, redirect_uri: nil, code: nil, token: nil)
click to toggle source
A sample usage:¶ ↑
First step to generate an authorization uri:
client_id = 'CLIENT-ID-PROVIDED-BY-FITBARK' client_secret = 'CLIENT-SECRET-PROVIDED-BY-FITBARK' redirect_uri = 'urn:ietf:wg:oauth:2.0:oob' auth = Fitbark::Auth.new(client_id: client_id, redirect_uri: redirect_uri) auth.authorization_uri
Once that authorization uri was open and fetched the authorization code from html (normally this operation is done with browser), proceed with 2nd step to retieve access token:
authorization_code = '27e5dd1307cddc7b5d8d72264ef1...' auth = Fitbark::Auth.new(client_id: client_id, redirect_uri: redirect_uri, code: authorization_code, client_secret: client_secret) auth.fetch_access_token!
# File lib/fitbark/auth.rb, line 20 def initialize(client_id: nil, client_secret: nil, redirect_uri: nil, code: nil, token: nil) @client_id = client_id @client_secret = client_secret @redirect_uri = redirect_uri @code = code @token = read_token(token) @uri = Addressable::URI.new(host: API_HOST, scheme: API_SCHEME) end
Public Instance Methods
fetch_access_token!()
click to toggle source
return a Fitbark::Data::Token
object
# File lib/fitbark/auth.rb, line 47 def fetch_access_token! if token_response.success? apply_token_data(token_response) else token_error(token_response) end end
fetch_token_info()
click to toggle source
return a Fitbark::Data::TokenInfo
object
# File lib/fitbark/auth.rb, line 56 def fetch_token_info response = token_info_response if response.success? apply_token_info(response) else token_error(response) end end
token()
click to toggle source
return the raw access_token (String)
# File lib/fitbark/auth.rb, line 66 def token read_token end
Private Instance Methods
apply_token_data(resp)
click to toggle source
# File lib/fitbark/auth.rb, line 103 def apply_token_data(resp) @token_data = Fitbark::Data::Token.new(parsed_body(resp)) end
apply_token_info(resp)
click to toggle source
# File lib/fitbark/auth.rb, line 107 def apply_token_info(resp) @token_info = Fitbark::Data::TokenInfo.new(parsed_body(resp)) end
parsed_body(resp)
click to toggle source
# File lib/fitbark/auth.rb, line 121 def parsed_body(resp) Oj.load(resp.body) end
read_token(val = @token)
click to toggle source
# File lib/fitbark/auth.rb, line 117 def read_token(val = @token) (token_data.respond_to?(:token) && token_data.token) || val end
request_token_body()
click to toggle source
# File lib/fitbark/auth.rb, line 81 def request_token_body { client_id: client_id, client_secret: client_secret, grant_type: :authorization_code, redirect_uri: redirect_uri, code: code } end
token_error(resp)
click to toggle source
# File lib/fitbark/auth.rb, line 111 def token_error(resp) raise(Fitbark::Errors::FetchTokenError .new(code: resp.status, message: parsed_body(resp)['error_description'])) end
token_info_response()
click to toggle source
# File lib/fitbark/auth.rb, line 91 def token_info_response raise Fitbark::Errors::TokenNotProvidedError if token.nil? Faraday.new(url: uri.site).get do |req| req.url TOKEN_INFO_PATH req.headers = { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{token}" } end end
token_response()
click to toggle source
# File lib/fitbark/auth.rb, line 74 def token_response @token_response ||= Faraday.new(url: uri.site).post do |req| req.url TOKEN_PATH req.body = request_token_body end end