Class: Charger::Component

Inherits:
Object
  • Object
show all
Includes:
Resource
Defined in:
lib/charger/component.rb

Direct Known Subclasses

Metered, OnOff, QuantityBased

Defined Under Namespace

Classes: Metered, OnOff, QuantityBased

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find(product_family_id, id) ⇒ Object



59
60
61
# File 'lib/charger/component.rb', line 59

def self.find product_family_id, id
  new(client.get("product_families/#{product_family_id}/components/#{id}")['component'])
end

.find_by_product_family_id(id) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/charger/component.rb', line 42

def self.find_by_product_family_id id
  components = []

  client.get("product_families/#{id}/components").each do |data|
    case data['component']['kind']
    when 'quantity_based_component'
      components << Component::QuantityBased.new(data['component'])
    when 'metered_component'
      components << Component::Metered.new(data['component'])
    when 'on_off_component'
      components << Component::OnOff.new(data['component'])
    end
  end

  components
end

Instance Method Details

#total_for(amount) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/charger/component.rb', line 16

def total_for amount
  prices.sort_by! { |k| k.starting_quantity }

  total = 0.0

  case pricing_scheme
  when 'per_unit'
    total = amount * unit_price
  when 'volume'
    total = amount * prices.select { |price| price.between_quantities?(amount) }.unit_price
  when 'tiered'
    prices.each do |price|
      total += price.total(amount)
    end
  when 'stairstep'
    total = prices.select { |price| price.between_quantities?(amount) }.unit_price
  end

  total
rescue => e
  puts "[ERROR] #{e}"
  puts "[ERROR] #{amount}"
  puts "[ERROR] #{self.to_json}"
  return 0.0
end