Class: Localtunnel::Client
- Inherits:
-
Object
- Object
- Localtunnel::Client
- Defined in:
- lib/localtunnel/client.rb
Constant Summary collapse
- @@pid =
nil
- @@url =
nil
Class Method Summary collapse
- .execution_string(log, options) ⇒ Object
- .package_installed? ⇒ Boolean
- .parse_url!(log, max_seconds = 10) ⇒ Object
- .running? ⇒ Boolean
- .start(options = {}) ⇒ Object
- .stop ⇒ Object
- .url ⇒ Object
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, ) s = "exec lt" s << " -p '#{[:port]}'" if .key?(:port) s << " -s '#{[:subdomain]}'" if .key?(:subdomain) s << " -h '#{[:remote_host]}'" if .key?(:remote_host) s << " -l '#{[:local_host]}'" if .key?(:local_host) s << " > #{log.path}" end |
.package_installed? ⇒ 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
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( = {}) return if running? raise NpmPackageNotFoundError unless package_installed? log = Tempfile.new("localtunnel") @@pid = Process.spawn(execution_string(log, )) @@url = parse_url!(log) at_exit { stop } # Ensure process is killed if Ruby exits. return # Explicitly return nil. end |
.stop ⇒ Object
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 |
.url ⇒ Object
35 36 37 |
# File 'lib/localtunnel/client.rb', line 35 def self.url @@url if running? end |