Class: Graybook::Importer::Gmail
- Inherits:
-
PageScraper
- Object
- Base
- PageScraper
- Graybook::Importer::Gmail
- Defined in:
- lib/graybook/importer/gmail.rb
Overview
Imports contacts from GMail
Constant Summary collapse
- RETRY_THRESHOLD =
5
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.
- #import(*args) ⇒ Object
-
#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!, #service_name
Instance Method Details
#=~(options = {}) ⇒ Object
Matches this importer to an user’s name/address
13 14 15 |
# File 'lib/graybook/importer/gmail.rb', line 13 def =~( = {}) && [:username] =~ /@(gmail|googlemail).com$/i ? true : false end |
#import(*args) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/graybook/importer/gmail.rb', line 17 def import(*args) # GMail depends on Hpricot for some reason... parser = WWW::Mechanize.html_parser WWW::Mechanize.html_parser = Hpricot return super do WWW::Mechanize.html_parser = parser end end |
#login ⇒ Object
login to gmail
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/graybook/importer/gmail.rb', line 29 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) return Graybook::Problem.new("Username and password were 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 true end |
#prepare ⇒ Object
prepare this importer
47 48 49 |
# File 'lib/graybook/importer/gmail.rb', line 47 def prepare login end |
#scrape_contacts ⇒ Object
scrape gmail contacts for this importer
54 55 56 57 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 |
# File 'lib/graybook/importer/gmail.rb', line 54 def scrape_contacts unless agent..find { |c| c.name == 'GAUSR' && c.value.match(/mail(.*?):#{[:username]}/) } return Graybook::Problem.new("Username and password were not accepted. Please check them and try again.") end page = agent.get('http://mail.google.com/mail/h/?v=cl&pnl=a') title = page.search("//title").inner_text if title == 'Redirecting' redirect_text = page.search('//meta').first.attributes['content'].inner_text url = redirect_text.match(/url='(http.*)'/i)[1] page = agent.get(url) end contact_rows = page.search("//input[@name='c']/../..") contact_rows.collect do |row| columns = row/"td" email = columns[2].inner_html.gsub( /(\n| )/, '' ) # email clean_email = email[/[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}/] unless clean_email.empty? columns = row/"td" { :name => ( columns[1] / "b" / "a" ).inner_html.strip, # name :email => clean_email } end end.compact end |