openbill-ruby 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2e8d41cf49c4b4c79c67648a13bb8d620021fdff
4
- data.tar.gz: cefc16e4aa3aaa1a0f64c1da0048e9ea30a7ff8b
3
+ metadata.gz: 1e596b1c45d1e6de254938e905bbf454058bf7df
4
+ data.tar.gz: 9b6bfc5f5e89ac5c1479e3f3de6e4aba3ecc1619
5
5
  SHA512:
6
- metadata.gz: b2b78ea598a78806137e6844fb7fcd35526dcaac3791ac53c2cadc34dc06972a580c0908a2421a5466a79887d94c6e9e01f93e687a56c29f1c1d912e02caaa86
7
- data.tar.gz: 6053481084ad0b63af80a33c881e380281b054f1616f00a2c26e0bc2105b27b8a4fc813234378d3be5347a0d78c066ef0c9e8511ba9b40279d85149c018388fd
6
+ metadata.gz: 2a27ab2112aed4d8f0fbf228d5570cacdb7d1ea71eee4e9927e90e07b90f0dc4e9e4deea520b0dcdaf6dd46552df1abdbb609632051023d2f3817f8890294e55
7
+ data.tar.gz: 023a1cf6968b662d6337aff8aa0a2b97077a01b9d31601b5b9f5d2a7324af27a4b6396a033c8130a2e92f43f6765ea256e9184cc2bc360b875eb9080cd8c318c
data/.travis.yml CHANGED
@@ -1,4 +1,10 @@
1
1
  language: ruby
2
+ addons:
3
+ postgresql: "9.5"
4
+ services:
5
+ - postgresql
2
6
  rvm:
3
- - 2.2.2
7
+ - 2.2.1
8
+ sudo: false
9
+ cache: bundler
4
10
  before_install: gem install bundler -v 1.10.6
data/README.md CHANGED
@@ -20,50 +20,26 @@ Migrate database:
20
20
 
21
21
  Add database configurartion to `./config/initializers/openbill.rb`
22
22
 
23
- ```ruby
24
- Openbill.config.database = ActiveRecord::Base.connection.instance_variable_get('@config');
25
- ```
26
-
27
23
  ## Usage
28
24
 
29
25
  ### Создание системных счетов
30
26
 
31
-
32
- Рекомендую создать такой сервис для работы с системными счетами:
33
-
34
27
  ```ruby
35
- module Billing
36
- NS = :system
37
-
38
- class << self
39
- def payments
40
- account :payments, 'Счет с которого поступает оплата'
41
- end
28
+ Openbill.config.database =
29
+ ActiveRecord::Base.connection.instance_variable_get('@config');
42
30
 
43
- def subscriptions
44
- account :subscriptions, 'Абонентская плата'
45
- end
46
31
 
47
- private
48
-
49
- # Находит, или создает аккаунт с указанным именем
50
- #
51
- def account(path, details)
52
- # Создаем uri аккаунта из его названия
53
- uri = Openbill.generate_uri NS, path
54
-
55
- Openbill.get_account_by_uri(uri) ||
56
- Openbill.create_account(uri, details: details)
57
- end
58
- end
32
+ SystemRegistry = Openbill::Registry.new Openbill.current do |registry|
33
+ registry.define :payments, 'Счет с которого поступает оплата'
34
+ registry.define :subscriptions, 'Абонентская плата'
59
35
  end
60
36
  ```
61
37
 
62
- Таким образом в системе появляется два счета `Billing.payments` и `Billing.subscriptions`
38
+ Таким образом в системом реестре появляется два счета `SystemRegistry[:payments]` и `SystemRegistry[:subscriptions]`
63
39
 
64
40
  ### Поддержка биллинга со стороны клиента (автоматическое создание клиентского счета):
65
41
 
66
- ```
42
+ ```ruby
67
43
  module AccountBilling
68
44
  extend ActiveSupport::Concern
69
45
 
