Skip to content

Commit

Permalink
auto validate option for Postmark API response
Browse files Browse the repository at this point in the history
  • Loading branch information
ibalosh committed Mar 17, 2020
1 parent 277d964 commit 9053ecc
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 19 deletions.
30 changes: 15 additions & 15 deletions lib/mailhandler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ module MailHandler
#
def sender(type = :postmark_api, settings = {})
handler = Handler.new
handler.init_sender(type)

settings.each do |setting_name, setting_value|
handler.sender.dispatcher.instance_variable_set("@#{setting_name}", setting_value)
end

handler.init_sender(type, settings)
yield(handler.sender.dispatcher) if block_given?

handler.sender
Expand Down Expand Up @@ -72,12 +67,7 @@ def sender(type = :postmark_api, settings = {})
#
def receiver(type = :folder, settings = {})
handler = Handler.new
handler.init_receiver(type, settings[:notifications])

settings.each do |setting_name, setting_value|
handler.receiver.checker.instance_variable_set("@#{setting_name}", setting_value)
end

handler.init_receiver(type, settings)
yield(handler.receiver.checker) if block_given?
handler.receiver
end
Expand All @@ -96,16 +86,26 @@ class Handler
attr_accessor :sender,
:receiver

def init_sender(type = :postmark_api)
def init_sender(type = :postmark_api, settings = {})
verify_type(type, SENDER_TYPES)
@sender = MailHandler::Sender.new(SENDER_TYPES[type].new)

settings.each do |setting_name, setting_value|
@sender.dispatcher.instance_variable_set("@#{setting_name}", setting_value)
end

@sender
end

def init_receiver(type = :folder, notifications = [])
def init_receiver(type = :folder, settings = {})
verify_type(type, CHECKER_TYPES)
receiver = MailHandler::Receiver.new(CHECKER_TYPES[type].new)

add_receiving_notifications(receiver, notifications)
settings.each do |setting_name, setting_value|
receiver.checker.instance_variable_set("@#{setting_name}", setting_value)
end

add_receiving_notifications(receiver, settings[:notifications])
@receiver = receiver
end

Expand Down
2 changes: 1 addition & 1 deletion lib/mailhandler/receiving/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def validate_count_option(options)

count = options[:count]
error_message = "Incorrect option options[:count]=#{options[:count]}."
raise MailHandler::Error, error_message if (count <0) || (count > 2000)
raise MailHandler::Error, error_message if (count < 0) || (count > 2000)
end

def validate_used_options(options)
Expand Down
13 changes: 11 additions & 2 deletions lib/mailhandler/sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ module MailHandler
# Class for sending email, and storing details about the sending.
class Sender
attr_accessor :dispatcher,
:sending
:sending,
:validate_response

# @param [Time] - sending started at Time
# @param [Time] - sending finished at Time
Expand All @@ -21,13 +22,14 @@ class Sender
def initialize(dispatcher)
@dispatcher = dispatcher
@sending = Sending.new
@validate_response = false
end

def send_email(email)
init_sending_details(email)
response = dispatcher.send(email)
update_sending_details(response)

check_response(response)
response
end

Expand All @@ -37,6 +39,13 @@ def dispatcher_client

private

def check_response(response)
return unless validate_response
return if dispatcher.valid_response?(response)

raise "Invalid sending response: #{response}"
end

def init_sending_details(email)
@sending = Sending.new
@sending.started_at = Time.now
Expand Down
12 changes: 12 additions & 0 deletions lib/mailhandler/sending/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ def setup_sending_client
host: host, secure: @use_ssl)
end

def formatted_response(response)
return if response.keys.select { |key| key.is_a? Symbol }.empty?
return unless response.is_a? Hash

response.keys.select { |key| key.is_a? String }.each { |s| response.delete(s) }
response
end

def valid_response?(response)
response[:message].to_s.strip.downcase == 'ok' && response[:error_code].to_s.downcase == '0'
end

DEFAULTS = {
host: 'api.postmarkapp.com',
read_timeout: 15,
Expand Down
4 changes: 4 additions & 0 deletions lib/mailhandler/sending/api_batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ def send(emails)
client.deliver_messages(emails)
end

def valid_response?(responses)
responses.map { |response| super(response) }.all?(true)
end

protected

def verify_email(emails)
Expand Down
4 changes: 4 additions & 0 deletions lib/mailhandler/sending/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def send(_email)
raise MailHandler::InterfaceError, 'Send interface not implemented.'
end

def valid_response?(_response)
raise MailHandler::InterfaceError, 'Method not implemented.'
end

protected

def verify_email(email)
Expand Down
4 changes: 4 additions & 0 deletions lib/mailhandler/sending/smtp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ def send_raw_email(text_email, smtp_from, smtp_to)
save_response ? response : nil
end

def valid_response?(response)
response.string.to_s.downcase.include?('250 2.0.0 ok')
end

private

def init_net_smtp
Expand Down
2 changes: 1 addition & 1 deletion lib/mailhandler/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MailHandler
VERSION = '1.0.52'
VERSION = '1.0.53'
end

0 comments on commit 9053ecc

Please sign in to comment.