Class: Konzertmeister::Customer
- Inherits:
-
Object
- Object
- Konzertmeister::Customer
- Defined in:
- lib/konzertmeister/customer.rb
Instance Attribute Summary collapse
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
-
#short_name ⇒ Object
readonly
Returns the value of attribute short_name.
-
#updated_at ⇒ Object
readonly
Returns the value of attribute updated_at.
Class Method Summary collapse
Instance Method Summary collapse
- #backends ⇒ Object
-
#initialize(data = {}) ⇒ Customer
constructor
A new instance of Customer.
- #save ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(data = {}) ⇒ Customer
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/konzertmeister/customer.rb', line 22 def initialize(data = {}) @data = data data.each do |k,value| next if k == 'backends' if m = k.match(/^customer_(.*)$/) key = m[1] else key = k end instance_variable_set("@#{key}", value) end end |
Instance Attribute Details
#created_at ⇒ Object (readonly)
Returns the value of attribute created_at.
6 7 8 |
# File 'lib/konzertmeister/customer.rb', line 6 def created_at @created_at end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
6 7 8 |
# File 'lib/konzertmeister/customer.rb', line 6 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/konzertmeister/customer.rb', line 5 def name @name end |
#short_name ⇒ Object (readonly)
Returns the value of attribute short_name.
6 7 8 |
# File 'lib/konzertmeister/customer.rb', line 6 def short_name @short_name end |
#updated_at ⇒ Object (readonly)
Returns the value of attribute updated_at.
6 7 8 |
# File 'lib/konzertmeister/customer.rb', line 6 def updated_at @updated_at end |
Class Method Details
.all ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/konzertmeister/customer.rb', line 8 def self.all response = Konzertmeister.session.get('/customers') if response response.map do |data| Konzertmeister::Customer.new(data) end.sort_by(&:short_name) end end |
.find_by(attr, value) ⇒ Object
17 18 19 20 |
# File 'lib/konzertmeister/customer.rb', line 17 def self.find_by(attr, value) encoded_value = URI.encode(value) unless attr == 'id' Customer.new(Konzertmeister.session.get("/customers/#{attr}/#{encoded_value}")) end |
Instance Method Details
#backends ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/konzertmeister/customer.rb', line 49 def backends @backends ||= if @data.key?('backends') @data.fetch('backends').map do |be_hash| Konzertmeister::Backend.new(be_hash) end.sort_by(&:short_name) else Customer.find_by('id', self.id).backends end end |
#save ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/konzertmeister/customer.rb', line 37 def save params = { customer_name: name } response = Konzertmeister.session.post("/customers", params) if response response_object = Konzertmeister::Customer.new(response) @id = response_object.id @short_name = response_object.short_name end end |
#to_s ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/konzertmeister/customer.rb', line 59 def to_s monthly_cost = backends.inject(0) do |sum, backend| sum + backend.monthly_cost end cost_string = "$%1.2f/month" % [monthly_cost] "%-14s %s" % [cost_string, "#{name} (#{short_name})"] end |