Module: ContactSync::Syncable

Defined in:
lib/contact_sync/syncable.rb

Instance Method Summary collapse

Instance Method Details

#complete_phone_numberObject



13
14
15
16
17
18
19
20
21
# File 'lib/contact_sync/syncable.rb', line 13

def complete_phone_number
  if self.cc_prefix.blank?
    "00#{self.phone_number}"
  elsif self.cc_prefix == "0"
    "#{self.cc_prefix}#{self.phone_number}"
  else
    "+#{self.cc_prefix}#{self.phone_number}"
  end
end

#matched_contactsObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/contact_sync/syncable.rb', line 107

def matched_contacts
  matched_users = []
  other_contacts = []
  self.contacts.includes([:emails, :phones]).each do |aKon|
    #Check Phones to Match existing users.
    matches = false
    aKon.phones.each do |ph|
      u = User.where(encrypted_number: ph.number.encrypt(:symmetric)).limit(1).first
      if !u.blank?
        matches = true
        matched_users << {user: u.id, phone: ph.id, contact: aKon.id}
      end
    end
    #Check Emails to Match existing users.
    aKon.emails.each do |em|
      u = User.where(email: em.email).limit(1).first
      if !u.blank?
        matches = true
        matched_users << {user: u.id, email: em.id, contact: aKon.id}
      end
    end
    other_contacts << aKon if !matches
  end
  return matched_users
end

#phone_numberObject



3
4
5
# File 'lib/contact_sync/syncable.rb', line 3

def phone_number
  return self.encrypted_number.try(:decrypt,:symmetric) || ""
end

#phone_number=(string) ⇒ Object



7
8
9
10
11
# File 'lib/contact_sync/syncable.rb', line 7

def phone_number=(string)
  self.cc_prefix, num = string.extract_country_code
  self.encrypted_number = num.encrypt(:symmetric)
  self.encrypted_number
end

#remove_all_contactsObject



133
134
135
136
137
138
139
# File 'lib/contact_sync/syncable.rb', line 133

def remove_all_contacts
  self.contacts.destroy_all
  if self.contacts.count > 0
    return false
  end
  return true
end

#sync_contacts(contact_hash = {}) ⇒ Object

Raises:

  • (ArgumentError)


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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/contact_sync/syncable.rb', line 22

def sync_contacts(contact_hash = {})
  result = {:new => {failed:[], duplicate: []}, modified: {failed:[]}, deleted: {failed:[]}}
  raise ArgumentError, "You need to provide contacts hash." if contact_hash.blank?
  # raise ArgumentError, "You need to provide Device Identifier as deviceID" if contact_hash[:device_id].blank?
  ##########################################################################
  #########################Create new Contacts##############################
  if !contact_hash[:new].blank?
    contacts = contact_hash[:new]
    contacts.each do |aContact|
      phones = aContact[:phones]
      emails = aContact[:emails]
      aContact.delete :phones
      aContact.delete :emails
      newContact = Contact.new(contact_params(aContact))
      begin
        # newContact = Contact.new(contact_params(aContact))
        # newContact.device_id = contact_hash[:device_id]
        if phones && phones.class == Array
          phones.each do |aPhone|
            newContact.phones.build(phone_params(aPhone))
          end
        end
        if emails && emails.class == Array
          emails.each do |anEmail|
            newContact.emails.build(email_params(anEmail))
          end
        end
        self.contacts << newContact
        puts "Trying to save Contact: #{newContact}"
        if newContact.save
          # result[:new][:success] << newContact.record_id
        else
          puts "Failed Saving. Error: #{newContact.errors.full_messages}"
          result[:new][:failed] << newContact.record_id
        end
      rescue ActiveRecord::RecordNotUnique => e
        puts "Rescuing Duplicate Record"
        result[:new][:duplicate] << newContact.record_id
        newContact.destroy
      end
    end
  end
  ##########################################################################
  ########################Modify Contacts###################################
  if !contact_hash[:modified].blank?
    modified_contacts = contact_hash[:modified]
    modified_contacts.each do |con|
      theContact = self.contacts.find_by_record_id(con[:record_id])
      if ! theContact.blank?
        #lets create phone_numbers that have been added in this contact.
        phones = con[:phones]
        phones.each do |ph|
          found_phone = theContact.phones.where(encrypted_number: ph[:number].extract_country_code[-1].encrypt(:symmetric)).limit(1).first
          if !found_phone
            theContact.phones.create(phone_params(ph))
          else
            puts 'modified phone_number is already present. Ignoring situation.'
          end
        end
        if !theContact.update_attributes(contact_params(con))
          result[:modified][:failed] << theContact.record_id
        else
          # result[:modified][:success] << theContact.record_id
        end
      end
    end
  end
  ##########################################################################
  ########################Delete Contacts###################################
  if !contact_hash[:deleted].blank?
    delete_contacts = contact_hash[:deleted]
    delete_contacts.each do |con|
      if self.contacts.find_by_record_id(con[:record_id].to_i) and self.contacts.find_by_record_id(con[:record_id].to_i).destroy
        # result[:deleted][:success] << con[:record_id]
      else
        result[:deleted][:failed] << con[:record_id]
      end
    end
  end
  after_contact_sync
  self.save
  return result
end