Class: Localtunnel::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/localtunnel/client.rb

Constant Summary collapse

@@pid =
nil
@@url =
nil

Class Method Summary collapse

Class Method Details

.execution_string(log, options) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/localtunnel/client.rb', line 45

def self.execution_string(log, options)
  s = "exec lt"
  s << " -p '#{options[:port]}'" if options.key?(:port)
  s << " -s '#{options[:subdomain]}'" if options.key?(:subdomain)
  s << " -h '#{options[:remote_host]}'" if options.key?(:remote_host)
  s << " -l '#{options[:local_host]}'" if options.key?(:local_host)
  s << " > #{log.path}"
end

.package_installed?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/localtunnel/client.rb', line 39

def self.package_installed?
  !`lt --version`.to_s.empty?
rescue Errno::ENOENT
  false
end

.parse_url!(log, max_seconds = 10) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/localtunnel/client.rb', line 54

def self.parse_url!(log, max_seconds = 10)
  max_seconds.times do
    sleep 1
    raise ClientConnectionFailedError unless running?

    log.rewind
    if match_data = log.read.match(/^your url is: (.*)$/)
      return match_data.captures[0]
    end
  end

  stop
  raise ClientConnectionFailedError
end

.running?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/localtunnel/client.rb', line 31

def self.running?
  !@@pid.nil?
end

.start(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/localtunnel/client.rb', line 8

def self.start(options = {})
  return if running?

  raise NpmPackageNotFoundError unless package_installed?

  log = Tempfile.new("localtunnel")
  @@pid = Process.spawn(execution_string(log, options))
  @@url = parse_url!(log)

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

  return # Explicitly return nil.
end

.stopObject



22
23
24
25
26
27
28
29
# File 'lib/localtunnel/client.rb', line 22

def self.stop
  return unless running?

  `kill -9 #{@@pid} 1>/dev/null 2>&1` # Can't use Process.kill, since JRuby on Raspbian doesn't support it.
  @@pid = nil

  return # Explicitly return nil.
end

.urlObject



35
36
37
# File 'lib/localtunnel/client.rb', line 35

def self.url
  @@url if running?
end