Class: Cloudsponge::Contact

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudsponge/contact.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contact_data) ⇒ Contact

Returns a new instance of Contact.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cloudsponge/contact.rb', line 10

def initialize(contact_data)
  super()
  # get the basic data
  self.first_name = contact_data['first_name']
  self.last_name = contact_data['last_name']

  # get the phone numbers
  self.phones = []
  contact_data['phone'] && contact_data['phone'].each do |phone|
    self.add_array_value(self.phones, phone['number'], phone['type'])
  end

  self.emails = []
  contact_data['email'] && contact_data['email'].each do |email|
    self.add_array_value(self.emails, email['address'], email['type'])
  end

  @addresses = contact_data['address'] && contact_data['address'].inject([]) do |memo, address|
    memo << {
      :type => address['type'], 
      :street => address["street"], 
      :city => address["city"], 
      :region => address["region"], 
      :country => address["country"], 
      :postal_code => address["postal_code"], 
      :formatted => address["formatted"]}
  end || []
  
  self
end

Instance Attribute Details

#addressesObject

Returns the value of attribute addresses.



4
5
6
# File 'lib/cloudsponge/contact.rb', line 4

def addresses
  @addresses
end

#emailsObject

Returns the value of attribute emails.



4
5
6
# File 'lib/cloudsponge/contact.rb', line 4

def emails
  @emails
end

#first_nameObject

Returns the value of attribute first_name.



4
5
6
# File 'lib/cloudsponge/contact.rb', line 4

def first_name
  @first_name
end

#last_nameObject

Returns the value of attribute last_name.



4
5
6
# File 'lib/cloudsponge/contact.rb', line 4

def last_name
  @last_name
end

#phonesObject

Returns the value of attribute phones.



4
5
6
# File 'lib/cloudsponge/contact.rb', line 4

def phones
  @phones
end

Class Method Details

.from_array(list) ⇒ Object



6
7
8
# File 'lib/cloudsponge/contact.rb', line 6

def self.from_array(list)
  list.map { |contact_data| Contact.new(contact_data) }.compact
end

Instance Method Details

#add_array_value(collection, value, type = nil) ⇒ Object



57
58
59
# File 'lib/cloudsponge/contact.rb', line 57

def add_array_value(collection, value, type = nil)
  collection << {:value => value, :type => type}
end

#addressObject



53
54
55
# File 'lib/cloudsponge/contact.rb', line 53

def address
  self.addresses && self.addresses.first && "#{self.addresses.first[:street]} #{self.addresses.first[:city]} #{self.addresses.first[:region]}".strip
end

#emailObject



45
46
47
# File 'lib/cloudsponge/contact.rb', line 45

def email
  Contact.get_first_value(self.emails)
end

#nameObject



41
42
43
# File 'lib/cloudsponge/contact.rb', line 41

def name
  "#{self.first_name} #{self.last_name}"
end

#phoneObject



49
50
51
# File 'lib/cloudsponge/contact.rb', line 49

def phone
  Contact.get_first_value(self.phones)
end