lib/howitzer/email.rb in howitzer-1.1.1 vs lib/howitzer/email.rb in howitzer-2.0.0

- old
+ new

@@ -1,134 +1,126 @@ require 'rspec/matchers' -require 'howitzer/mailgun/connector' require 'howitzer/exceptions' -class Email - include RSpec::Matchers +module Howitzer + # This class describes single email + class Email + include ::RSpec::Matchers - ## - # - # Creates new email with message - # - # *Parameters:* - # * +message+ - email message - # + attr_reader :message - def initialize(message) - @message = message - end + # @return [<MailAdapters::Abstract>] a mail adapter class - ## - # - # Search mail by recepient - # - # *Parameters:* - # * +recepient+ - recepient's email address - # + def self.adapter + return @adapter if @adapter + self.adapter = Howitzer.mail_adapter.to_sym + @adapter + end - def self.find_by_recipient(recipient) - find(recipient, self::SUBJECT) - end + class << self + attr_reader :adapter_name - ## - # - # Search mail by recepient and subject. - # - # *Parameters:* - # * +recepient+ - recepient's email address - # * +subject+ - email subject - # + protected - def self.find(recipient, subject) - message = {} - retryable(timeout: settings.timeout_small, sleep: settings.timeout_short, silent: true, logger: log, on: Howitzer::EmailNotFoundError) do - events = Mailgun::Connector.instance.client.get("#{Mailgun::Connector.instance.domain}/events", event: 'stored') - event = events.to_h['items'].find do |hash| - hash['message']['recipients'].first == recipient && hash['message']['headers']['subject'] == subject + # DSL method to specify a subject pattern directly in an email class + # @param value [String] an email subject with optional placeholders (strings started with : symbol) + # @example + # class WelcomeEmail < Howitzer::Email + # subject 'Welcome on board :name' + # end + # + # WelcomeEmail.find_by_recipient('john.smith@example.com', name: 'John') + # @!visibility public + + def subject(value) + @subject = value end - if event - message = Mailgun::Connector.instance.client.get_url(event['storage']['url']).to_h - else - raise Howitzer::EmailNotFoundError.new('Message not received yet, retry...') + end + + # Specifies a mail adapter + # @param adapter_name [String, Symbol] an email adapter name + # @raise [NoMailAdapterError] when the adapter name is not String or Symbol + + def self.adapter=(adapter_name) + @adapter_name = adapter_name + case adapter_name + when Symbol, String + require "howitzer/mail_adapters/#{adapter_name}" + @adapter = MailAdapters.const_get(adapter_name.to_s.capitalize.to_s) + else + raise Howitzer::NoMailAdapterError end end - log.error Howitzer::EmailNotFoundError, "Message with subject '#{subject}' for recipient '#{recipient}' was not found." if message.empty? - new(message) - end - ## - # - # Returns plain text body of email message - # + # Searches a mail by a recepient + # @param recepient [String] recepient's email address + # @param params [Hash] placeholders with appropriate values + # @raise [NoEmailSubjectError] when a subject is not specified for the email class + # @return [Email] an instance of the email message + # @see .subject - def plain_text_body - @message['body-plain'] - end + def self.find_by_recipient(recipient, params = {}) + raise Howitzer::NoEmailSubjectError, "Please specify email subject. For example:\n" \ + "class SomeEmail < Howitzer::Email\n" \ + " subject ‘some subject text’\nend" if @subject.nil? + new(adapter.find(recipient, expand_subject(params))) + end - ## - # - # Returns html body of email message - # + def initialize(message) + @message = message + end - def html_body - @message['stripped-html'] - end + # @return [String, nil] a plain text of the email message - ## - # - # Returns mail text - # + def plain_text_body + message.plain_text_body + end - def text - @message['stripped-text'] - end + # @return [String, nil] a html body of the email message - ## - # - # Returns who has send email data in format: User Name <user@email> - # + def html_body + message.html_body + end - def mail_from - @message['From'] - end + # @return [String, nil] a mail text - ## - # - # Returns array of recipients who has received current email - # + def text + message.text + end - def recipients - @message['To'].split ', ' - end + # @return [String] who has sent the email data in format: User Name <user@email> - ## - # - # Returns email received time in format: - # + def mail_from + message.mail_from + end - def received_time - @message['Received'][/\w+, \d+ \w+ \d+ \d+:\d+:\d+ -\d+ \(\w+\)$/] - end + # @return [Array<String>] array of recipients who has received current email - ## - # - # Returns sender user email - # + def recipients + message.recipients + end - def sender_email - @message['sender'] - end + # @return [String] email received time - ## - # - # Allows to get email MIME attachment - # + def received_time + message.received_time + end - def get_mime_part - files = @message['attachments'] - if files.empty? - log.error Howitzer::NoAttachmentsError, 'No attachments where found.' - return + # @return [String] a sender user email + + def sender_email + message.sender_email end - files + + # Allows to get email MIME attachment + + def mime_part + message.mime_part + end + + def self.expand_subject(params) + params.each { |k, v| @subject.sub!(":#{k}", v.to_s) } + @subject + end + private_class_method :expand_subject end end