Class: Openbill::Service

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Service

Returns a new instance of Service.



9
10
11
12
# File 'lib/openbill/service.rb', line 9

def initialize(config)
  @config = config
  @database = Openbill::Database.new config.database
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/openbill/service.rb', line 7

def config
  @config
end

#databaseObject (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

Parameters:

  • ident
    • ident аккаунта в виде: [:category, :key]

  • options
    • опции применяемые для создания аккаунта (см create_account)



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/openbill/service.rb', line 32

def (ident, currency: nil, details: nil, meta: {})
   = (ident)
  currency ||= config.default_currency

  if .present?
    fail "Account currency is wrong #{.amount_currency} <> #{currency}" unless .amount_currency == currency
    # TODO update details and meta
    return 
  end

  (ident, currency: currency, details: details, meta: meta)
end

#account_transactions(ident) ⇒ Object



66
67
68
69
70
# File 'lib/openbill/service.rb', line 66

def (ident)
   = ident.is_a?(Openbill::Account) ? ident : (ident)
  Openbill::Transaction
    .where('from_account_id = ? or to_account_id = ?', .id, .id)
end

#accountsObject

Return accounts repositiory (actualy sequel dataset)



24
25
26
# File 'lib/openbill/service.rb', line 24

def accounts
  Openbill::Account.dataset
end

#categoriesObject



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 (ident, currency: nil, details: nil, meta: {})
  category, key = prepare_ident ident
  Openbill::Account.create(
    category:        category,
    key:             key,
    details:         details,
    meta:            meta,
    amount_currency: currency || config.default_currency
  )
end

#get_account(ident) ⇒ Object

Parameters:

  • ident
    • ident аккаунта в виде: [:category, :key]



50
51
52
53
# File 'lib/openbill/service.rb', line 50

def (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 (id)
  Openbill::Account[id: id]
end

#make_transaction(from:, to:, amount:, key:, details:, meta: {}) ⇒ Object

Parameters:

  • key
    • уникальный текстовый ключ транзакции



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: {})
   =  from
   =  to

  amount = prepare_amount amount, .amount_currency

  Openbill::Transaction.create(
    from_account_id: .id,
    to_account_id:   .id,
    amount_cents:    amount.cents,
    amount_currency: amount.currency,
    key:             key,
    details:         details,
    meta:            meta
  )
end