Class: Ibancom::Client

Inherits:
Object
  • Object
show all
Includes:
API::Services
Defined in:
lib/ibancom/client.rb

Constant Summary collapse

BASE_URL =
"https://api.iban.com/clients/api"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API::Services

#validations

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(options = {})
  @base_url = options[:base_url] || BASE_URL
  @user_agent = options[:user_agent] || default_user_agent
  @proxy = options[:proxy]
  @apikey = options[:apikey] || ENV["IBANCOM_APIKEY"]
  @logger_enabled = options[:logger_enabled].nil? ? true : options[:logger_enabled]
  @services = {}
end

Instance Attribute Details

#apikeyObject (readonly)

Returns the value of attribute apikey.



16
17
18
# File 'lib/ibancom/client.rb', line 16

def apikey
  @apikey
end

#base_urlObject (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_enabledObject (readonly)

Returns the value of attribute logger_enabled.



16
17
18
# File 'lib/ibancom/client.rb', line 16

def logger_enabled
  @logger_enabled
end

#proxyObject (readonly)

Returns the value of attribute proxy.



16
17
18
# File 'lib/ibancom/client.rb', line 16

def proxy
  @proxy
end

#user_agentObject (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

#connectionObject



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_agentObject



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, options = {})
  request(method, path, data, options)
end

#get(path, options = {}) ⇒ Object



53
54
55
# File 'lib/ibancom/client.rb', line 53

def get(path, options = {})
  execute :get, path, nil, options
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, options = {})
  execute :patch, path, data, options
end

#post(path, data = nil, options = {}) ⇒ Object



57
58
59
# File 'lib/ibancom/client.rb', line 57

def post(path, data = nil, options = {})
  execute :post, path, data, options
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, options = {})
  request_options = request_options(method, path, data, options)
  uri = "#{base_url}#{path}"

  begin
    connection.run_request(method, uri, request_options[:body], request_options[: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 request_options(_method, _path, data, _options)
  default_options.tap do |defaults|
    defaults[:body] = defaults[:body].merge(data)
  end
end

#resourcesObject



45
46
47
# File 'lib/ibancom/client.rb', line 45

def resources
  @resources ||= {}
end