Sha256: ae551bd5a26df1baeb87daaf10fee5d24832ce3ab2e712c793ee1e6845e7a3f0
Contents?: true
Size: 1.92 KB
Versions: 2
Compression:
Stored size: 1.92 KB
Contents
# frozen_string_literal: true require 'faraday' require 'faraday_middleware' require 'rainbow' require 'addressable' require 'capital_on_tap/response' module CapitalOnTap class Connection attr_accessor :debug, :access_token, :expires_in, :refresh_token # This is a block to be run when the token is refreshed. This way you can do whatever you want # with the new parameters returned by the refresh method. attr_accessor :on_refresh_cb BASE_PATH = '/api/v1/' def initialize(access_token:, expires_in: 1200, refresh_token: nil) @access_token = access_token @expires_in = expires_in @refresh_token ||= refresh_token # do not overwrite if it's already set end def get(path, params = {}) log "GET #{path} with #{params}" http_response = adapter.get(path, params) Response.new(http_response) end def post(path, params = {}) log "POST #{path} with #{params}" http_response = adapter.post(path, params.to_json) Response.new(http_response) end def log(text) return unless CapitalOnTap.configuration.debug? puts Rainbow("[CapitalOnTap] #{text}").magenta.bright end private def base_url Addressable::URI.join(CapitalOnTap.configuration.base_url, BASE_PATH).to_s end # The authorization header that must be added to every request for authorized requests. def authorization_header { 'Authorization' => "Bearer #{@access_token}" } end def adapter Faraday.new(url: base_url) do |conn| conn.headers['Authorization'] = "Bearer #{@access_token}" if !@access_token.to_s.empty? conn.headers['Content-Type'] = 'application/json' conn.use FaradayMiddleware::ParseJson conn.response :json, parser_options: { symbolize_names: true } conn.response :logger if CapitalOnTap.configuration.debug? conn.adapter Faraday.default_adapter end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
capital_on_tap-0.1.3 | lib/capital_on_tap/connection.rb |
capital_on_tap-0.1.2 | lib/capital_on_tap/connection.rb |