Module: Openwhenready
- Defined in:
- lib/openwhenready.rb,
lib/openwhenready/version.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Class Method Summary collapse
Class Method Details
.normalize_url(url) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/openwhenready.rb', line 44 def normalize_url(url) # On OS X localhost gets resolved to an ipv6 address, but most # webservers only listen to ipv4, so let's hardcode ipv4 for # localhost url = url.gsub("localhost", "127.0.0.1") if url =~ /^[^:]+:\/\// url else "http://#{url}" end end |
.open(url) ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/openwhenready.rb', line 11 def open(url) url = normalize_url(url) uri = URI(url) ip, port = Resolv.getaddress(uri.host), uri.port puts " waiting for #{url} to accept connections " sleep 1 until port_open?(ip, port) puts " opening #{url} in your browser " Launchy.open(url) end |
.port_open?(ip, port) ⇒ Boolean
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/openwhenready.rb', line 21 def port_open?(ip, port) # From http://stackoverflow.com/questions/3464551/shortening-socket-timeout-using-timeouttimeoutn-does-not-seem-to-work-for-me/3473208#3473208 socket_family = ip.include?(":") ? Socket::AF_INET6 : Socket::AF_INET s = Socket.new(socket_family, Socket::SOCK_STREAM, 0) sa = Socket.sockaddr_in(port, ip) begin s.connect_nonblock(sa) rescue Errno::EINPROGRESS if IO.select(nil, [s], nil, 1) begin s.connect_nonblock(sa) rescue Errno::EISCONN return true rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH return false end end end return false end |