Class: RingioAPI::Contact

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

Defined Under Namespace

Classes: Datum

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find(contact_id) ⇒ Object

move the nested resource to the root



12
13
14
# File 'lib/ringioAPI/contact.rb', line 12

def self.find(contact_id)
  super(contact_id).contact
end

Instance Method Details

#saveObject



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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ringioAPI/contact.rb', line 16

def save

  # change the array of Datum objects to an array of hashes just to make it easy to parse as JSON
  new_data = []
  begin
    old_data = self.data
    self.data.each do |d|
      if d
        new_data << {:type => d.type, :value => d.value, :is_primary => d.is_primary, :rel => d.rel}
      end
    end
    self.data = new_data
  rescue NoMethodError
  end

  result = super

  # restore the array of Datums
  begin
    self.data = old_data
  rescue NoMethodError
  end


  # move the fields returned after an update response to their proper place in the root
  begin
    self.id = self.contact.id
  rescue NoMethodError
  end
  begin
    self.last_updated = self.contact.last_updated
  rescue NoMethodError
  end
  begin
    self.date_created = self.contact.last_updated
  rescue NoMethodError
  end

  # clean the unused nested resource
  begin
    self.contact = nil
  rescue NoMethodError
  end

  result
end