class DistributionWrappers::Google

Public Instance Methods

convert_to_contact_entry(entry, index) click to toggle source
# 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) click to toggle source

create a username/name from a given email

# 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) click to toggle source

create a full name given the individual first and last name

# 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_access() click to toggle source
# 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_contacts() click to toggle source
# 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) click to toggle source

normalize the name

# 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) click to toggle source
Calls superclass method DistributionWrappers::Base#send_message
# 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