Class: Figo::Session

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

Overview

Represents a user-bound connection to the figo Connect API and allows access to the user’s data.

Instance Method Summary collapse

Constructor Details

#initialize(access_token) ⇒ Session

Create session object with access token.

Parameters:

  • access_token (String)

    the access token



219
220
221
222
# File 'lib/figo.rb', line 219

def initialize(access_token)
  @access_token = access_token
  @https = HTTPS.new("figo-#{access_token}")
end

Instance Method Details

#accountsArray

Retrieve all accounts

Returns:

  • (Array)

    an array of ‘Account` objects, one for each account the user has granted the app access



291
292
293
# File 'lib/figo.rb', line 291

def accounts
  query_api_object Account, "/rest/accounts", nil, "GET", "accounts"
end

#add_notification(notification) ⇒ Notification

Register a new notification.

Parameters:

  • notification (Notification)

    notification to be crated. It should not have a notification_id set.

Returns:



416
417
418
# File 'lib/figo.rb', line 416

def add_notification(notification)
  query_api_object Notification, "/rest/notifications", notification.dump(), "POST"
end

#add_payment(payment) ⇒ Payment

Create new payment

Parameters:

  • payment (Payment)

    payment object to be created. It should not have a payment_id set.

Returns:

  • (Payment)

    newly created ‘Payment` object



456
457
458
# File 'lib/figo.rb', line 456

def add_payment(payment)
  query_api_object Payment, "/rest/accounts/#{payment.}/payments", payment.dump(), "POST"
end

#get_account(account_id) ⇒ Account

Retrieve specific account.

Parameters:

  • account_id (String)

    ID of the account to be retrieved.

Returns:



299
300
301
# File 'lib/figo.rb', line 299

def ()
  query_api_object Account, "/rest/accounts/#{}"
end

#get_account_balance(account_id) ⇒ AccountBalance

Retrieve balance of an account.

Returns:



321
322
323
# File 'lib/figo.rb', line 321

def ()
  query_api_object AccountBalance, "/rest/accounts/#{}/balance"
end

#get_bank(bank_id) ⇒ Bank

Retrieve specific bank

Returns:

  • (Bank)

    bank object



337
338
339
# File 'lib/figo.rb', line 337

def get_bank(bank_id)
  query_api_object Bank, "/rest/banks/#{bank_id}"
end

#get_notification(notification_id) ⇒ Notification

Retrieve specific notification.

Parameters:

  • notification_id (String)

    ID of the notification to be retrieved

Returns:

  • (Notification)

    ‘Notification` object for the respective notification



408
409
410
# File 'lib/figo.rb', line 408

def get_notification(notification_id)
  query_api_object Notification, "/rest/notifications/#{notification_id}"
end

#get_payment(account_id, payment_id) ⇒ Payment

Retrieve specific payment.

Parameters:

  • account_id (String)

    ID for the account on which the payment to be retrieved was created

  • payment_id (String)

    ID of the notification to be retrieved

Returns:

  • (Payment)

    ‘Payment` object for the respective payment



448
449
450
# File 'lib/figo.rb', line 448

def get_payment(, payment_id)
  query_api_object Payment, "/rest/accounts/#{}/payments/#{payment_id}"
end

#get_transaction(account_id, transaction_id) ⇒ Transaction

Retrieve a specific transaction

Parameters:

  • account_id (String)

    ID of the account on which the transaction occured

  • transaction_id (String)

    ID of the transaction to be retrieved

Returns:



379
380
381
# File 'lib/figo.rb', line 379

def get_transaction(, transaction_id)
  query_api_object Transaction, "/rest/accounts/#{}/transactions/#{transaction_id}"
end

#modify_account(account) ⇒ Account

Modify specific account

Parameters:

  • account (Account)

    modified account to be saved

Returns:

  • (Account)

    modified account returned by the server



307
308
309
# File 'lib/figo.rb', line 307

def ()
  query_api_object Account, "/rest/accounts/#{.}", .dump(), "PUT"
end

#modify_account_balance(account_id, account_balance) ⇒ AccountBalance

Modify balance or account limits

Parameters:

  • account_id (String)

    ID of the account which balance should be modified

  • account_balance (AccountBalance)

    modified AccountBalance to be saved

Returns:



330
331
332
# File 'lib/figo.rb', line 330

def (, )
  query_api_object AccountBalance, "/rest/accounts/#{}/balance", .dump(), "PUT"
end

#modify_bank(bank) ⇒ Bank

Modify bank

Parameters:

  • bank (Bank)

    modified bank object

