Class: Blackbook::Importer::Gmail

Inherits:
PageScraper show all
Defined in:
lib/blackbook/importer/gmail.rb

Overview

Imports contacts from GMail

Instance Attribute Summary

Attributes inherited from PageScraper

#agent

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from PageScraper

#create_agent, #fetch_contacts!, #strip_html

Methods inherited from Base

#fetch_contacts!, #import, #service_name

Instance Method Details

#=~(options = {}) ⇒ Object

Matches this importer to an user’s name/address



11
12
13
# File 'lib/blackbook/importer/gmail.rb', line 11

def =~(options = {})
  options && options[:username] =~ /@gmail.com$/i ? true : false
end

#loginObject

login to gmail



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/blackbook/importer/gmail.rb', line 18

def 
  page = agent.get('http://mail.google.com/mail/')
  form = page.forms.first
  form.Email = options[:username]
  form.Passwd = options[:password]
  page = agent.submit(form,form.buttons.first)
  
  raise( Blackbook::BadCredentialsError, "That username and password was not accepted. Please check them and try again." ) if page.body =~ /Username and password do not match/
  
  if page.search('//meta').first.attributes['content'] =~ /url='?(http.+?)'?$/i
    page = agent.get $1
  end
end

#prepareObject

prepare this importer



35
36
37
# File 'lib/blackbook/importer/gmail.rb', line 35

def prepare
  
end

#scrape_contactsObject

scrape gmail contacts for this importer



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/blackbook/importer/gmail.rb', line 42

def scrape_contacts
  unless agent.cookies.find{|c| c.name == 'GAUSR' && 
                         c.value == "mail:#{options[:username]}"}
    raise( Blackbook::BadCredentialsError, "Must be authenticated to access contacts." )
  end
  
  page = agent.get('http://mail.google.com/mail/h/?v=cl&pnl=a')
  contact_rows = page.search("input[@name='c']/../..")
  contact_rows.collect do |row|
    columns = row/"td"
    { 
      :name  => ( columns[1] / "b" ).inner_html, # name
      :email => columns[2].inner_html.gsub( /(\n| )/, '' ) # email
    }
  end
end