-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.rb
213 lines (181 loc) · 5.83 KB
/
client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
require 'openssl'
require 'base64'
require_relative './version'
require_relative './resource'
require_relative './installations'
require_relative './installation'
require_relative './device_servers'
require_relative './session_servers'
require_relative './user'
require_relative './user_company'
require_relative './monetary_account'
require_relative './monetary_accounts'
require_relative './payment'
require_relative './payments'
require_relative './signature'
require_relative './attachment_public_content.rb'
##
# Usage
#
# Bunq.configure do |config|
# config.api_key = 'YOUR_APIKEY'
# config.installation_token = 'YOUR_INSTALLATION_TOKEN'
# config.private_key = 'YOUR PRIVATE KEY'
# config.server_public_key = 'SERVER PUBLIC KEY'
# end
#
# client = Bunq.client
# number_of_accounts = client.me_as_user.monetary_accounts.index.to_a.count
# puts "User has #{number_of_accounts} accounts"
#
module Bunq
class << self
attr_accessor :configuration
def configure
self.configuration ||= Configuration.new
yield(configuration)
configuration.base_url = Configuration::SANDBOX_BASE_URL if configuration.sandbox
fail 'api_key is mandatory' unless self.configuration.api_key
end
def reset_configuration
self.configuration = nil
end
##
# Returns a new instance of +Client+ with the current +configuration+.
#
def client
fail "No configuration! Call Bunq.configure first." unless configuration
Client.new(configuration)
end
##
# Returns a new instance of +Signature+
#
def signature
fail "No configuration! Call Bunq.configure first." unless configuration
Signature.new(configuration.private_key, configuration.server_public_key)
end
end
##
# Configuration object for connecting to the bunq api
#
class Configuration
SANDBOX_BASE_URL = 'https://sandbox.public.api.bunq.com'
PRODUCTION_BASE_URL = 'https://api.bunq.com'
DEFAULT_LANGUAGE = 'nl_NL'
DEFAULT_REGION = 'nl_NL'
DEFAULT_GEOLOCATION = '0 0 0 0 000'
DEFAULT_USER_AGENT = "bunq ruby client #{Bunq::VERSION}"
DEFAULT_TIMEOUT = 60
# Base url for the bunq api. Defaults to +PRODUCTION_BASE_URL+
attr_accessor :base_url,
# Flag to set to connect to sandbox. Defaults to +false+.
# If set to +true+ you must also specify +sandbox_user+
# and +sandbox_password+
:sandbox,
# The username for connecting to the sandbox
:sandbox_user,
# The password for connecting to the sandbox
:sandbox_password,
# Your installation token obtained from bunq
:installation_token,
# Your api key obtained from bunq
:api_key,
# Your language. Defaults to +DEFAULT_LANGUAGE+
:language,
# Your region. Defaults to +DEFAULT_REGION+
:region,
# Your geolocation. Defaults to +DEFAULT_GEOLOCATION+
:geolocation,
# Arbitrary user agent to connect to bunq. Defaults to +DEFAULT_USER_AGENT+
:user_agent,
# Flag to set when you want to disable the signature
# retrieved from bunq. Mainly useful for testing.
# Defaults to +false+
:disable_response_signature_verification,
# The private key for signing the request
:private_key,
# The public key of this installation for verifying the response
:server_public_key,
# Timeout in seconds to wait for bunq api. Defaults to +DEFAULT_TIMEOUT+
:timeout
def initialize
@sandbox = false
@base_url = PRODUCTION_BASE_URL
@language = DEFAULT_LANGUAGE
@region = DEFAULT_REGION
@geolocation = DEFAULT_GEOLOCATION
@user_agent = DEFAULT_USER_AGENT
@disable_response_signature_verification = false
@timeout = DEFAULT_TIMEOUT
end
end
##
# The Bunq::Client is the adapter for the Bunq Public Api (doc.bunq.com)
#
# An instance of a +Client+ can be obtained via +Bunq.client+
class Client
attr_accessor :current_session
attr_reader :configuration
def initialize(configuration)
@configuration = configuration
end
def installations
Bunq::Installations.new(self)
end
def installation(id)
Bunq::Installation.new(self, id)
end
def device_servers
Bunq::DeviceServers.new(self)
end
def session_servers
Bunq::SessionServers.new(self)
end
def user(id)
Bunq::User.new(self, id)
end
def user_company(id)
Bunq::UserCompany.new(self, id)
end
# Returns the +Bunq::AttachmentPublicContent+ represented by the given id
def attachment_public_content(id)
with_session { Bunq::AttachmentPublicContent.new(self, id) }
end
# Returns the +Bunq::UserCompany+ represented by the +Bunq::Configuration.api_key+
def me_as_user_company
with_session { user_company(current_session_user_id) }
end
# Returns the +Bunq::User+ represented by the +Bunq::Configuration.api_key+
def me_as_user
with_session { user(current_session_user_id) }
end
def ensure_session!
@current_session ||= session_servers.create
end
def with_session(&block)
ensure_session!
block.call
end
def headers
{
'Accept': 'application/json',
'Cache-Control': 'no-cache',
'Content-Type': 'application/json',
'User-Agent': configuration.user_agent,
'X-Bunq-Language': configuration.language,
'X-Bunq-Geolocation': configuration.geolocation,
'X-Bunq-Region': configuration.region,
}.tap do |h|
if configuration.installation_token
h[:'X-Bunq-Client-Authentication'] = configuration.installation_token
end
if current_session
h[:'X-Bunq-Client-Authentication'] = current_session[1]['Token']['token']
end
end
end
def current_session_user_id
current_session[2].first[1]['id']
end
end
end