Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'debugger'
- require 'rubygems'
- require 'mechanize'
- require 'mail'
- class MailinatorReader
- BASE_URL = 'http://mailinator.com'
- attr_accessor :mailbox
- def initialize (name)
- @name = name
- @email = format_email(name)
- @mailbox = []
- @agent = Mechanize.new
- get_mailbox
- end
- def get_email
- @email
- end
- def last_email
- @mailbox.first
- end
- ## NOTE: only works if we compare the names of the senders, NOT EMAILS
- def last_email_from from
- @mailbox.each do |mail|
- return mail if compare_senders(mail.from, from)
- end
- nil
- end
- def last_email_with_subject subject
- @mailbox.each do |mail|
- return mail if compare_subjects(mail.subject, subject)
- end
- nil
- end
- def emails_with_subject subject
- emails = nil
- emails = @mailbox.select { |mail| compare_subjects(mail.subject, subject) }
- end
- def emails_from from
- emails = nil
- emails = @mailbox.select { |mail| compare_senders(mail.from, from) }
- end
- def inbox_url action
- "#{BASE_URL}/#{action}.jsp?to=#{self.get_email}"
- end
- private
- def get_mailbox
- begin
- page = @agent.get(self.inbox_url('inbox'))
- rescue
- raise "Coudln't connect: InboxError"
- end
- require 'debugger';debugger
- inbox_list= page.search("#mailcontainer")
- mails = inbox_list.search('//tr')
- #delete first and last 2 tr column
- mails = mails[1..(mails.size - 2)]
- mails.each do |line|
- @arr = line.search('td').map do |a|
- a.text
- end
- mail = Mail.new
- mail.subject = @arr[1]
- mail.body = "Nada"
- mail.to = @email
- mail.from = @arr[0]
- @mailbox << mail
- end
- end
- def action_url(action)
- URI.parse("#{BASE_URL}/#{action}.jsp?email=#{@name}").to_s
- end
- def format_email(email)
- email =~ /^[a-zA-Z0-9]+$/ ? email : "#{email}"
- end
- def compare_subjects sub_a, sub_b
- sub_a == sub_b
- end
- def compare_senders sen_a, sen_b
- sen_a == sen_b
- end
- end
- MailinatorReader.new('nonagency_qe')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement