openbill-ruby 0.1.0 → 0.1.5
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 +4 -4
- data/Gemfile +5 -0
- data/README.md +6 -0
- data/db/migrate/20160320150928_add_openbill.rb +20 -0
- data/lib/openbill/configuration.rb +7 -0
- data/lib/openbill/engine.rb +13 -0
- data/lib/openbill/service.rb +3 -7
- data/lib/openbill/version.rb +1 -1
- data/lib/openbill-ruby.rb +1 -0
- data/lib/openbill.rb +21 -5
- data/sql/0_db.sql +44 -0
- data/sql/1_trigger_disable_delete_account.sql +27 -0
- data/sql/2_trigger_disable_update_transaction.sql +11 -0
- data/sql/3_trigger_process_account_transaction.sql +40 -0
- metadata +9 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b81bdc0ec906d8dd0d12577772d4a6bd3c4eabce
|
4
|
+
data.tar.gz: f79967c83942d3f7e15df4cb5e62b2ba89048193
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bc1d4999a0428e9e23ca906417a0e9f7a3891699b1884636c79f9bbc02733f0265b7d01bb1587aa4c9335dea06618134ebbb5e8e8ba9f680239e690bdb98c795
|
7
|
+
data.tar.gz: ec4e573a1ff094828af7870f493bf252314d6133182f20e32c13f37ecba2d4a7dbd7692c2efde3a3f1bcc5161ea2f592cf05a7fcaca4da6422a16e5e04e263f7
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
class AddOpenbill < ActiveRecord::Migration
|
2
|
+
def up
|
3
|
+
Dir.entries(sql_dir).select{|f| File.file? sql_dir + f }.sort.each do |file|
|
4
|
+
say_with_time "Migrate with #{file}" do
|
5
|
+
execute File.read sql_dir + file
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def down
|
11
|
+
execute "DROP TABLE openbill_accounts CASCADE"
|
12
|
+
execute "DROP TABLE openbill_transactions CASCADE"
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def sql_dir
|
18
|
+
Openbill.root + '/sql/'
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Openbill
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
isolate_namespace Openbill
|
4
|
+
|
5
|
+
initializer 'append_migrations' do |app|
|
6
|
+
unless app.root.to_s.match root.to_s
|
7
|
+
config.paths["db/migrate"].expanded.each do |expanded_path|
|
8
|
+
app.config.paths["db/migrate"] << expanded_path
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/openbill/service.rb
CHANGED
@@ -7,10 +7,6 @@ module Openbill
|
|
7
7
|
DEFAULT_CURRENCY = 'RUB'.freeze
|
8
8
|
|
9
9
|
class Service
|
10
|
-
def self.rails_initialize
|
11
|
-
new ActiveRecord::Base.connection.instance_variable_get('@config')
|
12
|
-
end
|
13
|
-
|
14
10
|
def initialize(db_config)
|
15
11
|
init_db db_config
|
16
12
|
end
|
@@ -22,7 +18,7 @@ module Openbill
|
|
22
18
|
def create_account(uri, currency: DEFAULT_CURRENCY, details: nil, meta: {})
|
23
19
|
uri = prepare_uri uri
|
24
20
|
Openbill::Account.create(
|
25
|
-
uri:
|
21
|
+
uri: uri,
|
26
22
|
details: details,
|
27
23
|
meta: meta,
|
28
24
|
amount_currency: currency
|
@@ -34,12 +30,12 @@ module Openbill
|
|
34
30
|
amount = prepare_amount amoount
|
35
31
|
uri = prepare_uri uri
|
36
32
|
|
37
|
-
Transaction.create(
|
33
|
+
Openbill::Transaction.create(
|
38
34
|
from_account_id: from_account_id,
|
39
35
|
to_account_id: to_account_id,
|
40
36
|
amount_cents: amount.cents,
|
41
37
|
amount_currency: amount.currency,
|
42
|
-
uri:
|
38
|
+
uri: uri,
|
43
39
|
details: details,
|
44
40
|
meta: meta
|
45
41
|
)
|
data/lib/openbill/version.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
require 'openbill'
|
data/lib/openbill.rb
CHANGED
@@ -1,20 +1,36 @@
|
|
1
|
-
require '
|
2
|
-
|
1
|
+
require 'openbill/engine'
|
3
2
|
module Openbill
|
3
|
+
autoload :Service, 'openbill/service'
|
4
|
+
autoload :Account, 'openbill/account'
|
5
|
+
autoload :Transaction, 'openbill/transaction'
|
6
|
+
autoload :Configuration, 'openbill/configuration'
|
7
|
+
|
4
8
|
ACCOUNTS_TABLE_NAME = :openbill_accounts
|
5
9
|
TRANSACTIONS_TABLE_NAME = :openbill_transactions
|
6
10
|
|
7
11
|
class << self
|
8
|
-
|
12
|
+
def root
|
13
|
+
File.dirname __dir__
|
14
|
+
end
|
15
|
+
|
16
|
+
def configure
|
17
|
+
yield self.config
|
18
|
+
end
|
9
19
|
|
10
|
-
|
20
|
+
def config
|
21
|
+
Configuration.instance
|
22
|
+
end
|
11
23
|
|
12
24
|
def generate_uri(resource, id)
|
13
25
|
"obp://local/#{resource}/#{id}"
|
14
26
|
end
|
15
27
|
|
28
|
+
def create_connection(&block)
|
29
|
+
@create_connection_block = block
|
30
|
+
end
|
31
|
+
|
16
32
|
def current
|
17
|
-
@current ||= Openbill::Service.
|
33
|
+
@current ||= Openbill::Service.new config.database
|
18
34
|
end
|
19
35
|
end
|
20
36
|
end
|
data/sql/0_db.sql
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
|
2
|
+
CREATE EXTENSION IF NOT EXISTS hstore WITH SCHEMA public;
|
3
|
+
|
4
|
+
-- CREATE EXTENSION IF NOT EXISTS intarray WITH SCHEMA public;
|
5
|
+
|
6
|
+
CREATE TABLE OPENBILL_ACCOUNTS (
|
7
|
+
id BIGSERIAL PRIMARY KEY,
|
8
|
+
uri character varying(2048) not null,
|
9
|
+
amount_cents numeric not null default 0,
|
10
|
+
amount_currency char(3) not null default 'USD',
|
11
|
+
details text,
|
12
|
+
transactions_count integer not null default 0,
|
13
|
+
last_transaction_id integer,
|
14
|
+
last_transaction_at timestamp without time zone,
|
15
|
+
meta hstore not null default ''::hstore,
|
16
|
+
created_at timestamp without time zone default current_timestamp,
|
17
|
+
updated_at timestamp without time zone default current_timestamp
|
18
|
+
);
|
19
|
+
|
20
|
+
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 INDEX index_accounts_on_meta ON OPENBILL_ACCOUNTS USING gin (meta);
|
23
|
+
CREATE INDEX index_accounts_on_created_at ON OPENBILL_ACCOUNTS USING btree (created_at);
|
24
|
+
|
25
|
+
CREATE TABLE OPENBILL_TRANSACTIONS (
|
26
|
+
id BIGSERIAL PRIMARY KEY,
|
27
|
+
username character varying(255) not null,
|
28
|
+
created_at timestamp without time zone default current_timestamp,
|
29
|
+
from_account_id integer not null,
|
30
|
+
to_account_id integer not null,
|
31
|
+
amount_cents numeric not null CONSTRAINT positive CHECK (amount_cents>0),
|
32
|
+
amount_currency char(3) not null,
|
33
|
+
uri character varying(2048) not null,
|
34
|
+
details text not null,
|
35
|
+
meta hstore not null default ''::hstore,
|
36
|
+
foreign key (from_account_id) REFERENCES OPENBILL_ACCOUNTS (id) ON DELETE RESTRICT ON UPDATE RESTRICT,
|
37
|
+
foreign key (to_account_id) REFERENCES OPENBILL_ACCOUNTS (id)
|
38
|
+
);
|
39
|
+
|
40
|
+
CREATE UNIQUE INDEX index_transactions_on_uri ON OPENBILL_TRANSACTIONS USING btree (uri);
|
41
|
+
CREATE INDEX index_transactions_on_meta ON OPENBILL_TRANSACTIONS USING gin (meta);
|
42
|
+
CREATE INDEX index_transactions_on_created_at ON OPENBILL_TRANSACTIONS USING btree (created_at);
|
43
|
+
|
44
|
+
ALTER TABLE OPENBILL_ACCOUNTS ADD FOREIGN KEY(last_transaction_id) REFERENCES OPENBILL_TRANSACTIONS(id) ON DELETE RESTRICT ON UPDATE RESTRICT;
|
@@ -0,0 +1,27 @@
|
|
1
|
+
-- Запрещаем удаление и какое-либо изменение accounts
|
2
|
+
|
3
|
+
CREATE OR REPLACE FUNCTION disable_update_account() RETURNS TRIGGER AS $disable_update_account$
|
4
|
+
DECLARE
|
5
|
+
query text;
|
6
|
+
BEGIN
|
7
|
+
IF current_query() like 'insert into OPENBILL_TRANSACTIONS%' THEN
|
8
|
+
RETURN NEW;
|
9
|
+
ELSE
|
10
|
+
RAISE EXCEPTION 'Cannot update directly update amount_cents and timestamps of account';
|
11
|
+
END IF;
|
12
|
+
END
|
13
|
+
|
14
|
+
$disable_update_account$ LANGUAGE plpgsql;
|
15
|
+
|
16
|
+
CREATE TRIGGER disable_update_account
|
17
|
+
BEFORE UPDATE ON OPENBILL_ACCOUNTS FOR EACH ROW EXECUTE PROCEDURE disable_update_account();
|
18
|
+
|
19
|
+
CREATE OR REPLACE FUNCTION disable_delete_account() RETURNS TRIGGER AS $disable_delete_account$
|
20
|
+
BEGIN
|
21
|
+
RAISE EXCEPTION 'Cannot delete account';
|
22
|
+
END
|
23
|
+
|
24
|
+
$disable_delete_account$ LANGUAGE plpgsql;
|
25
|
+
|
26
|
+
CREATE TRIGGER disable_delete_account
|
27
|
+
BEFORE DELETE ON OPENBILL_ACCOUNTS FOR EACH ROW EXECUTE PROCEDURE disable_delete_account();
|
@@ -0,0 +1,11 @@
|
|
1
|
+
-- Запрещаем удаление и какое-либо изменение transactions
|
2
|
+
|
3
|
+
CREATE OR REPLACE FUNCTION disable_update_transaction() RETURNS TRIGGER AS $disable_update_transaction$
|
4
|
+
BEGIN
|
5
|
+
RAISE EXCEPTION 'Cannot update or delete transaction';
|
6
|
+
END
|
7
|
+
|
8
|
+
$disable_update_transaction$ LANGUAGE plpgsql;
|
9
|
+
|
10
|
+
CREATE TRIGGER disable_update_transaction
|
11
|
+
BEFORE UPDATE OR DELETE ON OPENBILL_TRANSACTIONS FOR EACH ROW EXECUTE PROCEDURE disable_update_transaction();
|
@@ -0,0 +1,40 @@
|
|
1
|
+
CREATE OR REPLACE FUNCTION process_account_transaction() RETURNS TRIGGER AS $process_transaction$
|
2
|
+
DECLARE
|
3
|
+
result text;
|
4
|
+
BEGIN
|
5
|
+
-- У всех счетов и транзакции должна быть одинаковая валюта
|
6
|
+
|
7
|
+
SELECT id FROM OPENBILL_ACCOUNTS where id = NEW.from_account_id and amount_currency = NEW.amount_currency INTO result;
|
8
|
+
IF NOT FOUND THEN
|
9
|
+
RAISE EXCEPTION 'Account (from #%) has wrong currency', NEW.from_account_id;
|
10
|
+
END IF;
|
11
|
+
|
12
|
+
SELECT * FROM OPENBILL_ACCOUNTS where id = NEW.to_account_id and amount_currency = NEW.amount_currency INTO result;
|
13
|
+
IF NOT FOUND THEN
|
14
|
+
RAISE EXCEPTION 'Account (to #%) has wrong currency', NEW.to_account_id;
|
15
|
+
END IF;
|
16
|
+
|
17
|
+
|
18
|
+
-- установить last_transaction_id, counts и _at
|
19
|
+
UPDATE OPENBILL_ACCOUNTS SET amount_cents = amount_cents - NEW.amount_cents, last_transaction_id = NEW.id, last_transaction_at = NEW.created_at, transactions_count = transactions_count + 1 WHERE id = NEW.from_account_id;
|
20
|
+
UPDATE OPENBILL_ACCOUNTS SET amount_cents = amount_cents + NEW.amount_cents, last_transaction_id = NEW.id, last_transaction_at = NEW.created_at, transactions_count = transactions_count + 1 WHERE id = NEW.to_account_id;
|
21
|
+
|
22
|
+
return NEW;
|
23
|
+
END
|
24
|
+
|
25
|
+
$process_transaction$ LANGUAGE plpgsql;
|
26
|
+
|
27
|
+
CREATE TRIGGER process_account_transaction
|
28
|
+
AFTER INSERT ON OPENBILL_TRANSACTIONS FOR EACH ROW EXECUTE PROCEDURE process_account_transaction();
|
29
|
+
|
30
|
+
-- current_user
|
31
|
+
|
32
|
+
CREATE OR REPLACE FUNCTION add_current_user_to_transaction() RETURNS TRIGGER AS $add_current_user_to_transaction$
|
33
|
+
BEGIN
|
34
|
+
NEW.username := current_user;
|
35
|
+
RETURN NEW;
|
36
|
+
END
|
37
|
+
$add_current_user_to_transaction$ LANGUAGE plpgsql;
|
38
|
+
|
39
|
+
CREATE TRIGGER add_current_user_to_transaction
|
40
|
+
BEFORE INSERT ON OPENBILL_TRANSACTIONS FOR EACH ROW EXECUTE PROCEDURE add_current_user_to_transaction();
|
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.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danil Pismenny
|
@@ -68,12 +68,20 @@ files:
|
|
68
68
|
- Rakefile
|
69
69
|
- bin/console
|
70
70
|
- bin/setup
|
71
|
+
- db/migrate/20160320150928_add_openbill.rb
|
72
|
+
- lib/openbill-ruby.rb
|
71
73
|
- lib/openbill.rb
|
72
74
|
- lib/openbill/account.rb
|
75
|
+
- lib/openbill/configuration.rb
|
76
|
+
- lib/openbill/engine.rb
|
73
77
|
- lib/openbill/service.rb
|
74
78
|
- lib/openbill/transaction.rb
|
75
79
|
- lib/openbill/version.rb
|
76
80
|
- openbill-ruby.gemspec
|
81
|
+
- sql/0_db.sql
|
82
|
+
- sql/1_trigger_disable_delete_account.sql
|
83
|
+
- sql/2_trigger_disable_update_transaction.sql
|
84
|
+
- sql/3_trigger_process_account_transaction.sql
|
77
85
|
homepage: http://brandymint.com
|
78
86
|
licenses:
|
79
87
|
- MIT
|