class Coruro::MailcatcherAdapter::Runner

Allows for launching and terminating mailcatcher programmaticaly

Attributes

stderr[RW]
stdin[RW]
stdout[RW]
thread[RW]

Public Instance Methods

start(config) click to toggle source
# File lib/coruro/mailcatcher_adapter.rb, line 77
def start(config)
  return if up?(config)
  new_env = Unbundle.all(ENV)
  self.stdin, self.stdout, self.stderr, self.thread =
    Open3.popen3(new_env, 'mailcatcher -f --ip=0.0.0.0', { unsetenv_others:true })

  debug_stream(:stdout, config: config)
  debug_stream(:stderr, config: config)
end
stop() click to toggle source
# File lib/coruro/mailcatcher_adapter.rb, line 94
def stop

  return unless self.thread
  streams.each do |(_, stream)|
    stream.close
  end
  `kill -9 #{ thread[:pid] }`
end
up?(config) click to toggle source
# File lib/coruro/mailcatcher_adapter.rb, line 87
def up?(config)
  response = Net::HTTP.get_response(URI("#{config.http_root}"))
  response.is_a?(Net::HTTPSuccess)
rescue Errno::ECONNREFUSED, Errno::EADDRNOTAVAIL => _
  false
end

Private Instance Methods

debug_stream(stream_name, config:) click to toggle source
# File lib/coruro/mailcatcher_adapter.rb, line 103
        def debug_stream(stream_name, config:)
  if config.expose_stream?(stream_name)
    stream_target = config.expose_streams[stream_name]
    stream = streams[stream_name]
    Thread.new {
      while line = stream.gets do
        stream_target.puts(line)
      end
    }
  end
end
streams() click to toggle source
# File lib/coruro/mailcatcher_adapter.rb, line 116
        def streams
  { stderr: stderr, stdout: stdout, stdin: stdin }
end