Class: Launchy::Rails::Opener

Inherits:
Object
  • Object
show all
Defined in:
lib/launchy/rails/opener.rb

Instance Method Summary collapse

Constructor Details

#initialize(protocol, host, port) ⇒ Opener

Returns a new instance of Opener.



4
5
6
7
8
# File 'lib/launchy/rails/opener.rb', line 4

def initialize(protocol, host, port)
  @host = host
  @port = port
  @protocol = protocol
end

Instance Method Details

#open_when_readyObject



10
11
12
13
14
15
16
17
18
# File 'lib/launchy/rails/opener.rb', line 10

def open_when_ready
  url = "#{@protocol}://#{@host}:#{@port}"
  Thread.new do
    while !port_open?(@host, @port, 1)
      sleep 1
    end
    Launchy.open(url)
  end
end

#port_open?(ip, port, seconds = 1) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/launchy/rails/opener.rb', line 20

def port_open?(ip, port, seconds=1)
  Timeout::timeout(seconds) do
    begin
      TCPSocket.new(ip, port).close
      true
    rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
      false
    end
  end
rescue Timeout::Error
  false
end