Skip to content

Commit

Permalink
custom parameters
Browse files Browse the repository at this point in the history
Update version to 2.0.1
Add custom parameters
  • Loading branch information
Anton Bogdanovich committed Oct 24, 2014
1 parent 5f124fa commit eb14af2
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 25 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ Or install it yourself as:
```ruby
require 'ngrok/tunnel'

# spawn ngrok at local port 3001
Ngrok::Tunnel.start(3001)
# spawn ngrok (default port 3001)
Ngrok::Tunnel.start

# ngrok local_port
Ngrok::Tunnel.local_port
Ngrok::Tunnel.port
=> 3001

# ngrok external url
Expand All @@ -50,7 +50,7 @@ Ngrok::Tunnel.pid
=> 27384

# ngrok log file descriptor
Ngrok::Tunnel.log_file
Ngrok::Tunnel.log
=> #<File:/tmp/ngrok20141022-27376-cmmiq4>

# kill ngrok
Expand All @@ -59,6 +59,17 @@ Ngrok::Tunnel.stop

```

```ruby
# ngrok custom parameters
Ngrok::Tunnel.start(port: 3333,
subdomain: 'MY_SUBDOMAIN',
authtoken: 'MY_TOKEN',
log: 'ngrok.log',
config: '~/.ngrok')

```


## Contributing

1. Fork it ( https://github.com/bogdanovich/ngrok-tunnel/fork )
Expand Down
68 changes: 51 additions & 17 deletions lib/ngrok/tunnel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,31 @@ module Ngrok

class NotFound < StandardError; end
class FetchUrlError < StandardError; end
class Error < StandardError; end

class Tunnel

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

def init
@status = :stopped
def init(params = {})
@params = {port: 3001, timeout: 10, config: '/dev/null'}.merge(params)
@status = :stopped unless @status
end

def start(port, options = {})
options[:timeout] ||= 10
@options = options

def start(params = {})
ensure_binary

@local_port = port.to_i
@log_file = Tempfile.new('ngrok')
init(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 }
@status = :running
fetch_urls
end

@pid = spawn("exec ngrok -log=stdout #{@local_port} > #{@log_file.path}")
at_exit { Ngrok::Tunnel.stop }
@status = :running
fetch_urls
@ngrok_url
end

def stop
Expand All @@ -36,6 +38,7 @@ def stop
@ngrok_url = @ngrok_url_https = @pid = nil
@status = :stopped
end
@status
end

def running?
Expand All @@ -46,18 +49,49 @@ def stopped?
@status == :stopped
end

def port
@params[:port]
end

def log
@params[:log]
end

def subdomain
@params[:subdomain]
end

def authtoken
@params[:authtoken]
end

def inherited(subclass)
init
end

private

def ngrok_exec_params
exec_params = "-log=stdout "
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}"
end

def fetch_urls
@options[:timeout].times do
@ngrok_url, @ngrok_url_https = @log_file.read.scan(/"Url":"([^"]+)"/).flatten
@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

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

sleep 1
@log_file.rewind
@params[:log].rewind
end
raise FetchUrlError, "Unable to fetch external url"
@ngrok_url
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 = "1.0.1"
VERSION = "2.0.1"
end
end
25 changes: 22 additions & 3 deletions spec/tunnel/tunnel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
describe "Run process" do

before(:all) do
@local_port = 3001
Ngrok::Tunnel.start(3001)
Ngrok::Tunnel.start
end

after(:all) { Ngrok::Tunnel.stop }
Expand All @@ -39,7 +38,7 @@
end

it "should match local_port" do
expect(Ngrok::Tunnel.local_port).to eq(@local_port)
expect(Ngrok::Tunnel.port).to eq(3001)
end

it "should have valid ngrok_url" do
Expand All @@ -60,4 +59,24 @@

end

describe "Custom log file" do
it "should be able to use custom log file" do
Ngrok::Tunnel.start(log: 'test.log')
expect(Ngrok::Tunnel.running?).to eq true
expect(Ngrok::Tunnel.log.path).to eq 'test.log'
Ngrok::Tunnel.stop
expect(Ngrok::Tunnel.stopped?).to eq true
end
end

describe "Custom subdomain" do
it "should fail without authtoken" do
expect {Ngrok::Tunnel.start(subdomain: 'test-subdomain')}.to raise_error
end

it "should fail with incorrect authtoken" do
expect {Ngrok::Tunnel.start(subdomain: 'test-subdomain', authtoken: 'incorrect_token')}.to raise_error
end
end

end

0 comments on commit eb14af2

Please sign in to comment.