module Mailgun::Mailbox
Constants
- VERSION
Public Class Methods
add_auth(base_url)
click to toggle source
# File lib/mailgun/mailbox.rb, line 23 def self.add_auth(base_url) uri = URI(base_url) uri.user=@mailgun_user uri.password=@mailgun_key uri.to_s end
get_events_url()
click to toggle source
# File lib/mailgun/mailbox.rb, line 18 def self.get_events_url base_url = "https://#{@mailgun_host}/v2/#{@mailbox_domain}/events" add_auth(base_url) end
get_message_urls(to_address) { |item['url']| ... }
click to toggle source
# File lib/mailgun/mailbox.rb, line 89 def self.get_message_urls(to_address) t = events_request({:params => {:recipient => to_address}}) return if t['items'].count==0 t['items'].each do |item| if item.has_key?('storage') yield item['storage']['url'] else t = events_request({:params => {'message-id' => item['message']['headers']['message-id']}}) yield t['items'][0]['storage']['url'] end end end
set_generated_email(email)
click to toggle source
# File lib/mailgun/mailbox.rb, line 30 def self.set_generated_email(email) @generated_email = email end
Public Instance Methods
clear_mailbox(to_address=nil)
click to toggle source
# File lib/mailgun/mailbox.rb, line 117 def clear_mailbox(to_address=nil) to_address=@generated_email if to_address.nil? delete_emails_to!(to_address) end
delete_emails_to!(to_address)
click to toggle source
deletes all email message in mailbox addressed to @to_address please note this method is tested to work on <100 messages in mailbox it would run slow if you'll try to delete 1000s of messages so it is suggested to run it after every test case @param [String] to_address
# File lib/mailgun/mailbox.rb, line 109 def delete_emails_to!(to_address) get_message_urls(to_address) do |url| RestClient.delete add_auth(url) end end
events_request(params)
click to toggle source
# File lib/mailgun/mailbox.rb, line 82 def events_request(params) ::Mailgun::RetryHelper::with_rescue_many([RestClient::BadGateway, RestClient::RequestTimeout], 5) do r = RestClient.get get_events_url, params JSON.parse(r.body) end end
generate_email()
click to toggle source
@return [String] = random email address with Mailgun
user Domain
# File lib/mailgun/mailbox.rb, line 140 def generate_email @generated_email = "bot_test_runner+#{Time.now.strftime('%Y%m%d_%H%M%S')}_#{Random.rand(999)}@#{@mailbox_domain}" end
get_first_message(t)
click to toggle source
# File lib/mailgun/mailbox.rb, line 73 def get_first_message(t) msg_url = add_auth(t['items'][0]['storage']['url']) r = RestClient.get add_auth(msg_url), {'accept' => 'message/rfc2822'} t = JSON.parse(r.body) m = Mail.new(t['body-mime']) rescue RestClient::ResourceNotFound=>e return nil end
get_message_by_mid(message_id)
click to toggle source
# File lib/mailgun/mailbox.rb, line 65 def get_message_by_mid(message_id) t = events_request({:params => {'message-id' => message_id}}) return if !t.has_key?('items') return if t['items'].count==0 return if !t['items'][0].has_key?('storage') get_first_message(t) end
look_for_email(subject, to_address)
click to toggle source
# File lib/mailgun/mailbox.rb, line 36 def look_for_email(subject, to_address) to_address=@generated_email if to_address.nil? t = events_request({:params => {:subject=>subject, :recipient => to_address}}) return if t['items'].count==0 m=nil if t['items'][0].has_key?('storage') m = get_first_message(t) else m = get_message_by_mid(t['items'][0]['message']['headers']['message-id']) end return if m.nil? found_message = nil found_message = m.text_part.decoded if m.text_part found_message = m.html_part.decoded if m.html_part and found_message.nil? found_message = m.body.decoded if found_message.nil? return if found_message.nil? return if found_message.length==0 @found_message = found_message end
Also aliased as: _look_for_email
set_config(mailgun_key, mailbox_domain, mailbox_timeout=90, mailgun_host='api.mailgun.net')
click to toggle source
set configuration @param [String] mailgun_key @param [String] mailbox_domain @param [Integer] mailbox_timeout @param [String] mailgun_host
# File lib/mailgun/mailbox.rb, line 150 def set_config(mailgun_key, mailbox_domain, mailbox_timeout=90, mailgun_host='api.mailgun.net') @mailgun_key = mailgun_key @mailbox_domain = mailbox_domain @mailgun_host = mailgun_host unless mailbox_timeout.nil? @mailbox_timeout = mailbox_timeout unless mailbox_timeout.nil? end
wait_for_email(subject, to_address, timeout=nil)
click to toggle source
blocks till email message with specific subject and recipient address show up in the mailbox if email message doesn't show up withing <timeout> seconds throw Selenium::WebDriver::Error::TimeOutError exception
@param [String] subject @param [String] to_address @param [Integer] timeout (optional) @return [String] return message body @raise Selenium::WebDriver::Error::TimeOutError if email message haven't shown up withing timeout seconds
# File lib/mailgun/mailbox.rb, line 130 def wait_for_email(subject, to_address, timeout=nil) timeout = @mailbox_timeout if timeout.nil? to_address=@generated_email if to_address.nil? ::Mailgun::WaitUntil::wait_until(timeout, message="Email to '#{to_address}' with subject '#{subject}' was not found within #{@mailbox_timeout}") do look_for_email(subject, to_address) end end