vault_client 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/helpers/http_helpers.rb +45 -0
- data/lib/models/account.rb +13 -0
- data/lib/models/acct_own.rb +31 -0
- data/lib/models/b_event.rb +27 -0
- data/lib/models/billable_unit.rb +5 -2
- data/lib/models/line_item.rb +2 -4
- data/lib/models/report.rb +51 -0
- data/lib/models/res_own.rb +31 -0
- data/lib/models/unit_group.rb +20 -21
- data/lib/presenters/base_presenter.rb +6 -3
- data/lib/presenters/line_item_presenter.rb +27 -45
- data/lib/presenters/report_presenter.rb +10 -4
- data/lib/presenters/unit_group_presenter.rb +21 -36
- data/lib/presenters/unit_presenter.rb +3 -3
- data/lib/services/line_item_builder.rb +1 -1
- data/lib/vault_client.rb +18 -74
- data/readme.md +190 -26
- data/test/models/b_event_test.rb +20 -0
- data/test/models/billable_unit_test.rb +8 -0
- data/test/models/line_item_test.rb +10 -0
- data/test/models/unit_group_test.rb +17 -0
- data/test/presenters/line_item_presenter_test.rb +36 -0
- data/test/presenters/report_presenter_test.rb +10 -4
- data/test/services/line_item_builder_test.rb +9 -5
- data/test/test_helper.rb +4 -6
- metadata +24 -20
- data/lib/billable_events_client.rb +0 -45
- data/lib/dto/billable_event.rb +0 -0
- data/lib/dto/usage_report.rb +0 -35
- data/lib/models/usage_report.rb +0 -21
- data/lib/resource_ownership_client.rb +0 -55
- data/lib/services/report_builder.rb +0 -15
- data/lib/usage_reports_client.rb +0 -28
- data/lib/vault_helper.rb +0 -15
- data/test/models/usage_report_test.rb +0 -38
- data/test/resource_ownership_client_test.rb +0 -42
@@ -0,0 +1,45 @@
|
|
1
|
+
module VC
|
2
|
+
module HttpHelpers
|
3
|
+
extend self
|
4
|
+
|
5
|
+
attr_accessor :url
|
6
|
+
def url
|
7
|
+
@url || ENV["VAULT_URL"]
|
8
|
+
end
|
9
|
+
|
10
|
+
def headers
|
11
|
+
{:content_type => :json, :accept => :json}
|
12
|
+
end
|
13
|
+
|
14
|
+
def enc_json(hash)
|
15
|
+
Yajl::Encoder.encode(hash)
|
16
|
+
end
|
17
|
+
|
18
|
+
def dec_json(json)
|
19
|
+
Yajl::Parser.parse(json)
|
20
|
+
end
|
21
|
+
|
22
|
+
def handle_req(&blk)
|
23
|
+
begin
|
24
|
+
resp = yield
|
25
|
+
dec_json(resp.body)
|
26
|
+
rescue RestClient::Exception => e
|
27
|
+
body = dec_json(e.http_body)
|
28
|
+
case e.http_code
|
29
|
+
when 404
|
30
|
+
when 401
|
31
|
+
raise(AuthorizationError, "Response: #{body.inspect}")
|
32
|
+
when 403
|
33
|
+
when 409
|
34
|
+
when 500
|
35
|
+
raise(UnexpectedError, "Response: #{body.inspect}")
|
36
|
+
when 503
|
37
|
+
raise(ServiceDownError, "#{VC.url} is down for maintenance.")
|
38
|
+
else
|
39
|
+
raise(UnexpectedError, "Response: #{body.inspect}")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module VC
|
2
|
+
module AcctOwn
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def act(args)
|
6
|
+
pmid, eid = args.delete(:payment_method_id), args.delete(:entity_id)
|
7
|
+
VC.handle_req {RestClient.post(acct_own_url(pmid, eid), args, VC.headers)}
|
8
|
+
end
|
9
|
+
|
10
|
+
def xfr(args)
|
11
|
+
prev_pmid, prev_eid = args.delete(:prev_payment_method_id), args.delete(:prev_entity_id)
|
12
|
+
VC.handle_req {RestClient.put(acct_own_url(prev_pmid, prev_eid), args, VC.headers)}
|
13
|
+
end
|
14
|
+
|
15
|
+
def deact(args)
|
16
|
+
pmid = args.delete(:payment_method_id)
|
17
|
+
eid = args.delete(:entity_id)
|
18
|
+
account_id = args[:account_id]
|
19
|
+
time = args[:time]
|
20
|
+
VC.handle_req {RestClient.delete([acct_own_url(pmid, eid), CGI.encode("?account_id=#{account_id}&time=#{time}")].join)}
|
21
|
+
end
|
22
|
+
|
23
|
+
def query(args)
|
24
|
+
VC.handle_req {RestClient.get([VC.url, "/accounts/#{args[:account_id]}/resource_ownerships"].join)}
|
25
|
+
end
|
26
|
+
|
27
|
+
def acct_own_url(payment_method_id, entity_id)
|
28
|
+
[VC.url, "/payment_methods/#{payment_method_id}/account_ownerships/#{entity_id}"].join
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module VC
|
2
|
+
module BEvent
|
3
|
+
extend self
|
4
|
+
|
5
|
+
OPEN = "open"
|
6
|
+
CLOSE = "close"
|
7
|
+
|
8
|
+
def open(args)
|
9
|
+
hid = args.delete(:hid)
|
10
|
+
entity_id = args.delete(:entity_id)
|
11
|
+
args[:state] = OPEN
|
12
|
+
VC.handle_req {RestClient.put(events_url(hid, entity_id), args, VC.headers)}
|
13
|
+
end
|
14
|
+
|
15
|
+
def close(args)
|
16
|
+
hid = args.delete(:hid)
|
17
|
+
entity_id = args.delete(:entity_id)
|
18
|
+
args[:state] = CLOSE
|
19
|
+
VC.handle_req {RestClient.put(events_url(hid, entity_id), args, VC.headers)}
|
20
|
+
end
|
21
|
+
|
22
|
+
def events_url(hid, entity_id)
|
23
|
+
[VC.url, "/resources/#{hid}/billable_events/#{entity_id}"].join
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
data/lib/models/billable_unit.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
module
|
1
|
+
module VC
|
2
2
|
class BillableUnit
|
3
3
|
attr_accessor(
|
4
4
|
:hid,
|
5
5
|
:from,
|
6
6
|
:to,
|
7
7
|
:qty,
|
8
|
-
:total,
|
9
8
|
:rate,
|
10
9
|
:rate_period,
|
11
10
|
:product_group,
|
@@ -28,5 +27,9 @@ module VaultClient
|
|
28
27
|
Time.now.to_i
|
29
28
|
end
|
30
29
|
|
30
|
+
def total
|
31
|
+
@rate * @qty
|
32
|
+
end
|
33
|
+
|
31
34
|
end
|
32
35
|
end
|
data/lib/models/line_item.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module VC
|
2
2
|
class LineItem
|
3
3
|
|
4
4
|
attr_reader :unit_groups
|
@@ -12,10 +12,8 @@ module VaultClient
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def total
|
15
|
-
@unit_groups.
|
15
|
+
@unit_groups.map(&:total).reduce(:+)
|
16
16
|
end
|
17
17
|
|
18
18
|
end
|
19
19
|
end
|
20
|
-
|
21
|
-
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module VC
|
2
|
+
class Report
|
3
|
+
attr_accessor(
|
4
|
+
:from,
|
5
|
+
:to,
|
6
|
+
:billable_units
|
7
|
+
)
|
8
|
+
|
9
|
+
def fetch
|
10
|
+
VC.handle_req do
|
11
|
+
RestClient.get([VC.url, resource].join, {:params => {:from => from.utc.to_s, :to => to.utc.to_s}})
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def billable_units
|
16
|
+
@billable_units ||= report["billable_units"].map {|buh| BillableUnit.new(buh)}
|
17
|
+
end
|
18
|
+
|
19
|
+
def total
|
20
|
+
report["total"]
|
21
|
+
end
|
22
|
+
|
23
|
+
def report
|
24
|
+
@report ||= fetch
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class UsageReport < Report
|
29
|
+
attr_accessor :account_id
|
30
|
+
|
31
|
+
def initialize(account_id, from, to)
|
32
|
+
@account_id, @from, @to = account_id, from, to
|
33
|
+
end
|
34
|
+
|
35
|
+
def resource
|
36
|
+
"/accounts/#{@account_id}/usage_reports"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class Invoice < Report
|
41
|
+
attr_accessor :payment_method_id
|
42
|
+
|
43
|
+
def initialize(payment_method_id, from, to)
|
44
|
+
@payment_method_id, @from, @to = payment_method_id, from, to
|
45
|
+
end
|
46
|
+
|
47
|
+
def resource
|
48
|
+
"/payment_methods/#{payment_method_id}/invoices"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module VC
|
2
|
+
module ResOwn
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def act(args)
|
6
|
+
aid, eid = args.delete(:account_id), args.delete(:entity_id)
|
7
|
+
VC.handle_req {RestClient.post(res_own_url(aid, eid), args, VC.headers)}
|
8
|
+
end
|
9
|
+
|
10
|
+
def xfr(args)
|
11
|
+
prev_aid, prev_eid = args.delete(:prev_account_id), args.delete(:prev_entity_id)
|
12
|
+
VC.handle_req {RestClient.put(res_own_url(prev_aid, prev_eid), args, VC.headers)}
|
13
|
+
end
|
14
|
+
|
15
|
+
def deact(args)
|
16
|
+
aid = args.delete(:account_id)
|
17
|
+
eid = args.delete(:entity_id)
|
18
|
+
hid = args[:hid]
|
19
|
+
time = args[:time]
|
20
|
+
VC.handle_req {RestClient.delete([res_own_url(aid, eid), CGI.encode("?hid=#{hid}&time=#{time}")].join)}
|
21
|
+
end
|
22
|
+
|
23
|
+
def query(args)
|
24
|
+
VC.handle_req {RestClient.get([VC.url, "/accounts/#{args[:account_id]}/resource_ownerships"].join)}
|
25
|
+
end
|
26
|
+
|
27
|
+
def res_own_url(account_id, entity_id)
|
28
|
+
[VC.url, "/accounts/#{account_id}/resource_ownerships/#{entity_id}"].join
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/models/unit_group.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
module
|
1
|
+
module VC
|
2
2
|
class UnitGroup
|
3
|
-
InvalidUnitGroup = Class.new(
|
3
|
+
InvalidUnitGroup = Class.new(VCException)
|
4
4
|
|
5
5
|
attr_reader :units
|
6
6
|
|
@@ -15,45 +15,44 @@ module VaultClient
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
def
|
19
|
-
|
18
|
+
def total
|
19
|
+
rate * qty
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
23
|
-
units.
|
22
|
+
def qty
|
23
|
+
@units.map(&:qty).reduce(:+)
|
24
24
|
end
|
25
25
|
|
26
|
-
def
|
27
|
-
|
26
|
+
def rate
|
27
|
+
sample_unit.rate
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
31
|
-
sample_unit.
|
30
|
+
def rate_period
|
31
|
+
sample_unit.rate_period
|
32
32
|
end
|
33
33
|
|
34
|
-
def
|
35
|
-
sample_unit.
|
34
|
+
def name
|
35
|
+
sample_unit.product_name
|
36
36
|
end
|
37
37
|
|
38
|
-
def
|
39
|
-
|
40
|
-
sample_unit.product_name == "run"
|
38
|
+
def hid
|
39
|
+
sample_unit.hid
|
41
40
|
end
|
42
41
|
|
43
|
-
def
|
42
|
+
def product_name
|
44
43
|
sample_unit.product_name
|
45
44
|
end
|
46
45
|
|
47
|
-
def
|
48
|
-
|
46
|
+
def product_group
|
47
|
+
sample_unit.product_group
|
49
48
|
end
|
50
49
|
|
51
|
-
def
|
52
|
-
sample_unit.
|
50
|
+
def description
|
51
|
+
sample_unit.product_name
|
53
52
|
end
|
54
53
|
|
55
54
|
def sample_unit
|
56
|
-
@sample_unit ||= @units.
|
55
|
+
@sample_unit ||= @units.sample
|
57
56
|
end
|
58
57
|
|
59
58
|
end
|
@@ -1,9 +1,12 @@
|
|
1
|
-
module
|
1
|
+
module VC
|
2
2
|
class BasePresenter
|
3
3
|
|
4
4
|
def money(cents)
|
5
|
-
|
6
|
-
|
5
|
+
add_commas(pennies_to_dollar(cents.to_i))
|
6
|
+
end
|
7
|
+
|
8
|
+
def add_commas(str)
|
9
|
+
str.reverse.scan(/(?:\d*\.)?\d{1,3}-?/).join(',').reverse
|
7
10
|
end
|
8
11
|
|
9
12
|
def pennies_to_dollar(qty)
|
@@ -1,67 +1,49 @@
|
|
1
|
-
module
|
1
|
+
module VC
|
2
2
|
class LineItemPresenter < BasePresenter
|
3
3
|
|
4
4
|
def initialize(line_item)
|
5
5
|
@line_item = line_item
|
6
|
+
@unit_groups = @line_item.unit_groups
|
7
|
+
@unit_group_presenters = @unit_groups.map {|ug| UnitGroupPresenter.new(ug)}
|
6
8
|
end
|
7
9
|
|
8
|
-
def
|
9
|
-
|
10
|
-
ugp.unit_group
|
11
|
-
end.inject(0) do |memo, ug|
|
12
|
-
ug.total
|
13
|
-
end
|
14
|
-
money(result)
|
15
|
-
end
|
16
|
-
|
17
|
-
def dyno_unit_group_total
|
18
|
-
result = dyno_unit_group_presenters.map do |ugp|
|
19
|
-
ugp.unit_group
|
20
|
-
end.inject(0) do |memo, ug|
|
21
|
-
ug.total
|
22
|
-
end
|
23
|
-
money(result)
|
10
|
+
def total
|
11
|
+
money(@line_item.total)
|
24
12
|
end
|
25
13
|
|
26
|
-
def
|
27
|
-
|
14
|
+
def name
|
15
|
+
@line_item.app_name
|
28
16
|
end
|
29
17
|
|
30
|
-
def
|
31
|
-
|
32
|
-
upg.unit_group
|
33
|
-
end.inject(0) do |memo, ug|
|
34
|
-
ug.qty
|
35
|
-
end
|
18
|
+
def unit_group_rate(product_group)
|
19
|
+
unit_group_presenters(product_group).sample.rate
|
36
20
|
end
|
37
21
|
|
38
|
-
def
|
39
|
-
|
22
|
+
def unit_group_total(product_group)
|
23
|
+
money(
|
24
|
+
unit_group_presenters(product_group).
|
25
|
+
map(&:unit_group).
|
26
|
+
map(&:total).
|
27
|
+
reduce(:+)
|
28
|
+
)
|
40
29
|
end
|
41
30
|
|
42
|
-
def
|
43
|
-
|
31
|
+
def unit_group_qty(product_group)
|
32
|
+
trunc_hours(
|
33
|
+
unit_group_presenters(product_group).
|
34
|
+
map(&:unit_group).
|
35
|
+
map(&:qty).
|
36
|
+
reduce(:+)
|
37
|
+
)
|
44
38
|
end
|
45
39
|
|
46
|
-
def unit_group_presenters
|
47
|
-
if
|
48
|
-
@unit_group_presenters
|
40
|
+
def unit_group_presenters(product_group=nil)
|
41
|
+
if product_group
|
42
|
+
@unit_group_presenters.select {|ugp| ugp.product_group == product_group}
|
49
43
|
else
|
50
|
-
@unit_group_presenters
|
44
|
+
@unit_group_presenters
|
51
45
|
end
|
52
46
|
end
|
53
47
|
|
54
|
-
def total
|
55
|
-
money(@line_item.total)
|
56
|
-
end
|
57
|
-
|
58
|
-
def name
|
59
|
-
@line_item.app_name
|
60
|
-
end
|
61
|
-
|
62
|
-
def unit_groups
|
63
|
-
@unit_groups ||= @line_item.unit_groups
|
64
|
-
end
|
65
|
-
|
66
48
|
end
|
67
49
|
end
|
@@ -1,13 +1,19 @@
|
|
1
|
-
module
|
1
|
+
module VC
|
2
2
|
class ReportPresenter < BasePresenter
|
3
3
|
|
4
|
-
def initialize(
|
5
|
-
@
|
4
|
+
def initialize(report, line_item_builder=LineItemBuilder)
|
5
|
+
@report = report
|
6
|
+
@units = @report.billable_units
|
7
|
+
@builder = line_item_builder
|
8
|
+
end
|
9
|
+
|
10
|
+
def total
|
11
|
+
money(@report.total)
|
6
12
|
end
|
7
13
|
|
8
14
|
def line_item_presenters
|
9
15
|
@line_item_presenters ||= begin
|
10
|
-
|
16
|
+
@builder.build(@units).map do |li|
|
11
17
|
LineItemPresenter.new(li)
|
12
18
|
end
|
13
19
|
end
|