Class: Blackbook::Importer::Gmail
- Inherits:
-
PageScraper
- Object
- Base
- PageScraper
- Blackbook::Importer::Gmail
- Defined in:
- lib/blackbook/importer/gmail.rb
Overview
Imports contacts from GMail
Instance Attribute Summary
Attributes inherited from PageScraper
Attributes inherited from Base
Instance Method Summary collapse
-
#=~(options = {}) ⇒ Object
Matches this importer to an user’s name/address.
-
#login ⇒ Object
login to gmail.
-
#prepare ⇒ Object
prepare this importer.
-
#scrape_contacts ⇒ Object
scrape gmail contacts for this importer.
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 =~( = {}) && [:username] =~ /@gmail.com$/i ? true : false end |
#login ⇒ Object
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 login page = agent.get('http://mail.google.com/mail/') form = page.forms.first form.Email = [:username] form.Passwd = [:password] page = agent.submit(form,form..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 |
#prepare ⇒ Object
prepare this importer
35 36 37 |
# File 'lib/blackbook/importer/gmail.rb', line 35 def prepare login end |
#scrape_contacts ⇒ Object
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..find{|c| c.name == 'GAUSR' && c.value == "mail:#{[: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 |