Module: MailCatcher
- Extended by:
- MailCatcher
- Included in:
- MailCatcher
- Defined in:
- lib/mail_catcher.rb,
lib/mail_catcher/bus.rb,
lib/mail_catcher/web.rb,
lib/mail_catcher/version.rb,
lib/mail_catcher/web/application.rb
Defined Under Namespace
Modules: Mail, Web Classes: Smtp
Constant Summary collapse
- Bus =
EventMachine::Channel.new
- VERSION =
"0.10.0"
- @@defaults =
{ :smtp_ip => "127.0.0.1", :smtp_port => "1025", :http_ip => "127.0.0.1", :http_port => "1080", :http_path => "/", :messages_limit => nil, :verbose => false, :daemon => !windows?, :browse => false, :quit => true, }
Instance Method Summary collapse
- #browsable? ⇒ Boolean
- #browse(url) ⇒ Object
- #development? ⇒ Boolean
- #env ⇒ Object
- #log_exception(message, context, exception) ⇒ Object
- #options ⇒ Object
- #parse!(arguments = ARGV, defaults = @defaults) ⇒ Object
- #quit! ⇒ Object
- #quittable? ⇒ Boolean
- #run!(options = nil) ⇒ Object
- #which?(command) ⇒ Boolean
- #windows? ⇒ Boolean
Instance Method Details
#browsable? ⇒ Boolean
44 45 46 |
# File 'lib/mail_catcher.rb', line 44 def browsable? windows? or which? "open" end |
#browse(url) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/mail_catcher.rb', line 48 def browse url if windows? system "start", "/b", url elsif which? "open" system "open", url end end |
#development? ⇒ Boolean
30 31 32 |
# File 'lib/mail_catcher.rb', line 30 def development? env == "development" end |
#env ⇒ Object
26 27 28 |
# File 'lib/mail_catcher.rb', line 26 def env ENV.fetch("MAILCATCHER_ENV", "production") end |
#log_exception(message, context, exception) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/mail_catcher.rb', line 56 def log_exception(, context, exception) gems_paths = (Gem.path | [Gem.default_dir]).map { |path| Regexp.escape(path) } gems_regexp = %r{(?:#{gems_paths.join("|")})/gems/([^/]+)-([\w.]+)/(.*)} gems_replace = '\1 (\2) \3' puts "*** #{}: #{context.inspect}" puts " Exception: #{exception}" puts " Backtrace:", *exception.backtrace.map { |line| " #{line.sub(gems_regexp, gems_replace)}" } puts " Please submit this as an issue at https://github.com/sj26/mailcatcher/issues" end |
#options ⇒ Object
80 81 82 |
# File 'lib/mail_catcher.rb', line 80 def @@options end |
#parse!(arguments = ARGV, defaults = @defaults) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/mail_catcher.rb', line 88 def parse! arguments=ARGV, defaults=@defaults @@defaults.dup.tap do || OptionParser.new do |parser| parser. = "Usage: mailcatcher [options]" parser.version = VERSION parser.separator "" parser.separator "MailCatcher v#{VERSION}" parser.separator "" parser.on("--ip IP", "Set the ip address of both servers") do |ip| [:smtp_ip] = [:http_ip] = ip end parser.on("--smtp-ip IP", "Set the ip address of the smtp server") do |ip| [:smtp_ip] = ip end parser.on("--smtp-port PORT", Integer, "Set the port of the smtp server") do |port| [:smtp_port] = port end parser.on("--http-ip IP", "Set the ip address of the http server") do |ip| [:http_ip] = ip end parser.on("--http-port PORT", Integer, "Set the port address of the http server") do |port| [:http_port] = port end parser.on("--messages-limit COUNT", Integer, "Only keep up to COUNT most recent messages") do |count| [:messages_limit] = count end parser.on("--http-path PATH", String, "Add a prefix to all HTTP paths") do |path| clean_path = Rack::Utils.clean_path_info("/#{path}") [:http_path] = clean_path end parser.on("--no-quit", "Don't allow quitting the process") do [:quit] = false end unless windows? parser.on("-f", "--foreground", "Run in the foreground") do [:daemon] = false end end if browsable? parser.on("-b", "--browse", "Open web browser") do [:browse] = true end end parser.on("-v", "--verbose", "Be more verbose") do [:verbose] = true end parser.on_tail("-h", "--help", "Display this help information") do puts parser exit end parser.on_tail("--version", "Display the current version") do puts "MailCatcher v#{VERSION}" exit end end.parse! end end |
#quit! ⇒ Object
221 222 223 224 225 |
# File 'lib/mail_catcher.rb', line 221 def quit! MailCatcher::Bus.push(type: "quit") EventMachine.next_tick { EventMachine.stop_event_loop } end |
#quittable? ⇒ Boolean
84 85 86 |
# File 'lib/mail_catcher.rb', line 84 def quittable? [:quit] end |
#run!(options = nil) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/mail_catcher.rb', line 160 def run! =nil # If we are passed options, fill in the blanks &&= @@defaults.merge # Otherwise, parse them from ARGV ||= parse! # Stash them away for later @@options = # If we're running in the foreground sync the output. unless [:daemon] $stdout.sync = $stderr.sync = true end puts "Starting MailCatcher v#{VERSION}" Thin::Logging.debug = development? Thin::Logging.silent = !development? # One EventMachine loop... EventMachine.run do # Set up an SMTP server to run within EventMachine rescue_port [:smtp_port] do EventMachine.start_server [:smtp_ip], [:smtp_port], Smtp puts "==> #{smtp_url}" end # Let Thin set itself up inside our EventMachine loop # Faye connections are hijacked but continue to be supervised by thin rescue_port [:http_port] do Thin::Server.start([:http_ip], [:http_port], Web, signals: false) puts "==> #{http_url}" end # Make sure we quit nicely when asked # We need to handle outside the trap context, hence the timer trap("INT") { EM.add_timer(0) { quit! } } trap("TERM") { EM.add_timer(0) { quit! } } trap("QUIT") { EM.add_timer(0) { quit! } } unless windows? # Open the web browser before detaching console if [:browse] EventMachine.next_tick do browse http_url end end # Daemonize, if we should, but only after the servers have started. if [:daemon] EventMachine.next_tick do if quittable? puts "*** MailCatcher runs as a daemon by default. Go to the web interface to quit." else puts "*** MailCatcher is now running as a daemon that cannot be quit." end Process.daemon end end end end |
#which?(command) ⇒ Boolean
34 35 36 37 38 |
# File 'lib/mail_catcher.rb', line 34 def which?(command) ENV["PATH"].split(File::PATH_SEPARATOR).any? do |directory| File.executable?(File.join(directory, command.to_s)) end end |
#windows? ⇒ Boolean
40 41 42 |
# File 'lib/mail_catcher.rb', line 40 def windows? RbConfig::CONFIG["host_os"].match?(/mswin|mingw/) end |