Module: MailCatcher
- Extended by:
- MailCatcher
- Included in:
- MailCatcher
- Defined in:
- lib/mail_catcher.rb,
lib/mail_catcher/growl.rb,
lib/mail_catcher/version.rb
Defined Under Namespace
Modules: Events, Growl, Mail Classes: Smtp, Web
Constant Summary collapse
- VERSION =
"1.1.5"
- @@defaults =
{ :smtp_ip => '0.0.0.0', :smtp_port => '9000', :http_ip => '127.0.0.1', :http_port => '1080', :verbose => false, :daemon => false, #:daemon => !windows?, :growl => growlnotify?, :browse => false, :quit => true, }
Instance Method Summary collapse
- #browse(url) ⇒ Object
- #browse? ⇒ Boolean
- #growl? ⇒ Boolean
- #growlnotify? ⇒ Boolean
- #mac? ⇒ Boolean
- #macruby? ⇒ Boolean
- #options ⇒ Object
- #parse!(arguments = ARGV, defaults = @defaults) ⇒ Object
- #quit! ⇒ Object
- #quittable? ⇒ Boolean
- #run!(options = nil) ⇒ Object
- #which(command) ⇒ Object
- #windows? ⇒ Boolean
Instance Method Details
#browse(url) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/mail_catcher.rb', line 41 def browse url if windows? system "start", "/b", url elsif which "open" system "open", url end end |
#browse? ⇒ Boolean
37 38 39 |
# File 'lib/mail_catcher.rb', line 37 def browse? windows? or which "open" end |
#growl? ⇒ Boolean
33 34 35 |
# File 'lib/mail_catcher.rb', line 33 def growl? growlnotify? end |
#growlnotify? ⇒ Boolean
29 30 31 |
# File 'lib/mail_catcher.rb', line 29 def growlnotify? which "growlnotify" end |
#mac? ⇒ Boolean
17 18 19 |
# File 'lib/mail_catcher.rb', line 17 def mac? RbConfig::CONFIG['host_os'] =~ /darwin/ end |
#macruby? ⇒ Boolean
25 26 27 |
# File 'lib/mail_catcher.rb', line 25 def macruby? mac? and const_defined? :MACRUBY_VERSION end |
#options ⇒ Object
62 63 64 |
# File 'lib/mail_catcher.rb', line 62 def @@options rescue @@defaults end |
#parse!(arguments = ARGV, defaults = @defaults) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 |
# File 'lib/mail_catcher.rb', line 70 def parse! arguments=ARGV, defaults=@defaults @@defaults.dup.tap do || OptionParser.new do |parser| parser. = "Usage: mailcatcher [options]" parser.version = VERSION 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("--no-quit", "Don't allow quitting the process") do [:quit] = false end if mac? parser.on("--[no-]growl", "Growl to the local machine when a message arrives") do |growl| if growl and not growlnotify? puts "You'll need to install growlnotify from the Growl installer." puts puts "See: http://growl.info/extras.php#growlnotify" exit -2 end [:growl] = growl end end unless windows? parser.on('-f', '--foreground', 'Run in the foreground') do [:daemon] = false end end if browse? 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('-h', '--help', 'Display this help information') do puts parser exit end end.parse! end end |
#quit! ⇒ Object
192 193 194 |
# File 'lib/mail_catcher.rb', line 192 def quit! EventMachine.next_tick { EventMachine.stop_event_loop } end |
#quittable? ⇒ Boolean
66 67 68 |
# File 'lib/mail_catcher.rb', line 66 def quittable? [:quit] end |
#run!(options = nil) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 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 |
# File 'lib/mail_catcher.rb', line 137 def run! =nil # If we are passed options, fill in the blanks &&= .reverse_merge @@defaults # Otherwise, parse them from ARGV ||= parse! # Stash them away for later @@options = puts "Starting MailCatcher" #Thin::Logging.silent = true # One EventMachine loop... EventMachine.run do # Get our lion on if asked MailCatcher::Growl.start if [:growl] smtp_url = "smtp://#{[:smtp_ip]}:#{[:smtp_port]}" http_url = "http://#{[:http_ip]}:#{[:http_port]}" # 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 # (Skinny/WebSockets just works on the inside) # rescue_port options[:http_port] do #Thin::Server.start options[:http_ip], options[:http_port], Web # puts "==> #{http_url}" # end # Open the web browser before detatching 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) ⇒ Object
11 12 13 14 15 |
# File 'lib/mail_catcher.rb', line 11 def which command not windows? and Open3.popen3 'which', 'command' do |stdin, stdout, stderr| return stdout.read.chomp.presence end end |
#windows? ⇒ Boolean
21 22 23 |
# File 'lib/mail_catcher.rb', line 21 def windows? RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ end |