Class: DistributionWrappers::Google

Inherits:
Email
  • Object
show all
Defined in:
lib/distribution_wrappers/google/google.rb

Instance Attribute Summary

Attributes inherited from Base

#params

Instance Method Summary collapse

Methods inherited from Email

#prepare

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from DistributionWrappers::Base

Instance Method Details

#convert_to_contact_entry(entry, index) ⇒ Object



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
# File 'lib/distribution_wrappers/google/google.rb', line 58

def convert_to_contact_entry(entry, index)
  # creating nil fields to keep the fields consistent across other networks
  name = ""
  if entry['gd$name']
    gd_name = entry['gd$name']
    first_name = normalize_name(entry['gd$name']['gd$givenName']['$t']) if gd_name['gd$givenName']
    last_name = normalize_name(entry['gd$name']['gd$familyName']['$t']) if gd_name['gd$familyName']
    name = normalize_name(entry['gd$name']['gd$fullName']['$t']) if gd_name['gd$fullName']
    name = full_name(first_name,last_name) if name.nil?
  end
  
  emails = []
  entry['gd$email'].each do |email|
    if email['rel']
      split_index = email['rel'].index('#')
      emails << {:name => email['rel'][split_index + 1, email['rel'].length - 1], :email => email['address']}
    elsif email['label']
      emails << {:name => email['label'], :email => email['address']}
    end
  end if entry['gd$email']

  # Support older versions of the gem by keeping singular entries around
  email = emails[0][:email] if emails[0]
  
  name = email_to_name(name) if (!name.nil? && name.include?('@'))
  name = email_to_name(email) if (name.nil? && emails[0] && email)
  
  if email && email != "" && name && name != ""
    return_string = CSV.generate_line [name, email, '{"url": null}', 'email', @params[:channel_id]]
  else
    return_string = nil
  end
  
  return_string
end

#email_to_name(username_or_email) ⇒ Object

create a username/name from a given email



118
119
120
121
122
123
124
125
126
127
# File 'lib/distribution_wrappers/google/google.rb', line 118

def email_to_name username_or_email
  username_or_email = username_or_email.split('@').first if username_or_email.include?('@')
  if group = (/(?<first>[a-z|A-Z]+)[\.|_](?<last>[a-z|A-Z]+)/).match(username_or_email)
    first_name = normalize_name(group[:first])
    last_name = normalize_name(group[:last])
    return "#{first_name} #{last_name}"
  end
  username = normalize_name(username_or_email)
  return username
end

#full_name(first_name, last_name) ⇒ Object

create a full name given the individual first and last name



110
111
112
113
114
115
# File 'lib/distribution_wrappers/google/google.rb', line 110

def full_name first_name, last_name
  return "#{first_name} #{last_name}" if first_name && last_name
  return "#{first_name}" if first_name && !last_name
  return "#{last_name}" if !first_name && last_name
  return nil
end

#get_accessObject



94
95
96
97
98
# File 'lib/distribution_wrappers/google/google.rb', line 94

def get_access
  service = Google::Google::Apis::GmailV1::GmailService.new
  service.authorization = @auth[:key]
  service
end

#get_contactsObject



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
# File 'lib/distribution_wrappers/google/google.rb', line 30

def get_contacts
  record_count = 0
  loop do
    url = "https://www.google.com/m8/feeds/contacts/default/full?max-results=10000&alt=json&start-index=#{record_count+1}"
    response = JSON.parse(RestClient.get(url, {"GData-Version" => "3.0", "Authorization" => "Bearer #{@auth[:key]}"}))
    
    break if response['feed'].nil? || response['feed']['entry'].nil?
    
    response['feed']['entry'].each_with_index do |entry, index|
      new_string = convert_to_contact_entry(entry, index)
      csv_string += new_string unless new_string.nil?
      if index % 100 == 0
        @params[:temp_file].write(csv_string)
        csv_string = ""
      end
    end
    
    @params[:temp_file].write(csv_string)
    csv_string = ""
    
    record_count += response['feed']['entry'].length
    break if response['feed']['entry'].length < 10000
  end 
  
  @params[:temp_file].rewind
  return true
end

#normalize_name(name) ⇒ Object

normalize the name



101
102
103
104
105
106
107
# File 'lib/distribution_wrappers/google/google.rb', line 101

def normalize_name name
  return nil if name.nil?
  name.chomp!
  name.squeeze!(' ')
  name.strip!
  return name
end

#send_message(recipient) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/distribution_wrappers/google/google.rb', line 9

def send_message(recipient)
  super
  
  email = recipient['identifier']
  client = get_access
  
  msg              = Mail.new
  msg.date         = Time.now
  msg.subject      = @params[:subject]
  msg.body         = @message
  msg.to           = email
  msg.content_type = 'text/html'
  
  if @params[:custom_opts]['send_from']
    msg.from = @params[:custom_opts]['send_from']
  end
  
  send_response = client.send_user_message('me', upload_source: StringIO.new(msg.to_s), content_type: 'message/rfc822')
  return {:response => send_response, :msg => msg}
end