Class: BancoBrasilStatements::ApiClient
- Inherits:
-
Object
- Object
- BancoBrasilStatements::ApiClient
- Defined in:
- lib/bb_statements/api_client.rb
Constant Summary collapse
- TOKEN_EXPIRE_TIME =
570
Instance Attribute Summary collapse
-
#access_token_requested_at ⇒ Object
Returns the value of attribute access_token_requested_at.
-
#config ⇒ Object
Returns the value of attribute config.
-
#default_headers ⇒ Object
Returns the value of attribute default_headers.
Class Method Summary collapse
Instance Method Summary collapse
- #build_request(path, opts = {}) ⇒ Object
- #build_request_url(path, base_path = nil) ⇒ Object
- #call_api(http_method, path, opts = {}) ⇒ Object
-
#initialize(config = Configuration.default) ⇒ ApiClient
constructor
Initializes the ApiClient.
- #renew_token ⇒ Object
- #update_params_for_auth!(header_params) ⇒ Object
- #valid_token? ⇒ Boolean
Constructor Details
#initialize(config = Configuration.default) ⇒ ApiClient
Initializes the ApiClient
16 17 18 19 20 21 22 23 24 |
# File 'lib/bb_statements/api_client.rb', line 16 def initialize(config = Configuration.default) @config = config @user_agent = "bb-statements-gem/#{VERSION}/ruby" @default_headers = { 'Content-Type' => 'application/json', 'User-Agent' => @user_agent } @access_token_requested_at = nil end |
Instance Attribute Details
#access_token_requested_at ⇒ Object
Returns the value of attribute access_token_requested_at.
12 13 14 |
# File 'lib/bb_statements/api_client.rb', line 12 def access_token_requested_at @access_token_requested_at end |
#config ⇒ Object
Returns the value of attribute config.
12 13 14 |
# File 'lib/bb_statements/api_client.rb', line 12 def config @config end |
#default_headers ⇒ Object
Returns the value of attribute default_headers.
12 13 14 |
# File 'lib/bb_statements/api_client.rb', line 12 def default_headers @default_headers end |
Class Method Details
Instance Method Details
#build_request(path, opts = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/bb_statements/api_client.rb', line 43 def build_request(path, opts = {}) base_path = opts[:base_path] url = build_request_url(path, base_path) header_params = @default_headers.merge(opts[:header_params] || {}) header_params['Accept-Encoding'] = 'gzip' header_params['x-br-com-bb-ipa-mciteste'.to_sym] = '704950857' unless Rails.env.production? query_params = opts[:query_params] || {} form_params = opts[:form_params] || {} update_params_for_auth! header_params Faraday.new(url: url) do |faraday| faraday.request :url_encoded faraday.request :json faraday.adapter Faraday.default_adapter if @config.verify_ssl faraday.ssl.verify = true faraday.ssl.verify_mode = @config.verify_ssl_host ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE faraday.ssl.ca_file = @config.ssl_ca_cert.to_s if @config.ssl_ca_cert faraday.ssl.client_cert = OpenSSL::X509::Certificate.new(File.read(@config.cert_file)) if @config.cert_file if @config.key_file faraday.ssl.client_key = OpenSSL::PKey::RSA.new(File.read(@config.key_file), @config.ssl_key_passwd) end else faraday.ssl.verify = false end faraday.response :logger if @config.debugging faraday.response :raise_error faraday.response :json faraday.headers = header_params faraday.params = query_params faraday..timeout = @config.timeout if @config.timeout end end |
#build_request_url(path, base_path = nil) ⇒ Object
81 82 83 84 85 |
# File 'lib/bb_statements/api_client.rb', line 81 def build_request_url(path, base_path = nil) # Add leading and trailing slashes to path path = "/#{path}".gsub(/\/+/, '/') @config.base_url(base_path || @config.base_path) + path end |
#call_api(http_method, path, opts = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/bb_statements/api_client.rb', line 30 def call_api(http_method, path, opts = {}) request = build_request(path, opts) response = request.send(http_method.downcase.to_sym) if @config.debugging @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n" end response.body rescue Faraday::Error => e fail BancoBrasilStatements::ApiError.new(e.) end |
#renew_token ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/bb_statements/api_client.rb', line 98 def renew_token api_instance = BancoBrasilClientCredentials::DefaultApi.new @config.client_credentials_api result = api_instance.oauth_token_post('client_credentials', @config.access_token_scopes, @config.basic_auth_token, 'application/x-www-form-urlencoded') self.access_token_requested_at = Time.zone.now @config.access_token = result.access_token result.access_token end |
#update_params_for_auth!(header_params) ⇒ Object
87 88 89 90 91 |
# File 'lib/bb_statements/api_client.rb', line 87 def update_params_for_auth!(header_params) renew_token unless valid_token? header_params['Authorization'] = "Bearer #{@config.access_token}" end |
#valid_token? ⇒ Boolean
93 94 95 96 |
# File 'lib/bb_statements/api_client.rb', line 93 def valid_token? access_token_requested_at && (Time.zone.now - access_token_requested_at) < TOKEN_EXPIRE_TIME end |