class NgrokV1::Client

Public Class Methods

new(options = {}) click to toggle source
# File lib/ngrok-v1/client.rb, line 5
def initialize(options = {})
  raise "Failed to find ngrok v1 binary." unless binary_installed?
  raise "Port number not specified." unless options.key?(:port)
  raise "Protocol not specified." unless options.key?(:protocol)

  @port = options[:port]
  @protocol = options[:protocol]
  @log = nil
  @pid = nil
  @uri = nil
end

Public Instance Methods

running?() click to toggle source
# File lib/ngrok-v1/client.rb, line 36
def running?
  !@pid.nil?
end
start() click to toggle source
# File lib/ngrok-v1/client.rb, line 17
def start
  return if running?

  @log = Tempfile.new("ngrok-v1")
  @pid = Process.spawn(execution_string)

  at_exit { stop } # Ensure process is killed if Ruby exits.

  @uri = parse_uri!
end
stop() click to toggle source
# File lib/ngrok-v1/client.rb, line 28
def stop
  return unless running?

  Process.kill("KILL", @pid)

  @pid = nil
end
uri() click to toggle source
# File lib/ngrok-v1/client.rb, line 40
def uri
  @uri if running?
end

Private Instance Methods

binary_installed?() click to toggle source
# File lib/ngrok-v1/client.rb, line 46
def binary_installed?
  !`ngrok version`.empty?
rescue Errno::ENOENT
  false
end
execution_string() click to toggle source
# File lib/ngrok-v1/client.rb, line 52
def execution_string
  s = "exec ngrok"
  s << " -log stdout"
  s << " -proto #{@protocol}" if @protocol
  s << " #{@port}" if @port
  s << " > #{@log.path} 2>&1"
end
parse_uri!(max_seconds = 10) click to toggle source
# File lib/ngrok-v1/client.rb, line 60
def parse_uri!(max_seconds = 10)
  max_seconds.times do
    sleep 1
    @log.rewind

    if match_data = @log.read.match(/Tunnel established at (.*)$/)
      return match_data.captures.first
    end
  end

  stop
  raise "Failed to establish tunnel." # Timeout.
end