openbill-ruby 0.1.8 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/openbill/registry.rb +5 -5
- data/lib/openbill/service.rb +25 -33
- data/lib/openbill/version.rb +1 -1
- data/sql/0_db.sql +5 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e426ce6297d3b3dc2b8d70e86fd26ca82e28ae51
|
4
|
+
data.tar.gz: 6688d100ecfac9d3a86718595185a1e0674f065c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 31f1bfbf3c1ebf97dba300457baffce74ec3657b4cc1634222569ffa476255f2922d0ac9c7189ff97ad3cc89e766a06e512cc8aef4af7bfb56c279bef4b0ff54
|
7
|
+
data.tar.gz: d9d9e8bc664a21e19836658176b1ab536780695b3c684f7ab2f77bc385b2f594ffc3edfa20d8516bc164a05b43d646fa2453020eb56ebadcabd646f5d0e6d329
|
data/lib/openbill/registry.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
module Openbill
|
2
2
|
class Registry
|
3
|
-
|
3
|
+
DEFAULT_CATEGORY = :common
|
4
4
|
AccountNotFound = Class.new StandardError
|
5
5
|
|
6
6
|
attr_reader :accounts
|
7
7
|
|
8
|
-
def initialize(service,
|
8
|
+
def initialize(service, category = DEFAULT_CATEGORY)
|
9
9
|
fail("Must be a Openbill::Service #{service}") unless service.is_a? Openbill::Service
|
10
10
|
@service = service
|
11
|
-
@
|
11
|
+
@category = category
|
12
12
|
@accounts = {}
|
13
13
|
yield self
|
14
14
|
@accounts.freeze
|
@@ -17,7 +17,7 @@ module Openbill
|
|
17
17
|
# Находит, или создает аккаунт с указанным именем
|
18
18
|
#
|
19
19
|
def define(name, details)
|
20
|
-
accounts[name] = service.account([
|
20
|
+
accounts[name] = service.account([category, name], details: details)
|
21
21
|
end
|
22
22
|
|
23
23
|
def [](name)
|
@@ -30,6 +30,6 @@ module Openbill
|
|
30
30
|
|
31
31
|
private
|
32
32
|
|
33
|
-
attr_reader :service, :
|
33
|
+
attr_reader :service, :category
|
34
34
|
end
|
35
35
|
end
|
data/lib/openbill/service.rb
CHANGED
@@ -1,30 +1,20 @@
|
|
1
1
|
require 'sequel'
|
2
|
-
require 'uri'
|
3
2
|
require 'money'
|
4
3
|
|
5
4
|
module Openbill
|
6
5
|
|
7
6
|
class Service
|
8
|
-
PROTO = 'obp'.freeze
|
9
|
-
HOST = 'local'.freeze
|
10
|
-
|
11
7
|
def initialize(config)
|
12
8
|
@config = config
|
13
9
|
@database = Openbill::Database.new config.database
|
14
10
|
end
|
15
11
|
|
16
|
-
|
17
|
-
"#{PROTO}://#{HOST}/#{resource}/#{id}"
|
18
|
-
end
|
19
|
-
|
20
|
-
# @param uri - uri аккаунта в виде строки: "odp://local/resource/id"
|
21
|
-
# или в виде массива [resource, id]
|
12
|
+
# @param ident - ident аккаунта в виде: [:category, :key]
|
22
13
|
#
|
23
14
|
# @param options - опции применяемые для создания аккаунта (см create_account)
|
24
15
|
#
|
25
|
-
def account(
|
26
|
-
|
27
|
-
account = get_account_by_uri(uri)
|
16
|
+
def account(ident, currency: nil, details: nil, meta: {})
|
17
|
+
account = get_account(ident)
|
28
18
|
currency ||= config.default_currency
|
29
19
|
|
30
20
|
if account.present?
|
@@ -33,42 +23,44 @@ module Openbill
|
|
33
23
|
return account
|
34
24
|
end
|
35
25
|
|
36
|
-
create_account(
|
26
|
+
create_account(ident, currency: currency, details: details, meta: meta)
|
37
27
|
end
|
38
28
|
|
39
29
|
def get_account_by_id(id)
|
40
30
|
Openbill::Account[id: id]
|
41
31
|
end
|
42
32
|
|
43
|
-
|
44
|
-
|
45
|
-
|
33
|
+
# @param ident - ident аккаунта в виде: [:category, :key]
|
34
|
+
def get_account(ident)
|
35
|
+
category, key = prepare_ident ident
|
36
|
+
Openbill::Account[category: category, key: key]
|
46
37
|
end
|
47
38
|
|
48
|
-
def create_account(
|
49
|
-
|
39
|
+
def create_account(ident, currency: nil, details: nil, meta: {})
|
40
|
+
category, key = prepare_ident ident
|
50
41
|
Openbill::Account.create(
|
51
|
-
|
42
|
+
category: category,
|
43
|
+
key: key,
|
52
44
|
details: details,
|
53
45
|
meta: meta,
|
54
46
|
amount_currency: currency || config.default_currency
|
55
47
|
)
|
56
48
|
end
|
57
49
|
|
58
|
-
# @param
|
59
|
-
|
50
|
+
# @param key - уникальный текстовый ключ транзакции
|
51
|
+
#
|
52
|
+
def make_transaction(from:, to:, amount:, key:, details: , meta: {})
|
60
53
|
account_from = get_account_id from
|
61
54
|
account_to = get_account_id to
|
62
55
|
|
63
56
|
amount = prepare_amount amount, account_from.amount_currency
|
64
|
-
uri = prepare_uri uri
|
65
57
|
|
66
58
|
Openbill::Transaction.create(
|
67
59
|
from_account_id: account_from.id,
|
68
60
|
to_account_id: account_to.id,
|
69
61
|
amount_cents: amount.cents,
|
70
62
|
amount_currency: amount.currency,
|
71
|
-
|
63
|
+
key: key,
|
72
64
|
details: details,
|
73
65
|
meta: meta
|
74
66
|
)
|
@@ -83,16 +75,21 @@ module Openbill
|
|
83
75
|
def get_account_id(account)
|
84
76
|
case account
|
85
77
|
when Fixnum
|
86
|
-
|
87
|
-
when
|
88
|
-
|
78
|
+
get_account_by_id(account)
|
79
|
+
when Array
|
80
|
+
get_account(account)
|
89
81
|
when Openbill::Account
|
90
82
|
account
|
91
83
|
else
|
92
|
-
fail "Unknown type of account #{account}. Must be Fixnum,
|
84
|
+
fail "Unknown type of account #{account}. Must be Fixnum, Array or Openbill::Account"
|
93
85
|
end
|
94
86
|
end
|
95
87
|
|
88
|
+
def prepare_ident(ident)
|
89
|
+
fail "ident has wrong size" unless ident.count == 2
|
90
|
+
return ident.first.to_s, ident.second.to_s
|
91
|
+
end
|
92
|
+
|
96
93
|
def prepare_amount(amount, account_currency)
|
97
94
|
if amount.is_a? Money
|
98
95
|
unless amount.currency == account_currency
|
@@ -105,10 +102,5 @@ module Openbill
|
|
105
102
|
|
106
103
|
Money.new(amount, account_currency)
|
107
104
|
end
|
108
|
-
|
109
|
-
def prepare_uri(uri)
|
110
|
-
uri = generate_uri uri.first, uri.second if uri.is_a? Array
|
111
|
-
URI(uri).to_s # Парсим и валидируем заодно
|
112
|
-
end
|
113
105
|
end
|
114
106
|
end
|
data/lib/openbill/version.rb
CHANGED
data/sql/0_db.sql
CHANGED
@@ -5,7 +5,8 @@ CREATE EXTENSION IF NOT EXISTS hstore WITH SCHEMA public;
|
|
5
5
|
|
6
6
|
CREATE TABLE OPENBILL_ACCOUNTS (
|
7
7
|
id BIGSERIAL PRIMARY KEY,
|
8
|
-
|
8
|
+
category character varying(256) not null default 'common',
|
9
|
+
key character varying(256) not null,
|
9
10
|
amount_cents numeric not null default 0,
|
10
11
|
amount_currency char(3) not null default 'USD',
|
11
12
|
details text,
|
@@ -18,7 +19,7 @@ CREATE TABLE OPENBILL_ACCOUNTS (
|
|
18
19
|
);
|
19
20
|
|
20
21
|
CREATE UNIQUE INDEX index_accounts_on_id ON OPENBILL_ACCOUNTS USING btree (id);
|
21
|
-
CREATE UNIQUE INDEX
|
22
|
+
CREATE UNIQUE INDEX index_accounts_on_key ON OPENBILL_ACCOUNTS USING btree (category, key);
|
22
23
|
CREATE INDEX index_accounts_on_meta ON OPENBILL_ACCOUNTS USING gin (meta);
|
23
24
|
CREATE INDEX index_accounts_on_created_at ON OPENBILL_ACCOUNTS USING btree (created_at);
|
24
25
|
|
@@ -30,14 +31,14 @@ CREATE TABLE OPENBILL_TRANSACTIONS (
|
|
30
31
|
to_account_id integer not null,
|
31
32
|
amount_cents numeric not null CONSTRAINT positive CHECK (amount_cents>0),
|
32
33
|
amount_currency char(3) not null,
|
33
|
-
|
34
|
+
key character varying(256) not null,
|
34
35
|
details text not null,
|
35
36
|
meta hstore not null default ''::hstore,
|
36
37
|
foreign key (from_account_id) REFERENCES OPENBILL_ACCOUNTS (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
|
37
38
|
foreign key (to_account_id) REFERENCES OPENBILL_ACCOUNTS (id)
|
38
39
|
);
|
39
40
|
|
40
|
-
CREATE UNIQUE INDEX
|
41
|
+
CREATE UNIQUE INDEX index_transactions_on_key ON OPENBILL_TRANSACTIONS USING btree (key);
|
41
42
|
CREATE INDEX index_transactions_on_meta ON OPENBILL_TRANSACTIONS USING gin (meta);
|
42
43
|
CREATE INDEX index_transactions_on_created_at ON OPENBILL_TRANSACTIONS USING btree (created_at);
|
43
44
|
|