Returns:

  • (Bank)

    modified bank object returned by server



345
346
347
# File 'lib/figo.rb', line 345

def modify_bank(bank)
  query_api_object Bank, "/rest/banks/#{bank.bank_id}", bank.dump(), "PUT"
end

#modify_notification(notification) ⇒ Notification

Modify notification.

Parameters:

  • notification (Notification)

    modified notification object

Returns:

  • (Notification)

    modified notification returned by server



424
425
426
# File 'lib/figo.rb', line 424

def modify_notification(notification)
  query_api_object Notification, "/rest/notifications/#{notification.notification_id}", notification.dump(), "PUT"
end

#modify_payment(payment) ⇒ Payment

Modify payment

Parameters:

  • payment (Payment)

    modified payment object

Returns:

  • (Payment)

    modified payment object



464
465
466
# File 'lib/figo.rb', line 464

def modify_payment(payment)
  query_api_object Payment, "/rest/accounts/#{payment.}/payments/#{payment.payment_id}", payment.dump(), "PUT"
end

#modify_user(user) ⇒ User

Modify the current user

Parameters:

  • user (User)

    the modified user object to be saved

Returns:

  • (User)

    the modified user returned



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

def modify_user(user)
  query_api_object User, "/rest/user", user.dump(), "PUT"
end

#notificationsNotification

Retrieve list of registered notifications.

Returns:

  • (Notification)

    an array of ‘Notification` objects, one for each registered notification



400
401
402
# File 'lib/figo.rb', line 400

def notifications
  query_api_object Notification, "/rest/notifications", nil, "GET", "notifications"
end

#payments(account_id = nil) ⇒ Payment

Retrieve list of all payments (on all accounts or one)

Parameters:

  • account_id (String) (defaults to: nil)

    ID of the account for whicht to list the payments

Returns:

  • (Payment)

    an array of ‘Payment` objects, one for each payment



439
440
441
# File 'lib/figo.rb', line 439

def payments( = nil)
  query_api_object Payment, .nil? ? "/rest/payments" : "/rest/accounts/#{}/payments", nil, "GET", "payments"
end

#query_api(path, data = nil, method = "GET") ⇒ Hash

Helper method for making a REST request.

Parameters:

  • path (String)

    the URL path on the server

  • data (hash) (defaults to: nil)

    this optional object will be used as JSON-encoded POST content.

  • method (String) (defaults to: "GET")

    the HTTP method

Returns:

  • (Hash)

    JSON response



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/figo.rb', line 230

def query_api(path, data=nil, method="GET") # :nodoc:
  uri = URI("https://#{$api_endpoint}#{path}")

  # Setup HTTP request.
  request = case method
    when "POST"
      Net::HTTP::Post.new(path)
    when "PUT"
      Net::HTTP::Put.new(path)
    when "DELETE"
      Net::HTTP::Delete.new(path)
    else
      Net::HTTP::Get.new(path)
  end

  request["Authorization"] = "Bearer #{@access_token}"
  request["Accept"] = "application/json"
  request["Content-Type"] = "application/json"
  request["User-Agent"] =  "ruby-figo"
  request.body = JSON.generate(data) unless data.nil?

  # Send HTTP request.
  response = @https.request(uri, request)

  # Evaluate HTTP response.
  return nil if response.nil?
  return nil if response.body.nil?
  return response.body == "" ? nil : JSON.parse(response.body)
end

#query_api_object(type, path, data = nil, method = "GET", array_name = nil) ⇒ Object

:nodoc:



260
261
262
263
264
265
# File 'lib/figo.rb', line 260

def query_api_object(type, path, data=nil, method="GET", array_name=nil) # :nodoc:
  response = query_api path, data, method
  return nil if response.nil?
  return type.new(self, response) if array_name.nil?
  return response[array_name].map {|entry| type.new(self, entry)}
end

#remove_account(account) ⇒ Object

Remove specific account

Parameters:

  • account (Account, String)

    the account to be removed or its ID



314
315
316
# File 'lib/figo.rb', line 314

def ()
  query_api .is_a?(String) ? "/rest/accounts/#{}" : "/rest/accounts/#{.}", nil, "DELETE"
end

#remove_bank_pin(bank) ⇒ nil

Remove stored PIN from bank

Parameters:

  • bank (Bank, String)

    the bank whose stored PIN should be removed or its ID

Returns:

  • (nil)


353
354
355
# File 'lib/figo.rb', line 353

def remove_bank_pin(bank)
  query_app bank.is_a?(String) ? "/rest/banks/#{bank}/submit": "/rest/banks/#{bank.bank_id}/submit", nil, "POST"
