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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a75471ea494c07660f3d35539b4ea8cb609dc5cb
4
- data.tar.gz: 64f789bd6587756d24d06eb07fd3953d58a82238
3
+ metadata.gz: e426ce6297d3b3dc2b8d70e86fd26ca82e28ae51
4
+ data.tar.gz: 6688d100ecfac9d3a86718595185a1e0674f065c
5
5
  SHA512:
6
- metadata.gz: 50da8cc7f4b6a391317b3e25275ee97c26f97f4fb78b68c00db17f77c77003399a83b5a661096926c667c3863697bfe02169f1f047261ca07bd31cef102a2165
7
- data.tar.gz: d3ca24a7c5494219153f6ddf90c2a51043521411dbd5ed1858da8b5c5597ab934d4f9a3134577246a73f2c9b9dabdc40ce8ebe2f819b3d5104d765ec7f67606d
6
+ metadata.gz: 31f1bfbf3c1ebf97dba300457baffce74ec3657b4cc1634222569ffa476255f2922d0ac9c7189ff97ad3cc89e766a06e512cc8aef4af7bfb56c279bef4b0ff54
7
+ data.tar.gz: d9d9e8bc664a21e19836658176b1ab536780695b3c684f7ab2f77bc385b2f594ffc3edfa20d8516bc164a05b43d646fa2453020eb56ebadcabd646f5d0e6d329
@@ -1,14 +1,14 @@
1
1
  module Openbill
2
2
  class Registry
3
- SYSTEM_NS = :system
3
+ DEFAULT_CATEGORY = :common
4
4
  AccountNotFound = Class.new StandardError
5
5
 
6
6
  attr_reader :accounts
7
7
 
8
- def initialize(service, system_ns = SYSTEM_NS)
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
- @system_ns = system_ns
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([system_ns, name], details: details)
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, :system_ns
33
+ attr_reader :service, :category
34
34
  end
35
35
  end
@@ -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
- def generate_uri(resource, id)
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(uri, currency: nil, details: nil, meta: {})
26
- uri = prepare_uri uri
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(uri, currency: currency, details: details, meta: meta)
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
- def get_account_by_uri(uri)
44
- uri = prepare_uri uri
45
- Openbill::Account[uri: uri]
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(uri, currency: nil, details: nil, meta: {})
49
- uri = prepare_uri uri
39
+ def create_account(ident, currency: nil, details: nil, meta: {})
40
+ category, key = prepare_ident ident
50
41
  Openbill::Account.create(
51
- uri: uri,
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 uri - уникальный uri транзакции
59
- def make_transaction(from:, to:, amount:, uri:, details: , meta: {})
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
- uri: uri,
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
- get_account_by_uri(account)
87
- when String, Array
88
- get_account_by_uri(account)
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, String, Array or Openbill::Account"
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
@@ -1,3 +1,3 @@
1
1
  module Openbill
2
- VERSION = '0.1.8'
2
+ VERSION = '0.1.9'
3
3
  end
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
- uri character varying(2048) not null,
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 index_accounts_on_uri ON OPENBILL_ACCOUNTS USING btree (uri);
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
- uri character varying(2048) not null,
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 index_transactions_on_uri ON OPENBILL_TRANSACTIONS USING btree (uri);
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openbill-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danil Pismenny