class Redox::Source
Constants
- ACCESS_TOKEN_EXPIRATION_BUFFER
- SECONDS_PER_DAY
Attributes
access_token_expires_at[R]
api_key[R]
endpoint[R]
Public Class Methods
new(endpoint:, api_key:, secret:, test_mode: true)
click to toggle source
Calls superclass method
# File lib/redox/source.rb, line 15 def initialize(endpoint:, api_key:, secret:, test_mode: true) super() @connection = Faraday.new( url: endpoint, headers: { accept: "application/json", content_type: "application/json" } ) @endpoint = endpoint @api_key = api_key @secret = secret @test_mode = test_mode end
Public Instance Methods
access_token()
click to toggle source
# File lib/redox/source.rb, line 56 def access_token @connection.headers["Authorization"]&.delete_prefix "Bearer " end
access_token=(token)
click to toggle source
# File lib/redox/source.rb, line 60 def access_token=(token) if token.nil? @connection.headers.delete "Authorization" else @connection.authorization :Bearer, token end end
ensure_access_token()
click to toggle source
# File lib/redox/source.rb, line 50 def ensure_access_token synchronize { authenticate if access_token.nil? || token_expiring_soon? } end
execute_query(model)
click to toggle source
# File lib/redox/source.rb, line 30 def execute_query(model) ensure_access_token res = @connection.post("/endpoint", model.to_redox_json) message = begin JSON.parse(res.body) rescue nil end unless res.success? raise Redox::Error.new( status: res.status, body: res.body, message: Redox::Models::Message.from_redox_json(message) ) end message end
token_expiring_soon?()
click to toggle source
# File lib/redox/source.rb, line 68 def token_expiring_soon? DateTime.now > @access_token_expires_at - ACCESS_TOKEN_EXPIRATION_BUFFER end
Private Instance Methods
authenticate()
click to toggle source
# File lib/redox/source.rb, line 74 def authenticate self.access_token = nil res = @connection.post("/auth/authenticate", {apiKey: @api_key, secret: @secret}.to_json) raise Redox::AuthenticationError.new status: res.status, body: res.body unless res.success? data = JSON.parse(res.body) self.access_token = data["accessToken"] @access_token_expires_at = DateTime.parse(data["expires"]) end