Class: Bunq::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bunq/client.rb

Overview

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

Constant Summary collapse

APPLICATION_JSON =
'application/json'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Client

Returns a new instance of Client.



181
182
183
184
185
# File 'lib/bunq/client.rb', line 181

def initialize(configuration)
  fail ArgumentError, 'configuration is required' unless configuration

  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



179
180
181
# File 'lib/bunq/client.rb', line 179

def configuration
  @configuration
end

#current_sessionObject

Returns the value of attribute current_session.



178
179
180
# File 'lib/bunq/client.rb', line 178

def current_session
  @current_session
end

Instance Method Details

#attachment_public(id) ⇒ Object



191
192
193
# File 'lib/bunq/client.rb', line 191

def attachment_public(id)
  Bunq::AttachmentPublic.new(self, id)
end

#attachment_public_content(id) ⇒ Object

Returns the Bunq::AttachmentPublicContent represented by the given id



232
233
234
# File 'lib/bunq/client.rb', line 232

def attachment_public_content(id)
  with_session { Bunq::AttachmentPublicContent.new(self, id) }
end

#attachment_publicsObject



187
188
189
# File 'lib/bunq/client.rb', line 187

def attachment_publics
  Bunq::AttachmentPublics.new(self)
end

#avatar(id) ⇒ Object



199
200
201
# File 'lib/bunq/client.rb', line 199

def avatar(id)
  Bunq::Avatar.new(self, id)
end

#avatarsObject



195
196
197
# File 'lib/bunq/client.rb', line 195

def avatars
  Bunq::Avatars.new(self)
end

#create_sessionObject



259
260
261
# File 'lib/bunq/client.rb', line 259

def create_session
  session_servers.create
end

#current_session_user_idObject



298
299
300
# File 'lib/bunq/client.rb', line 298

def current_session_user_id
  current_session[2].first[1]['id']
end

#device_serversObject



211
212
213
# File 'lib/bunq/client.rb', line 211

def device_servers
  Bunq::DeviceServers.new(self)
end

#encryptorObject



278
279
280
# File 'lib/bunq/client.rb', line 278

def encryptor
  @encryptor ||= Encryptor.new(configuration.server_public_key)
end

#ensure_session!Object



255
256
257
# File 'lib/bunq/client.rb', line 255

def ensure_session!
  @current_session ||= configuration.session_cache.get { create_session } # rubocop:disable Naming/MemoizedInstanceVariableName
end

#headersObject



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/bunq/client.rb', line 282

def headers
  {
    Bunq::Header::ACCEPT => APPLICATION_JSON,
    Bunq::Header::CACHE_CONTROL => 'no-cache',
    Bunq::Header::CONTENT_TYPE => APPLICATION_JSON,
    Bunq::Header::USER_AGENT => configuration.user_agent,
    Bunq::Header::LANGUAGE => configuration.language,
    Bunq::Header::GEOLOCATION => configuration.geolocation,
    Bunq::Header::REGION => configuration.region,
  }.tap do |h|
    h[Bunq::Header::CLIENT_AUTH] = configuration.installation_token if configuration.installation_token

    h[Bunq::Header::CLIENT_AUTH] = current_session[1]['Token']['token'] if current_session
  end
end

#installation(id) ⇒ Object



207
208
209
# File 'lib/bunq/client.rb', line 207

def installation(id)
  Bunq::Installation.new(self, id)
end

#installationsObject



203
204
205
# File 'lib/bunq/client.rb', line 203

def installations
  Bunq::Installations.new(self)
end

#me_as_userObject

Returns the Bunq::User represented by the Bunq::Configuration.api_key



247
248
249
# File 'lib/bunq/client.rb', line 247

def me_as_user
  with_session { user(current_session_user_id) }
end

#me_as_user_companyObject

Returns the Bunq::UserCompany represented by the Bunq::Configuration.api_key



242
243
244
# File 'lib/bunq/client.rb', line 242

def me_as_user_company
  with_session { user_company(current_session_user_id) }
end

#me_as_user_personObject

Returns the Bunq::UserPerson represented by the Bunq::Configuration.api_key



237
238
239
# File 'lib/bunq/client.rb', line 237

def me_as_user_person
  with_session { user_person(current_session_user_id) }
end

#session_serversObject



215
216
217
# File 'lib/bunq/client.rb', line 215

def session_servers
  Bunq::SessionServers.new(self)
end

#signatureObject



274
275
276
# File 'lib/bunq/client.rb', line 274

def signature
  @signature ||= Signature.new(configuration.private_key, configuration.server_public_key)
end

#user(id) ⇒ Object



219
220
221
# File 'lib/bunq/client.rb', line 219

def user(id)
  Bunq::User.new(self, id)
end

#user_company(id) ⇒ Object



223
224
225
# File 'lib/bunq/client.rb', line 223

def user_company(id)
  Bunq::UserCompany.new(self, id)
end

#user_person(id) ⇒ Object



227
228
229
# File 'lib/bunq/client.rb', line 227

def user_person(id)
  Bunq::UserPerson.new(self, id)
end

#with_local_config {|configuration.dup| ... } ⇒ Object

Yields:



251
252
253
# File 'lib/bunq/client.rb', line 251

def with_local_config
  yield(configuration.dup)
end

#with_session(&block) ⇒ Object



263
264
265
266
267
268
269
270
271
272
# File 'lib/bunq/client.rb', line 263

def with_session(&block)
  retries ||= 0
  ensure_session!
  block.call
rescue UnauthorisedResponse => e
  configuration.session_cache.clear
  @current_session = nil
  retry if (retries += 1) < 2
  raise e
end