@@ -74,21 +50,7 @@ module AccountBilling
74
50
  delegate :amount, to: :billing_account
75
51
 
76
52
  def billing_account
77
- find_billing || attach_billing
78
- end
79
-
80
- private
81
-
82
- def account_uri
83
- Openbill.generate_uri :accounts, id
84
- end
85
-
86
- def find_billing
87
- Openbill.current.get_account_by_uri account_uri
88
- end
89
-
90
- def attach_billing
91
- Openbill.current.create_account account_uri
53
+ Openbill.current.account([:accounts, id])
92
54
  end
93
55
  end
94
56
  ```
@@ -102,10 +64,22 @@ end
102
64
 
103
65
  ```ruby
104
66
  Openbill.current.make_transaction(
67
+ // Счет списания
105
68
  from_account_id: Billing.payment.id,
69
+
70
+ // Счет зачисление
106
71
  to_account_id: user.billing_account.id,
107
- amount: Money(123),
72
+
73
+ // Уникальный идентификатор транзакции
74
+ uri: Openbill.current.generate_uri(:transactions, 123),
75
+
76
+ // Сумма транзакции. Валюта должна совпадать с обоими счетами
77
+ amount: Money(123, 'RUB'),
78
+
79
+ // Не обязательное текстовое описание транзакции
108
80
  details: 'Оплата такая-то',
81
+
82
+ // hash с данными транзакции для структурированного поиска в дальнейшем
109
83
  meta: { key: 'value' } // не обязательно
110
84
  )
