class Coruro::MailcatcherAdapter

Translates between Curoro and Mailcatcher's API

Attributes

config[RW]
runner[RW]
timeout[RW]

Public Class Methods

new(timeout:, config:) click to toggle source
# File lib/coruro/mailcatcher_adapter.rb, line 15
def initialize(timeout:, config:)
  self.timeout = timeout
  self.config = Configuration.new(config)
end

Public Instance Methods

all() click to toggle source
# File lib/coruro/mailcatcher_adapter.rb, line 20
def all
  messages
end
match?(query, value) click to toggle source
# File lib/coruro/mailcatcher_adapter.rb, line 36
def match?(query, value)
  return false if query.nil?
  return value.any? { |child| match?(query, child) } if value.respond_to?(:any?)
  return query.match?(value) if query.respond_to?(:match?)
  return !query.match(value).nil? if query.respond_to?(:match)
  raise ArgumentError, "Query #{query} must respond to `match?` or Value #{value} must respond to `any?`"
end
start() click to toggle source
# File lib/coruro/mailcatcher_adapter.rb, line 48
def start
  runner.start(config)
end
up?() click to toggle source
# File lib/coruro/mailcatcher_adapter.rb, line 44
def up?
  runner.up?(config)
end
where(to: nil, from: nil, subject: nil) click to toggle source
# File lib/coruro/mailcatcher_adapter.rb, line 24
def where(to: nil, from: nil, subject: nil)
  result = []; start_time = Time.now.to_f
  while (result.empty? && (Time.now.to_f - start_time) <=  timeout)
    result = messages.select do |message|
      match?(to, message[:recipients]) ||
      match?(from, message[:sender]) ||
      match?(subject, message[:subject])
    end.map(&method(:find_by))
  end
  result
end

Private Instance Methods

find_by(attributes) click to toggle source
# File lib/coruro/mailcatcher_adapter.rb, line 65
        def find_by(attributes)
  message = Message.new(Mail.new(raw_message(attributes[:id])))
  message.id = attributes[:id]
  message
end
messages() click to toggle source
# File lib/coruro/mailcatcher_adapter.rb, line 56
        def messages
  JSON.parse(Net::HTTP.get(URI("#{http_root}/messages")), symbolize_names: true)
end
raw_message(message_id) click to toggle source
# File lib/coruro/mailcatcher_adapter.rb, line 60
        def raw_message(message_id)
  Net::HTTP.get(URI("#{http_root}/messages/#{message_id}.eml"))
end