Class: AddressFetcher::Gmail

Inherits:
Base
  • Object
show all
Defined in:
lib/address_fetcher/gmail.rb

Instance Attribute Summary

Attributes inherited from Base

#agent, #options

Instance Method Summary collapse

Methods inherited from Base

#create_agent, #fetch, #type

Instance Method Details

#=~(options) ⇒ Object



3
4
5
# File 'lib/address_fetcher/gmail.rb', line 3

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

#do_fetchObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/address_fetcher/gmail.rb', line 22

def do_fetch
  raise BadCredentialsError, "Login at first" unless @agent.cookies.find { |c| c.name == 'GAUSR' && c.value == "mail:#{options[:username]}" }

  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_text, :email => columns[2].inner_html.gsub( /(\n| )/, '') }
  end
end

#loginObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/address_fetcher/gmail.rb', line 7

def 
  page = @agent.get("http://www.gmail.com")
   = page.forms.first
  .Email = @options[:username]
  .Passwd = @options[:password]
  page = @agent.submit()

  raise BadCredentialsError, "Wrong username and password pair." if page.body =~ /Username and password do not match/

  url = page.search('//meta').first.attributes['content'].split(/[URL|url]='?/).last rescue nil
  url = url[0...-1] if url[-1] == 39

  page = @agent.get url
end