Class: Modulr::Client

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

Constant Summary collapse

SANDBOX_URL =
"https://api-sandbox.modulrfinance.com/api-sandbox-token"
BASE_URL =
SANDBOX_URL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API::Services

#accounts, #customers, #notifications, #payments, #transactions

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



19
20
21
22
23
24
25
26
27
# File 'lib/modulr/client.rb', line 19

def initialize(options = {})
  @base_url = options[:base_url] || BASE_URL
  @origin = options[:origin] || default_origin
  @proxy = options[:proxy]
  @apikey = options[:apikey] || ENV["MODULR_APIKEY"]
  @apisecret = options[:apisecret] || ENV["MODULR_APISECRET"]
  @logger_enabled = options[:logger_enabled].nil? ? true : options[:logger_enabled]
  @services = {}
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



17
18
19
# File 'lib/modulr/client.rb', line 17

def base_url
  @base_url
end

#logger_enabledObject (readonly)

Returns the value of attribute logger_enabled.



17
18
19
# File 'lib/modulr/client.rb', line 17

def logger_enabled
  @logger_enabled
end

#originObject (readonly)

Returns the value of attribute origin.



17
18
19
# File 'lib/modulr/client.rb', line 17

def origin
  @origin
end

#proxyObject (readonly)

Returns the value of attribute proxy.



17
18
19
# File 'lib/modulr/client.rb', line 17

def proxy
  @proxy
end

#usernameObject (readonly)

Returns the value of attribute username.



17
18
19
# File 'lib/modulr/client.rb', line 17

def username
  @username
end

Instance Method Details

#add_auth_options!(options) ⇒ Object



82
83
84
85
86
# File 'lib/modulr/client.rb', line 82

def add_auth_options!(options)
  return sandbox_auth_options(options) if @base_url.eql?(SANDBOX_URL)

  auth_options(options)
end

#auth_options(options) ⇒ Object



92
93
94
95
96
97
# File 'lib/modulr/client.rb', line 92

def auth_options(options)
  signature = Auth::Signature.calculate(apikey: @apikey, apisecret: @apisecret)
  options[:headers][:authorization] = signature.authorization
  options[:headers][:date] = signature.timestamp
  options[:headers][:"x-mod-nonce"] = signature.nonce
end

#connectionObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/modulr/client.rb', line 29

def connection
  @connection ||= Faraday.new do |builder|
    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_originObject



110
111
112
# File 'lib/modulr/client.rb', line 110

def default_origin
  "Modulr/#{Modulr::VERSION} Ruby Client (Faraday/#{Faraday::VERSION})"
end

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



58
59
60
# File 'lib/modulr/client.rb', line 58

def execute(method, path, data = nil, options = {})
  request(method, path, data, options)
end

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



46
47
48
# File 'lib/modulr/client.rb', line 46

def get(path, options = {})
  execute :get, path, nil, options
end

#handle_request_error(error) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/modulr/client.rb', line 99

def handle_request_error(error)
  case error
  when Faraday::ClientError
    raise ClientError, error
  when Faraday::ServerError
    raise ServerError, error
  else
    raise Error, error
  end
end

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



50
51
52
# File 'lib/modulr/client.rb', line 50

def post(path, data = nil, options = {})
  execute :post, path, data, options
end

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



54
55
56
# File 'lib/modulr/client.rb', line 54

def put(path, data = nil, options = {})
  execute :put, path, data, options
end

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



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/modulr/client.rb', line 62

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]) do |request|
      request.params.update(options) if options
    end
  rescue StandardError => e
    handle_request_error(e)
  end
end

#request_options(_method, _path, data, _options) ⇒ Object



75
76
77
78
79
80
# File 'lib/modulr/client.rb', line 75

def request_options(_method, _path, data, _options)
  default_options.tap do |defaults|
    add_auth_options!(defaults)
    defaults[:body] = JSON.dump(data) if data
  end
end

#sandbox_auth_options(options) ⇒ Object



88
89
90
# File 'lib/modulr/client.rb', line 88

def sandbox_auth_options(options)
  options[:headers][:authorization] = @apikey
end