end

#remove_notification(notification) ⇒ Object

Unregister notification.

Parameters:

  • notification (Notification, String)

    notification object which should be deleted or its ID



431
432
433
# File 'lib/figo.rb', line 431

def remove_notification(notification)
  query_api notification.is_a?(String) ? "/rest/notifications/#{notification}" : "/rest/notifications/#{notification.notification_id}", nil, "DELETE"
end

#remove_payment(payment) ⇒ Object

Remove payment

Parameters:

  • payment (Payment, String)

    payment object which should be removed



485
486
487
# File 'lib/figo.rb', line 485

def remove_payment(payment)
  query_api "/rest/accounts/#{payment.}/payments/#{payment.payment_id}", nil, "DELETE"
end

#remove_userObject

Remove the current user Note: this has immidiate effect and you wont be able to interact with the user after this call



284
285
286
# File 'lib/figo.rb', line 284

def remove_user
  query_api "/rest/user", nil, "DELETE"
end

#submit_payment(payment, tan_scheme_id, state, redirect_uri = nil) ⇒ String

Submit payment

Parameters:

  • tan_scheme_id (String)

    TAN scheme ID of user-selected TAN scheme

  • state (String)

    Any kind of string that will be forwarded in the callback response message

  • redirect_uri (String) (defaults to: nil)

    At the end of the submission process a response will be sent to this callback URL

Returns:

  • (String)

    the URL to be opened by the user for the TAN process



474
475
476
477
478
479
480
# File 'lib/figo.rb', line 474

def submit_payment(payment, tan_scheme_id, state, redirect_uri = nil)
  params = {"tan_scheme_id" => tan_scheme_id, "state" => state}
  params['redirect_uri'] = redirect_uri unless redirect_uri.nil?

  response = query_api "/rest/accounts/#{payment.}/payments/#{payment.payment_id}/submit", params, "POST"
  return "https://#{$api_endpoint}/task/start?id=#{response["task_token"]}"
end

#sync_url(redirect_uri, state, if_not_synced_since = 0) ⇒ String

Retrieve the URL a user should open in the web browser to start the synchronization process.

Parameters:

  • redirect_uri (String)

    the user will be redirected to this URL after the process completes

  • state (String)

    this string will be passed on through the complete synchronization process and to the redirect target at the end. It should be used to validated the authenticity of the call to the redirect URL

  • if_not_synced_since (Integer) (defaults to: 0)

    if this parameter is set, only those accounts will be synchronized, which have not been synchronized within the specified number of minutes.

Returns:

  • (String)

    the URL to be opened by the user.



392
393
394
395
# File 'lib/figo.rb', line 392

def sync_url(redirect_uri, state, if_not_synced_since = 0)
  response = query_api "/rest/sync", {"redirect_uri" => redirect_uri, "state" => state, "if_not_synced_since" => if_not_synced_since}, "POST"
  return "https://#{$api_endpoint}/task/start?id=#{response["task_token"]}"
end

#transactions(account_id = nil, since = nil, count = 1000, offset = 0, include_pending = false) ⇒ Array

Retrieve list of transactions (on all or a specific account)

Parameters:

  • account_id (String) (defaults to: nil)

    ID of the account for which to list the transactions

  • since (String, Date) (defaults to: nil)

    this parameter can either be a transaction ID or a date

  • count (Integer) (defaults to: 1000)

    limit the number of returned transactions

  • offset (Integer) (defaults to: 0)

    which offset into the result set should be used to determin the first transaction to return (useful in combination with count)

  • include_pending (Boolean) (defaults to: false)

    this flag indicates whether pending transactions should be included in the response; pending transactions are always included as a complete set, regardless of the ‘since` parameter

Returns:

  • (Array)

    an array of ‘Transaction` objects, one for each transaction of the user



367
368
369
370
371
372
# File 'lib/figo.rb', line 367

def transactions( = nil, since = nil, count = 1000, offset = 0, include_pending = false)
  data = {"count" => count.to_s, "offset" => offset.to_s, "include_pending" => include_pending ? "1" : "0"}
  data["since"] = ((since.is_a?(Date) ? since.to_s : since) unless since.nil?)

  query_api_object Transaction, (.nil? ? "/rest/transactions?" : "/rest/accounts/#{}/transactions?") + URI.encode_www_form(data), nil, "GET", "transactions"
end

#userUser

Retrieve current User

Returns:

  • (User)

    the current user



270
271
272
# File 'lib/figo.rb', line 270

def user
  query_api_object User, "/rest/user"
end