Class: Openbill::Service
- Inherits:
-
Object
- Object
- Openbill::Service
- Defined in:
- lib/openbill/service.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#database ⇒ Object
readonly
Returns the value of attribute database.
Instance Method Summary collapse
- #account(ident, currency: nil, details: nil, meta: {}) ⇒ Object
- #account_transactions(ident) ⇒ Object
-
#accounts ⇒ Object
Return accounts repositiory (actualy sequel dataset).
- #categories ⇒ Object
- #create_account(ident, currency: nil, details: nil, meta: {}) ⇒ Object
- #get_account(ident) ⇒ Object
- #get_account_by_id(id) ⇒ Object
-
#initialize(config) ⇒ Service
constructor
A new instance of Service.
- #make_transaction(from:, to:, amount:, key:, details:, meta: {}) ⇒ Object
Constructor Details
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
7 8 9 |
# File 'lib/openbill/service.rb', line 7 def config @config end |
#database ⇒ Object (readonly)
Returns the value of attribute database.
7 8 9 |
# File 'lib/openbill/service.rb', line 7 def database @database end |
Instance Method Details
#account(ident, currency: nil, details: nil, meta: {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/openbill/service.rb', line 32 def account(ident, currency: nil, details: nil, meta: {}) account = get_account(ident) currency ||= config.default_currency if account.present? fail "Account currency is wrong #{account.amount_currency} <> #{currency}" unless account.amount_currency == currency # TODO update details and meta return account end create_account(ident, currency: currency, details: details, meta: ) end |
#account_transactions(ident) ⇒ Object
66 67 68 69 70 |
# File 'lib/openbill/service.rb', line 66 def account_transactions(ident) account = ident.is_a?(Openbill::Account) ? ident : get_account(ident) Openbill::Transaction .where('from_account_id = ? or to_account_id = ?', account.id, account.id) end |
#accounts ⇒ Object
Return accounts repositiory (actualy sequel dataset)
24 25 26 |
# File 'lib/openbill/service.rb', line 24 def accounts Openbill::Account.dataset end |
#categories ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/openbill/service.rb', line 14 def categories database .db[ACCOUNTS_TABLE_NAME] .group_and_count(:category) .all .map { |raw| Category.new name: raw[:category], accounts_count: raw[:count] } end |
#create_account(ident, currency: nil, details: nil, meta: {}) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/openbill/service.rb', line 55 def create_account(ident, currency: nil, details: nil, meta: {}) category, key = prepare_ident ident Openbill::Account.create( category: category, key: key, details: details, meta: , amount_currency: currency || config.default_currency ) end |
#get_account(ident) ⇒ Object
50 51 52 53 |
# File 'lib/openbill/service.rb', line 50 def get_account(ident) category, key = prepare_ident ident Openbill::Account[category: category, key: key] end |
#get_account_by_id(id) ⇒ Object
45 46 47 |
# File 'lib/openbill/service.rb', line 45 def get_account_by_id(id) Openbill::Account[id: id] end |
#make_transaction(from:, to:, amount:, key:, details:, meta: {}) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/openbill/service.rb', line 74 def make_transaction(from:, to:, amount:, key:, details: , meta: {}) account_from = get_account_id from account_to = get_account_id to amount = prepare_amount amount, account_from.amount_currency Openbill::Transaction.create( from_account_id: account_from.id, to_account_id: account_to.id, amount_cents: amount.cents, amount_currency: amount.currency, key: key, details: details, meta: ) end |