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.



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



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

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

#add_notification(notification) ⇒ Notification

Register a new notification.



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



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

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

#get_account(account_id) ⇒ Account

Retrieve specific account.



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

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

#get_account_balance(account_id) ⇒ AccountBalance

Retrieve balance of an account.



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

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

#get_bank(bank_id) ⇒ Bank

Retrieve specific bank



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.



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.



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

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

#get_transaction(account_id, transaction_id) ⇒ Transaction

Retrieve a specific transaction



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

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

#modify_account(account) ⇒ Account

Modify specific account



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

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

#modify_account_balance(account_id, account_balance) ⇒ AccountBalance

Modify balance or account limits



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

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

#modify_bank(bank) ⇒ Bank

Modify bank



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.



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



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

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

#modify_user(user) ⇒ User

Modify the current user



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.



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)



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

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

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

Helper method for making a REST request.



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



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

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

#remove_bank_pin(bank) ⇒ nil

Remove stored PIN from bank



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.



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



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

def remove_payment(payment)
  query_api "/rest/accounts/#{payment.account_id}/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



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.account_id}/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.



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)



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/#{account_id}/transactions?") + URI.encode_www_form(data), nil, "GET", "transactions"
end

#userUser

Retrieve current User



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

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