Skip to content

Commit

Permalink
Decompose fetch_urls for maintainability
Browse files Browse the repository at this point in the history
  • Loading branch information
texpert committed Jan 9, 2022
1 parent 02c4233 commit 8b36db2
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions lib/ngrok/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def parse_persistence_file
def raise_if_similar_ngroks(pid)
other_ngrok_on_port = ngrok_process_status_lines.find do |line|
# If found an Ngrok process with other pid, tunneling on the port, specified in Ngrok::Wrapper.start params
!line.start_with?(pid || '') && line.end_with?(addr.to_s)
line.include?('ngrok http -log') && !line.start_with?(pid || '') && line.end_with?(addr.to_s)
end

raise Ngrok::Error, "ERROR: Other ngrok instances tunneling to port #{addr} found" if other_ngrok_on_port
Expand All @@ -93,7 +93,7 @@ def raise_if_similar_ngroks(pid)

tunnel_on_other_port = ngrok_process_status_lines.find do |line|
# If the line starts with this pid, but the port is other than specified in Ngrok::Wrapper.start params
line.start_with?(pid) && !line.end_with?(addr.to_s)
line.include?('ngrok http -log') && line.start_with?(pid) && !line.end_with?(addr.to_s)
end

return unless tunnel_on_other_port
Expand Down Expand Up @@ -125,7 +125,7 @@ def try_params_from_running_ngrok
def ngrok_running?(pid)
ngrok_process_status_lines.find do |line|
# If found the Ngrok process with correct pid, tunneling on the port, specified in Ngrok::Wrapper.start params
line.start_with?(pid) && line.end_with?(addr.to_s)
line.include?('ngrok http -log') && line.start_with?(pid) && line.end_with?(addr.to_s)
end
end

Expand Down Expand Up @@ -155,17 +155,7 @@ def ngrok_exec_params

def fetch_urls
@params[:timeout].times do
log_content = @params[:log].read
result = log_content.scan(/URL:(.+)\sProto:(http|https)\s/)
unless result.empty?
result = Hash[*result.flatten].invert
@ngrok_url = result['http']
@ngrok_url_https = result['https']
break if @ngrok_url || @ngrok_url_https
end

@error = log_content.scan(/msg="command failed" err="([^"]+)"/).flatten
break unless @error.empty?
break if scan_log_for_urls || !@error.empty?

sleep 1
@params[:log].rewind
Expand All @@ -180,6 +170,20 @@ def fetch_urls
raise Ngrok::Error, @error.first
end

def scan_log_for_urls
log_content = @params[:log].read
result = log_content.scan(/URL:(.+)\sProto:(http|https)\s/)
unless result.empty?
result = Hash[*result.flatten].invert
@ngrok_url = result['http']
@ngrok_url_https = result['https']
return true if @ngrok_url || @ngrok_url_https
end

@error = log_content.scan(/msg="command failed" err="([^"]+)"/).flatten
false
end

def ensure_binary
`ngrok version`
rescue Errno::ENOENT
Expand Down

0 comments on commit 8b36db2

Please sign in to comment.