111
85
  ```
data/lib/openbill.rb CHANGED
@@ -4,6 +4,8 @@ module Openbill
4
4
  autoload :Account, 'openbill/account'
5
5
  autoload :Transaction, 'openbill/transaction'
6
6
  autoload :Configuration, 'openbill/configuration'
7
+ autoload :Registry, 'openbill/registry'
8
+ autoload :Database, 'openbill/database'
7
9
 
8
10
  ACCOUNTS_TABLE_NAME = :openbill_accounts
9
11
  TRANSACTIONS_TABLE_NAME = :openbill_transactions
@@ -21,16 +23,10 @@ module Openbill
21
23
  Configuration.instance
22
24
  end
23
25
 
24
- def generate_uri(resource, id)
25
- "obp://local/#{resource}/#{id}"
26
- end
27
-
28
- def create_connection(&block)
29
- @create_connection_block = block
30
- end
31
-
32
26
  def current
33
- @current ||= Openbill::Service.new config.database
27
+ return @current if @current
28
+
29
+ @current = Openbill::Service.new config
34
30
  end
35
31
  end
36
32
  end
@@ -1,7 +1,15 @@
1
1
  require 'singleton'
2
2
 
3
3
  class Configuration
4
+ DEFAULT_CURRENCY = 'USD'.freeze
5
+
4
6
  include Singleton
5
7
 
6
8
  attr_accessor :database
9
+
10
+ attr_writer :default_currency
11
+
12
+ def default_currency
13
+ @default_currency || DEFAULT_CURRENCY
14
+ end
7
15
  end
@@ -0,0 +1,14 @@
1
+ module Openbill
2
+ class Database
3
+ MAX_CONNECTIONS = 10
4
+
5
+ def initialize(db_config)
6
+ @db_config = db_config
7
+ @db = Sequel.connect db_config, logger: logger, max_connections: MAX_CONNECTIONS
8
+ @db.extension :pagination
9
+ @db.extension :pg_hstore
10
+ end
11
+
12
+ delegate :logger, to: Rails
13
+ end
14
+ end
@@ -0,0 +1,36 @@
1
+ module Openbill
2
+ class Registry
3
+ SYSTEM_NS = :system
4
+ AccountNotFound = Class.new StandardError
5
+
6
+ def initialize(service, system_ns = SYSTEM_NS)
7
+ fail("Must be a Openbill::Service #{service}") unless service.is_a? Openbill::Service
8
+ @service = service
9
+ @system_ns = system_ns
10
+ @accounts = {}
11
+ yield self
12
+ @accounts.freeze
13
+ return @accounts
14
+ rescue
15
+ @system_ns = nil
16
+ end
17
+
18
+ # Находит, или создает аккаунт с указанным именем
19
+ #
20
+ def define(name, options)
21
+ @accounts[name] = service.account([system_ns, name], options)
22
+ end
23
+
24
+ def [](name)
25
+ @account[name]
26
+ end
27
+
28
+ def find(name)
29
+ [name] || raise(AccountNotFound, name)
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :service, :system_ns
35
+ end
36
+ end
@@ -3,25 +3,42 @@ require 'uri'
3
3
  require 'money'
4
4
 
5
5
  module Openbill
6
- MAX_CONNECTIONS = 10
7
- DEFAULT_CURRENCY = 'RUB'.freeze
8
6
 
9
7
  class Service
10
- def initialize(db_config)
11
- init_db db_config
8
+ PROTO = 'obp'.freeze
9
+ HOST = 'local'.freeze
10
+
11
+ def initialize(config)
12
+ @config = config
13
+ @database = Openbill::Database.new config.database
14
+ end
15
+
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]
22
+ #
23
+ # @param options - опции применяемые для создания аккаунта (см create_account)
24
+ #
25
+ def account(uri, options = {})
26
+ uri = prepare_uri uri
27
+ get_account_by_uri(uri) || create_account(uri, options)
12
28
  end
13
29
 
14
30
  def get_account_by_uri(uri)
31
+ uri = prepare_uri uri
15
32
  Openbill::Account[uri: uri]
16
33
  end
17
34
 
18
- def create_account(uri, currency: DEFAULT_CURRENCY, details: nil, meta: {})
35
+ def create_account(uri, currency: nil, details: nil, meta: {})
19
36
  uri = prepare_uri uri
20
37
  Openbill::Account.create(
21
38
  uri: uri,
22
39
  details: details,
23
40
  meta: meta,
24
- amount_currency: currency
41
+ amount_currency: currency || config.default_currency
25
42
  )
26
43
  end
27
44
 
@@ -43,25 +60,19 @@ module Openbill
43
60
 
44
61
  private
45
62
 
46
- attr_reader :db_config, :db
63
+ attr_reader :database, :config
47
64
 
48
65
  delegate :logger, to: Rails
49
66
 
50
67
  def prepare_amount(amount)
51
68
  return amount if amount.is_a? Money
52
- return Money.new(amount, DEFAULT_CURRENCY) if amount.is_a? Fixnum
69
+ return Money.new(amount, config.default_currency) if amount.is_a? Fixnum
53
70
  raise "amount parameter (#{amount}) must be a Money or a Fixnum"
54
71
  end
55
72
 
56
73
  def prepare_uri(uri)
74
+ uri = generate_uri uri.first, uri.second if uri.is_a? Array
57
75
  URI(uri).to_s # Парсим и валидируем заодно
58
76
  end
59
-
60
- def init_db(db_config)
61
- @db_config = db_config
62
- @db = Sequel.connect db_config, logger: logger, max_connections: MAX_CONNECTIONS
63
- @db.extension :pagination
64
- @db.extension :pg_hstore
65
- end
66
77
  end
67
78
  end
@@ -1,3 +1,3 @@
1
1
  module Openbill
2
- VERSION = '0.1.6'
2
+ VERSION = '0.1.7'
3
3
  end
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.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danil Pismenny
@@ -73,7 +73,9 @@ files:
73
73
  - lib/openbill.rb
74
74
  - lib/openbill/account.rb
75
75
  - lib/openbill/configuration.rb
76
+ - lib/openbill/database.rb
76
77
  - lib/openbill/engine.rb
78
+ - lib/openbill/registry.rb
77
79
  - lib/openbill/service.rb
78
80
  - lib/openbill/transaction.rb
79
81
  - lib/openbill/version.rb