Class: Getairmail::Email

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil) ⇒ Email

Creates a new Email object with a random email and URL

Parameters:

  • URL (String)

    of the mail account web interface



16
17
18
19
20
21
22
# File 'lib/getairmail.rb', line 16

def initialize(url = nil)
  url ||= Net::HTTP.get(URI.parse('http://getairmail.com/random')).match(/href="([^"]+)"/)[1]
  @url = url
  puts "********GETAIRMAIL URL  =  #{@url} *************" # TODO For logging purpose need to delete it.
  @email_address = Nokogiri::HTML.parse(open(@url)).css('#tempemail').first.attributes['value'].value
  load_from_server
end

Instance Attribute Details

#email_addressObject (readonly)

Returns the value of attribute email_address.



11
12
13
# File 'lib/getairmail.rb', line 11

def email_address
  @email_address
end

#emailsObject (readonly)

Returns the value of attribute emails.



11
12
13
# File 'lib/getairmail.rb', line 11

def emails
  @emails
end

#urlObject (readonly)

Returns the value of attribute url.



11
12
13
# File 'lib/getairmail.rb', line 11

def url
  @url
end

Class Method Details

.get_mail(url) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/getairmail.rb', line 64

def self.get_mail(url)
  inbox_emails = self.new(url).load_from_server
  if inbox_emails.blank?
    wait_secs(12) #Wait for email 
    inbox_emails = self.new(url).load_from_server
  end
  return inbox_emails
end

.get_second_email(url) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/getairmail.rb', line 73

def self.get_second_email(url)
  inbox_emails = self.new(url).load_from_server
  if(inbox_emails.blank? or inbox_emails.count!=2)
    wait_secs(12) #Wait for email 
    inbox_emails = self.new(url).load_from_server
  end 
  return inbox_emails   
end

Instance Method Details

#delete(mail) ⇒ Boolean

Deletes the email you pass it

Parameters:

  • The (Mail)

    email you want to delete

Returns:

  • (Boolean)

    Returns true if successful, else returns false



27
28
29
30
31
32
33
34
35
# File 'lib/getairmail.rb', line 27

def delete(mail)
  resp = Net::HTTP.new('getairmail.com').request(Net::HTTP::Delete.new("/d/#{mail.message_id}"))
  if resp.code == "200"
    @emails.delete_if{|email| email.message_id == mail.message_id }
    true
  else
    false
  end
end

#delete_allBoolean

Deletes all emails

Returns:

  • (Boolean)

    Returns true if successfull, else returns false



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

def delete_all
  count = 0
  # For some reason it fails periodically, so I do it a few times
  while @emails.any? and count < 3
    @emails.each{|email| delete(email) }
    count += 1
  end
  @emails.empty?
end

#load_from_serverArray

Gets the emails from the web interface

Returns:

  • (Array)

    Array of emails (can be called with .emails)



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/getairmail.rb', line 51

def load_from_server
  messages = JSON.parse(Nokogiri::HTML.parse(open(@url)).css('head script')[1].text.match(/messages = ({.+})/)[1])['m']
  @emails = messages.map do |message|
    Mail.new do
      from       message['f']
      message_id message['u']
      to         @email_address
      subject    message['s']
      body       open("http://getairmail.com/html/#{message['u']}").read
    end
  end
end