Class: Tact::GoogleContacts::Syncer

Inherits:
Object
  • Object
show all
Defined in:
lib/tact/google_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(entry) ⇒ Syncer

Returns a new instance of Syncer.



79
80
81
82
83
# File 'lib/tact/google_client.rb', line 79

def initialize(entry)
  @entry = entry
  @new_numbers = []
  @new_emails = []
end

Instance Method Details

#add_new_emails(contact) ⇒ Object



112
113
114
# File 'lib/tact/google_client.rb', line 112

def add_new_emails(contact)
  contact.emails << new_emails
end

#add_new_phone_numbers(contact) ⇒ Object



108
109
110
# File 'lib/tact/google_client.rb', line 108

def add_new_phone_numbers(contact)
  contact.phone_numbers << new_numbers
end

#find_contactObject



94
95
96
97
98
99
# File 'lib/tact/google_client.rb', line 94

def find_contact
  Contact.find_by(
    first_name: entry.first_name.upcase,
    last_name: (entry.last_name || "").upcase
  )
end

#get_new_emails(contact) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/tact/google_client.rb', line 124

def get_new_emails(contact)
  entry.emails.each do |email|
    if !contact.emails.any? { |e| e.address == email.address }
      new_emails << email
    end
  end if entry.emails
end

#get_new_phone_numbers(contact) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/tact/google_client.rb', line 116

def get_new_phone_numbers(contact)
  entry.phone_numbers.each do |number|
    if !contact.phone_numbers.any? { |n| n.number == number.number }
      new_numbers << number
    end
  end if entry.phone_numbers
end

#merge_properties(contact) ⇒ Object



101
102
103
104
105
106
# File 'lib/tact/google_client.rb', line 101

def merge_properties(contact)
  get_new_phone_numbers(contact)
  get_new_emails(contact)
  add_new_phone_numbers(contact)
  add_new_emails(contact)
end

#syncObject



85
86
87
88
89
90
91
92
# File 'lib/tact/google_client.rb', line 85

def sync
  contact = find_contact || Contact.new(
    first_name: entry.first_name.upcase,
    last_name: (entry.last_name || "").upcase
  )
  merge_properties(contact)
  contact.save
end