Class: Ngrok::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/ngrok/wrapper.rb,
lib/ngrok/wrapper/version.rb

Constant Summary collapse

VERSION =
'0.3.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.ngrok_urlObject (readonly)

Returns the value of attribute ngrok_url.



24
25
26
# File 'lib/ngrok/wrapper.rb', line 24

def ngrok_url
  @ngrok_url
end

.ngrok_url_httpsObject (readonly)

Returns the value of attribute ngrok_url_https.



24
25
26
# File 'lib/ngrok/wrapper.rb', line 24

def ngrok_url_https
  @ngrok_url_https
end

.paramsObject (readonly)

Returns the value of attribute params.



24
25
26
# File 'lib/ngrok/wrapper.rb', line 24

def params
  @params
end

.pidObject (readonly)

Returns the value of attribute pid.



24
25
26
# File 'lib/ngrok/wrapper.rb', line 24

def pid
  @pid
end

.statusObject (readonly)

Returns the value of attribute status.



24
25
26
# File 'lib/ngrok/wrapper.rb', line 24

def status
  @status
end

Class Method Details

.addrObject



73
74
75
# File 'lib/ngrok/wrapper.rb', line 73

def addr
  @params[:addr]
end

.inherited(subclass) ⇒ Object



83
84
85
86
# File 'lib/ngrok/wrapper.rb', line 83

def inherited(subclass)
  super
  init
end

.init(params = {}) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/ngrok/wrapper.rb', line 26

def init(params = {})
  # map old key 'port' to 'addr' to maintain backwards compatibility with versions 2.0.21 and earlier
  params[:addr] = params.delete(:port) if params.key?(:port)

  @params = { addr: 3001, timeout: 10, config: '/dev/null' }.merge!(params)
  @status ||= :stopped # rubocop:disable Naming/MemoizedInstanceVariableName
end

.portObject



77
78
79
80
81
# File 'lib/ngrok/wrapper.rb', line 77

def port
  return addr if addr.is_a?(Numeric)

  addr.split(':').last.to_i
end

.running?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/ngrok/wrapper.rb', line 65

def running?
  @status == :running
end

.start(params = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ngrok/wrapper.rb', line 34

def start(params = {})
  ensure_binary
  @current_version_params = VERSION_PARAMS[@major_version]
  @flag_prefix = @current_version_params[:flag_prefix]

  init(params)

  persistent_ngrok = @params[:persistence] == true
  # Attempt to read the attributes of an existing process instead of starting a new process.
  try_params_from_running_ngrok if persistent_ngrok

  spawn_new_ngrok(persistent_ngrok: persistent_ngrok) if stopped?

  @status = :running
  if persistent_ngrok
    # Record the attributes of the new process so that it can be reused on a subsequent call.
    File.write(@persistence_file, { pid: @pid, ngrok_url: @ngrok_url, ngrok_url_https: @ngrok_url_https }.to_json)
  end

  @ngrok_url_https || @ngrok_url
end

.stopObject



56
57
58
59
60
61
62
63
# File 'lib/ngrok/wrapper.rb', line 56

def stop
  if running?
    Process.kill(9, @pid)
    @ngrok_url = @ngrok_url_https = @pid = nil
    @status = :stopped
  end
  @status
end

.stopped?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/ngrok/wrapper.rb', line 69

def stopped?
  @status == :stopped
end