vault_client 0.0.3 → 0.1.0

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.
@@ -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,13 @@
1
+ module VC
2
+ module Account
3
+ extend self
4
+
5
+ def create
6
+ VC.handle_req {RestClient.post(accounts_url, {}, VC.headers)}
7
+ end
8
+
9
+ def accounts_url
10
+ [VC.url, "/accounts"].join
11
+ end
12
+ end
13
+ 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
@@ -1,11 +1,10 @@
1
- module VaultClient
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
@@ -1,4 +1,4 @@
1
- module VaultClient
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.inject(0) {|memo, unit_group| unit_group.total}
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
@@ -1,6 +1,6 @@
1
- module VaultClient
1
+ module VC
2
2
  class UnitGroup
3
- InvalidUnitGroup = Class.new(Exception)
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 name
19
- @name ||= units.first.product_name
18
+ def total
19
+ rate * qty
20
20
  end
21
21
 
22
- def hid
23
- units.first.hid
22
+ def qty
23
+ @units.map(&:qty).reduce(:+)
24
24
  end
25
25
 
26
- def total
27
- units.inject(0) {|memo, unit| unit.rate * unit.qty}
26
+ def rate
27
+ sample_unit.rate
28
28
  end
29
29
 
30
- def dyno_type?
31
- sample_unit.product_group == "dyno"
30
+ def rate_period
31
+ sample_unit.rate_period
32
32
  end
33
33
 
34
- def add_on_type?
35
- sample_unit.product_group == "addon"
34
+ def name
35
+ sample_unit.product_name
36
36
  end
37
37
 
38
- def run_type?
39
- return false
40
- sample_unit.product_name == "run"
38
+ def hid
39
+ sample_unit.hid
41
40
  end
42
41
 
43
- def description
42
+ def product_name
44
43
  sample_unit.product_name
45
44
  end
46
45
 
47
- def qty
48
- @units.inject(0) {|memo, unit| unit.qty}
46
+ def product_group
47
+ sample_unit.product_group
49
48
  end
50
49
 
51
- def rate
52
- sample_unit.rate
50
+ def description
51
+ sample_unit.product_name
53
52
  end
54
53
 
55
54
  def sample_unit
56
- @sample_unit ||= @units.first
55
+ @sample_unit ||= @units.sample
57
56
  end
58
57
 
59
58
  end
@@ -1,9 +1,12 @@
1
- module VaultClient
1
+ module VC
2
2
  class BasePresenter
3
3
 
4
4
  def money(cents)
5
- return if cents.nil?
6
- pennies_to_dollar(cents.to_i)
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 VaultClient
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 add_on_unit_group_total
9
- result = add_on_unit_group_presenters.map do |ugp|
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 dyno_unit_group_rate
27
- 0.05
14
+ def name
15
+ @line_item.app_name
28
16
  end
29
17
 
30
- def dyno_unit_group_qty
31
- dyno_unit_group_presenters.map do |upg|
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 dyno_unit_group_presenters
39
- unit_group_presenters.select {|ugp| ugp.unit_group.dyno_type?}
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 add_on_unit_group_presenters
43
- unit_group_presenters.select {|ugp| ugp.unit_group.add_on_type?}
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 defined?(@unit_group_presenters)
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 = unit_groups.map {|ug| UnitGroupPresenter.new(ug)}
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 VaultClient
1
+ module VC
2
2
  class ReportPresenter < BasePresenter
3
3
 
4
- def initialize(units)
5
- @units = units
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
- LineItemBuilder.build(@units).map do |li|
16
+ @builder.build(@units).map do |li|
11
17
  LineItemPresenter.new(li)
12
18
  end
13
19
  end