Class: Rails::Contact::Google::PayloadMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/rails/contact/google/payload_mapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(contact) ⇒ PayloadMapper

Returns a new instance of PayloadMapper.



5
6
7
# File 'lib/rails/contact/google/payload_mapper.rb', line 5

def initialize(contact)
  @contact = contact
end

Instance Method Details

#to_people_payloadObject



9
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
# File 'lib/rails/contact/google/payload_mapper.rb', line 9

def to_people_payload
  bio_text = [ @contact.biography, @contact.region_name ].compact.join("\n")
  {
    names: [ { givenName: @contact.given_name, familyName: family_name_for_google } ],
    emailAddresses: @contact.emails.filter_map do |email|
      next if email.value.blank?

      { value: email.value, type: (email.label || "other") }
    end,
    phoneNumbers: @contact.phones.filter_map do |phone|
      value = phone.e164.presence || phone.value
      next if value.blank?

      { value: value, type: (phone.label || "mobile") }
    end,
    addresses: @contact.addresses.map do |address|
      {
        city: address.city,
        formattedValue: address.formatted_value.presence || "Current city: #{@contact.current_city}\nDeparture city: #{@contact.departure_city}"
      }
    end,
    biographies: ([ { value: bio_text, contentType: "TEXT_PLAIN" } ] if bio_text.present?)
  }.compact.tap do |h|
    h.delete(:emailAddresses) if h[:emailAddresses].blank?
    h.delete(:phoneNumbers) if h[:phoneNumbers].blank?
    h.delete(:addresses) if h[:addresses].blank?
  end
end