Skip to content

Commit

Permalink
Support ngrok version 2.0.16
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Bogdanovich authored and Anton Bogdanovich committed Apr 19, 2015
1 parent 2e38289 commit 956f01e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
23 changes: 14 additions & 9 deletions lib/ngrok/tunnel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class FetchUrlError < StandardError; end
class Error < StandardError; end

class Tunnel

class << self
attr_reader :pid, :ngrok_url, :ngrok_url_https, :status

Expand All @@ -23,12 +23,12 @@ def start(params = {})

if stopped?
@params[:log] = (@params[:log]) ? File.open(@params[:log], 'w+') : Tempfile.new('ngrok')
@pid = spawn("exec ngrok " + ngrok_exec_params)
at_exit { Ngrok::Tunnel.stop }
@pid = spawn("exec ngrok http " + ngrok_exec_params)
at_exit { Ngrok::Tunnel.stop }
@status = :running
fetch_urls
end

@ngrok_url
end

Expand Down Expand Up @@ -72,7 +72,7 @@ def inherited(subclass)
private

def ngrok_exec_params
exec_params = "-log=stdout "
exec_params = "-log=stdout -log-level=debug "
exec_params << "-authtoken=#{@params[:authtoken]} " if @params[:authtoken]
exec_params << "-subdomain=#{@params[:subdomain]} " if @params[:subdomain]
exec_params << "-config=#{@params[:config]} #{@params[:port].to_i} > #{@params[:log].path}"
Expand All @@ -81,15 +81,20 @@ def ngrok_exec_params
def fetch_urls
@params[:timeout].times do
log_content = @params[:log].read
@ngrok_url, @ngrok_url_https = log_content.scan(/"Url":"([^"]+)"/).flatten
return @ngrok_url if @ngrok_url
result = log_content.scan(/URL:(.+)\sProto:(http|https)\s/)
if !result.empty?
result = Hash[*result.flatten].invert
@ngrok_url = result['http']
@ngrok_url_https = result['https']
return @ngrok_url if @ngrok_url
end

error = log_content.scan(/"Error":"([^"]+)"/).flatten
error = log_content.scan(/msg="command failed" err="([^"]+)"/).flatten
unless error.empty?
self.stop
raise Ngrok::Error, error.first
end

sleep 1
@params[:log].rewind
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ngrok/tunnel/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Ngrok
class Tunnel
VERSION = "2.0.1"
VERSION = "2.0.16"
end
end
8 changes: 2 additions & 6 deletions spec/tunnel/tunnel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,17 @@
end

it "should have valid ngrok_url" do
expect(Ngrok::Tunnel.ngrok_url).to be =~ /http:\/\/.*ngrok.com$/
expect(Ngrok::Tunnel.ngrok_url).to be =~ /http:\/\/.*ngrok\.io$/
end

it "should have valid ngrok_url_https" do
expect(Ngrok::Tunnel.ngrok_url_https).to be =~ /https:\/\/.*ngrok.com$/
expect(Ngrok::Tunnel.ngrok_url_https).to be =~ /https:\/\/.*ngrok\.io$/
end

it "should have pid > 0" do
expect(Ngrok::Tunnel.pid).to be > 0
end

it "should present in process list" do
expect(Ngrok::Tunnel.pid.to_s).to eq `pidof ngrok`.strip
end

end

describe "Custom log file" do
Expand Down

0 comments on commit 956f01e

Please sign in to comment.