Class: Ibancom::Client
- Inherits:
-
Object
- Object
- Ibancom::Client
- Includes:
- API::Services
- Defined in:
- lib/ibancom/client.rb
Constant Summary collapse
- BASE_URL =
"https://api.iban.com/clients/api"
Instance Attribute Summary collapse
-
#apikey ⇒ Object
readonly
Returns the value of attribute apikey.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#logger_enabled ⇒ Object
readonly
Returns the value of attribute logger_enabled.
-
#proxy ⇒ Object
readonly
Returns the value of attribute proxy.
-
#user_agent ⇒ Object
readonly
Returns the value of attribute user_agent.
Instance Method Summary collapse
- #connection ⇒ Object
- #default_user_agent ⇒ Object
- #execute(method, path, data = nil, options = {}) ⇒ Object
- #get(path, options = {}) ⇒ Object
- #handle_error_response(error) ⇒ Object
- #handle_network_error(error) ⇒ Object
- #handle_request_error(error) ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #patch(path, data = nil, options = {}) ⇒ Object
- #post(path, data = nil, options = {}) ⇒ Object
- #request(method, path, data = nil, options = {}) ⇒ Object
- #request_options(_method, _path, data, _options) ⇒ Object
- #resources ⇒ Object
Methods included from API::Services
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
18 19 20 21 22 23 24 25 |
# File 'lib/ibancom/client.rb', line 18 def initialize( = {}) @base_url = [:base_url] || BASE_URL @user_agent = [:user_agent] || default_user_agent @proxy = [:proxy] @apikey = [:apikey] || ENV["IBANCOM_APIKEY"] @logger_enabled = [:logger_enabled].nil? ? true : [:logger_enabled] @services = {} end |
Instance Attribute Details
#apikey ⇒ Object (readonly)
Returns the value of attribute apikey.
16 17 18 |
# File 'lib/ibancom/client.rb', line 16 def apikey @apikey end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
16 17 18 |
# File 'lib/ibancom/client.rb', line 16 def base_url @base_url end |
#logger_enabled ⇒ Object (readonly)
Returns the value of attribute logger_enabled.
16 17 18 |
# File 'lib/ibancom/client.rb', line 16 def logger_enabled @logger_enabled end |
#proxy ⇒ Object (readonly)
Returns the value of attribute proxy.
16 17 18 |
# File 'lib/ibancom/client.rb', line 16 def proxy @proxy end |
#user_agent ⇒ Object (readonly)
Returns the value of attribute user_agent.
16 17 18 |
# File 'lib/ibancom/client.rb', line 16 def user_agent @user_agent end |
Instance Method Details
#connection ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ibancom/client.rb', line 27 def connection @connection ||= Faraday.new do |builder| builder.request :url_encoded builder.use Faraday::Response::RaiseError builder.response :json, content_type: /\bjson$/, preserve_raw: true, parser_options: { symbolize_names: true } builder.proxy = @proxy if proxy if @logger_enabled builder.response :logger, nil, { headers: true, bodies: true } do |logger| logger.filter(/("password":)"(\w+)"/, '\1[FILTERED]') end end builder.adapter :net_http end end |
#default_user_agent ⇒ Object
49 50 51 |
# File 'lib/ibancom/client.rb', line 49 def default_user_agent "Ibancom/#{Ibancom::VERSION} Ruby Client (Faraday/#{Faraday::VERSION})" end |
#execute(method, path, data = nil, options = {}) ⇒ Object
65 66 67 |
# File 'lib/ibancom/client.rb', line 65 def execute(method, path, data = nil, = {}) request(method, path, data, ) end |
#get(path, options = {}) ⇒ Object
53 54 55 |
# File 'lib/ibancom/client.rb', line 53 def get(path, = {}) execute :get, path, nil, end |
#handle_error_response(error) ⇒ Object
99 100 101 102 |
# File 'lib/ibancom/client.rb', line 99 def handle_error_response(error) puts "ERROR #{error.response}" raise error end |
#handle_network_error(error) ⇒ Object
104 105 106 |
# File 'lib/ibancom/client.rb', line 104 def handle_network_error(error) raise error end |
#handle_request_error(error) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ibancom/client.rb', line 86 def handle_request_error(error) case error when Faraday::ClientError if error.response handle_error_response(error) else handle_network_error(error) end else raise error end end |
#patch(path, data = nil, options = {}) ⇒ Object
61 62 63 |
# File 'lib/ibancom/client.rb', line 61 def patch(path, data = nil, = {}) execute :patch, path, data, end |
#post(path, data = nil, options = {}) ⇒ Object
57 58 59 |
# File 'lib/ibancom/client.rb', line 57 def post(path, data = nil, = {}) execute :post, path, data, end |
#request(method, path, data = nil, options = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ibancom/client.rb', line 69 def request(method, path, data = nil, = {}) = (method, path, data, ) uri = "#{base_url}#{path}" begin connection.run_request(method, uri, [:body], [:headers]) rescue StandardError => e handle_request_error(e) end end |
#request_options(_method, _path, data, _options) ⇒ Object
80 81 82 83 84 |
# File 'lib/ibancom/client.rb', line 80 def (_method, _path, data, ) .tap do |defaults| defaults[:body] = defaults[:body].merge(data) end end |
#resources ⇒ Object
45 46 47 |
# File 'lib/ibancom/client.rb', line 45 def resources @resources ||= {} end |