Class: RubePost::Grabber

Inherits:
Object
  • Object
show all
Defined in:
lib/rube_post.rb

Constant Summary collapse

URL =
"https://portal.epost.de"
SHOW_URL =
"#{URL}/mail/mailbox/view?msgStart=&messages="

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Grabber

Returns a new instance of Grabber.



38
39
40
41
42
43
44
45
46
47
# File 'lib/rube_post.rb', line 38

def initialize(username, password)
  @agent = Mechanize.new
  page = @agent.get("#{URL}/login")
   = page.forms.first
  .field_with('username').value = username.sub(/@.*/,'')
  .field_with('password').value = password
  @inbox = .submit
  @account = messages_form.action.match(/(\d{10,})/)[1]
  @agent
end

Instance Method Details

#email_content(id) ⇒ Object



61
62
63
64
# File 'lib/rube_post.rb', line 61

def email_content(id)
  body = @agent.get("#{SHOW_URL}#{full_id(id).gsub('/','%2F')}").send(:html_body)
  Nokogiri::HTML(body).css('#e-post-body').first.text.strip
end

#emails_in_inboxObject



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rube_post.rb', line 49

def emails_in_inbox
  doc = Nokogiri::HTML(@inbox.send(:html_body))
  mails = doc.css('#messageList > tr')[1..-1] || raise('could not open inbox <-> login failed ?')
  mails.map do |mail|
    {
      :id => mail.attr('id').split('/').last,
      :sender => self.class.extract_sender(mail),
      :subject => mail.css('td.subject').text.strip
    }
  end
end

#move_to_trash(id) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/rube_post.rb', line 66

def move_to_trash(id)
  form = messages_form
  checkbox = form.checkboxes.detect{|c| c.value == full_id(id) }
  checkbox.check
  button = form.buttons.detect{|b| b.name == 'cmd[delete-message]'}
  page = @agent.submit(form, button)
  checkbox.uncheck # revert checking or something might o wrong later...
  page
end