Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 2.09 KB | None | 0 0
  1. require 'debugger'
  2. require 'rubygems'
  3. require 'mechanize'
  4. require 'mail'
  5.  
  6. class MailinatorReader
  7.   BASE_URL = 'http://mailinator.com'
  8.  
  9.   attr_accessor :mailbox
  10.  
  11.   def initialize (name)
  12.     @name = name
  13.     @email = format_email(name)
  14.     @mailbox = []
  15.     @agent = Mechanize.new
  16.  
  17.     get_mailbox
  18.   end
  19.  
  20.   def get_email
  21.     @email
  22.   end
  23.  
  24.   def last_email
  25.     @mailbox.first
  26.   end
  27.  
  28.   ## NOTE: only works if we compare the names of the senders, NOT EMAILS
  29.   def last_email_from from
  30.     @mailbox.each do |mail|
  31.       return mail if compare_senders(mail.from, from)
  32.     end
  33.     nil
  34.   end
  35.  
  36.   def last_email_with_subject subject
  37.     @mailbox.each do |mail|
  38.       return mail if compare_subjects(mail.subject, subject)
  39.     end
  40.     nil
  41.   end
  42.  
  43.   def emails_with_subject subject
  44.     emails = nil
  45.     emails = @mailbox.select { |mail| compare_subjects(mail.subject, subject) }
  46.   end
  47.  
  48.   def emails_from from
  49.     emails = nil
  50.     emails = @mailbox.select { |mail| compare_senders(mail.from, from) }
  51.   end
  52.  
  53.   def inbox_url action
  54.     "#{BASE_URL}/#{action}.jsp?to=#{self.get_email}"
  55.   end
  56.  
  57. private
  58.   def get_mailbox
  59.     begin
  60.       page = @agent.get(self.inbox_url('inbox'))
  61.     rescue
  62.       raise "Coudln't connect: InboxError"
  63.     end
  64.     require 'debugger';debugger
  65.     inbox_list= page.search("#mailcontainer")
  66.     mails = inbox_list.search('//tr')
  67.    
  68.     #delete first and last 2 tr column
  69.     mails = mails[1..(mails.size - 2)]
  70.  
  71.     mails.each do |line|
  72.       @arr = line.search('td').map do |a|
  73.         a.text
  74.       end
  75.       mail = Mail.new
  76.       mail.subject = @arr[1]
  77.       mail.body = "Nada"
  78.       mail.to = @email
  79.       mail.from = @arr[0]
  80.  
  81.       @mailbox << mail
  82.     end
  83.    
  84.   end
  85.  
  86.   def action_url(action)
  87.     URI.parse("#{BASE_URL}/#{action}.jsp?email=#{@name}").to_s
  88.   end
  89.  
  90.   def format_email(email)
  91.     email =~ /^[a-zA-Z0-9]+$/ ? email : "#{email}"
  92.   end
  93.  
  94.   def compare_subjects sub_a, sub_b
  95.     sub_a == sub_b
  96.   end
  97.  
  98.   def compare_senders sen_a, sen_b
  99.     sen_a == sen_b
  100.   end
  101. end
  102.  
  103. MailinatorReader.new('nonagency_qe')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement