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

Class Method 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.



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

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.



19
20
21
# File 'lib/modulr/client.rb', line 19

def base_url
  @base_url
end

#logger_enabledObject (readonly)

Returns the value of attribute logger_enabled.



19
20
21
# File 'lib/modulr/client.rb', line 19

def logger_enabled
  @logger_enabled
end

#originObject (readonly)

Returns the value of attribute origin.



19
20
21
# File 'lib/modulr/client.rb', line 19

def origin
  @origin
end

#proxyObject (readonly)

Returns the value of attribute proxy.



19
20
21
# File 'lib/modulr/client.rb', line 19

def proxy
  @proxy
end

#usernameObject (readonly)

Returns the value of attribute username.



19
20
21
# File 'lib/modulr/client.rb', line 19

def username
  @username
end

Class Method Details

.idempotency_nonce(idempotency_key) ⇒ Object



60
61
62
63
64
# File 'lib/modulr/client.rb', line 60

def self.idempotency_nonce(idempotency_key)
  digest = OpenSSL::Digest.new("SHA256")
  hash = OpenSSL::HMAC.digest(digest, ENV["MODULR_APIKEY"], idempotency_key)
  Base64.urlsafe_encode64(hash)
end

Instance Method Details

#add_auth_options!(options) ⇒ Object



89
90
91
92
93
# File 'lib/modulr/client.rb', line 89

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

  auth_options(options)
end

#auth_options(options) ⇒ Object



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

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



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

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



136
137
138
# File 'lib/modulr/client.rb', line 136

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

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



48
49
50
# File 'lib/modulr/client.rb', line 48

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

#handle_request_error(error) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/modulr/client.rb', line 125

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



52
53
54
# File 'lib/modulr/client.rb', line 52

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

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



56
57
58
# File 'lib/modulr/client.rb', line 56

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

#request(method, path, data = nil, options = {}) ⇒ Object Also known as: execute



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/modulr/client.rb', line 66

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|
      merge_query_params(request, method, options)
    end
  rescue StandardError => e
    handle_request_error(e)
  end
end

#request_options(method, _path, data, options) ⇒ Object



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

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

#sandbox_auth_options(options) ⇒ Object



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

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