class Bunq::Client

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

Constants

APPLICATION_JSON

Attributes

configuration[R]
current_session[RW]

Public Class Methods

new(configuration) click to toggle source
# File lib/bunq/client.rb, line 181
def initialize(configuration)
  fail ArgumentError, 'configuration is required' unless configuration

  @configuration = configuration
end

Public Instance Methods

attachment_public(id) click to toggle source
# File lib/bunq/client.rb, line 191
def attachment_public(id)
  Bunq::AttachmentPublic.new(self, id)
end
attachment_public_content(id) click to toggle source

Returns the Bunq::AttachmentPublicContent represented by the given id

# File lib/bunq/client.rb, line 232
def attachment_public_content(id)
  with_session { Bunq::AttachmentPublicContent.new(self, id) }
end
attachment_publics() click to toggle source
# File lib/bunq/client.rb, line 187
def attachment_publics
  Bunq::AttachmentPublics.new(self)
end
avatar(id) click to toggle source
# File lib/bunq/client.rb, line 199
def avatar(id)
  Bunq::Avatar.new(self, id)
end
avatars() click to toggle source
# File lib/bunq/client.rb, line 195
def avatars
  Bunq::Avatars.new(self)
end
create_session() click to toggle source
# File lib/bunq/client.rb, line 259
def create_session
  session_servers.create
end
current_session_user_id() click to toggle source
# File lib/bunq/client.rb, line 298
def current_session_user_id
  current_session[2].first[1]['id']
end
device_servers() click to toggle source
# File lib/bunq/client.rb, line 211
def device_servers
  Bunq::DeviceServers.new(self)
end
encryptor() click to toggle source
# File lib/bunq/client.rb, line 278
def encryptor
  @encryptor ||= Encryptor.new(configuration.server_public_key)
end
ensure_session!() click to toggle source
# File lib/bunq/client.rb, line 255
def ensure_session!
  @current_session ||= configuration.session_cache.get { create_session } # rubocop:disable Naming/MemoizedInstanceVariableName
end
headers() click to toggle source
# 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) click to toggle source
# File lib/bunq/client.rb, line 207
def installation(id)
  Bunq::Installation.new(self, id)
end
installations() click to toggle source
# File lib/bunq/client.rb, line 203
def installations
  Bunq::Installations.new(self)
end
me_as_user() click to toggle source

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

# File lib/bunq/client.rb, line 247
def me_as_user
  with_session { user(current_session_user_id) }
end
me_as_user_company() click to toggle source

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

# File lib/bunq/client.rb, line 242
def me_as_user_company
  with_session { user_company(current_session_user_id) }
end
me_as_user_person() click to toggle source

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

# File lib/bunq/client.rb, line 237
def me_as_user_person
  with_session { user_person(current_session_user_id) }
end
session_servers() click to toggle source
# File lib/bunq/client.rb, line 215
def session_servers
  Bunq::SessionServers.new(self)
end
signature() click to toggle source
# File lib/bunq/client.rb, line 274
def signature
  @signature ||= Signature.new(configuration.private_key, configuration.server_public_key)
end
user(id) click to toggle source
# File lib/bunq/client.rb, line 219
def user(id)
  Bunq::User.new(self, id)
end
user_company(id) click to toggle source
# File lib/bunq/client.rb, line 223
def user_company(id)
  Bunq::UserCompany.new(self, id)
end
user_person(id) click to toggle source
# File lib/bunq/client.rb, line 227
def user_person(id)
  Bunq::UserPerson.new(self, id)
end
with_local_config() { |dup| ... } click to toggle source
# File lib/bunq/client.rb, line 251
def with_local_config
  yield(configuration.dup)
end
with_session(&block) click to toggle source
# 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