Class: Konzertmeister::Customer

Inherits:
Object
  • Object
show all
Defined in:
lib/konzertmeister/customer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_atObject (readonly)

Returns the value of attribute created_at.



6
7
8
# File 'lib/konzertmeister/customer.rb', line 6

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/konzertmeister/customer.rb', line 6

def id
  @id
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/konzertmeister/customer.rb', line 5

def name
  @name
end

#short_nameObject (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_atObject (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

.allObject



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

#backendsObject



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

#saveObject



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_sObject



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