Class: Aceroute::Customer

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, email, location = {}, cid = nil) ⇒ Aceroute::Customer

Creates a new Aceroute::Customer object. Note this does not persist the Customer to Aceroute, that can be done by calling the create! method on the new object.

Parameters:

  • name (String)

    customer name

  • email (String)

    customer email

  • location (Hash) (defaults to: {})

    customer Location, optional

  • cid (Integer) (defaults to: nil)

    Aceroute customer id, optional; useful for instantiating Customer objects from Aceroute API response



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/aceroute/customer.rb', line 16

def initialize(name, email, location = {}, cid = nil)
  self.locations = []
  #create getters/setters for each param
  self.email = email
  self.name = name
  self.cid = cid

  if !location.empty?
    locations << Aceroute::Location.new(location[:address1], location[:address2], location[:description],
    location[:name], location[:phone])
  end
end

Instance Attribute Details

#cidObject

Returns the value of attribute cid.



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

def cid
  @cid
end

#emailObject

Returns the value of attribute email.



4
5
6
# File 'lib/aceroute/customer.rb', line 4

def email
  @email
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#locationsObject

Returns the value of attribute locations.



3
4
5
# File 'lib/aceroute/customer.rb', line 3

def locations
  @locations
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.delete(id) ⇒ Object

Deletes Aceroute::Customer of given id from Aceroute

Parameters:

  • id (Integer)


62
63
64
65
66
# File 'lib/aceroute/customer.rb', line 62

def self.delete(id)
  recs = "<data><del><id>#{id}</id></del></data>"
  ret = Aceroute::call_api("customer.delete", recs)
  ret.success == "true" ? true : false
end

Instance Method Details

#create!Aceroute::Customer

Persists Customer object to Aceroute API.

Returns:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/aceroute/customer.rb', line 32

def create!
  recs = "<data>
    <cst>
      <nm>#{self.name}</nm>
      <locnm>#{self.locations.first.description}</locnm>
      <adr>#{self.locations.first.address1}</adr>
      <adr2>#{self.locations.first.address2}</adr2>
      <cntnm>#{self.locations.first.name}</cntnm>
      <tel>#{self.locations.first.phone}</tel>
      <eml>#{self.email}</eml>
    </cst>
  </data>"

  #puts recs
  data = Aceroute::call_api("customer.create", recs)
  location = data.locs.loc
  customer = data.cnts.cnt
  update_attrs(customer)
  self.cid = customer.cid
  locations.first.update_attrs(location)
  return self
end

#destroy!(id = nil) ⇒ Object

Deletes this Aceroute::Customer object (self) from Aceroute;



56
57
58
# File 'lib/aceroute/customer.rb', line 56

def destroy!(id = nil)
  Customer.delete(id ? id : self.cid)
end