Module: Gretl
Defined Under Namespace
Classes: Client, PortInfo
Constant Summary
collapse
- BASE =
"http://127.0.0.1:27892"
- PA =
Client.new
Class Method Summary
collapse
Class Method Details
._daemon_running? ⇒ Boolean
10
11
12
13
14
15
|
# File 'lib/gretl.rb', line 10
def _daemon_running?
uri = URI("#{BASE}/health")
Net::HTTP.get_response(uri).is_a?(Net::HTTPSuccess)
rescue
false
end
|
._ensure_daemon ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/gretl.rb', line 17
def _ensure_daemon
return if _daemon_running?
gr = `which gr 2>/dev/null`.strip
raise "Gretl daemon is not running and `gr` was not found on PATH.\nInstall with: npm install -g @gretl/cli then run: gr daemon start" if gr.empty?
spawn(gr, "daemon", "start", out: File::NULL, err: File::NULL)
25.times do
sleep 0.2
return if _daemon_running?
end
raise "Gretl daemon did not start in time. Run: gr daemon start"
end
|
._fetch_ports ⇒ Object
58
59
60
|
# File 'lib/gretl.rb', line 58
def _fetch_ports
_request(:get, "/ports").map { |d| PortInfo.new(**d.slice(*PortInfo.members)) }
end
|
._request(method, path) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/gretl.rb', line 41
def _request(method, path)
_ensure_daemon
uri = URI("#{BASE}#{path}")
http = Net::HTTP.new(uri.host, uri.port)
http.open_timeout = 5
http.read_timeout = 5
req = case method
when :get then Net::HTTP::Get.new(uri)
when :post then Net::HTTP::Post.new(uri)
when :delete then Net::HTTP::Delete.new(uri)
end
res = http.request(req)
JSON.parse(res.body, symbolize_names: true)
rescue Errno::ECONNREFUSED => e
raise "Cannot connect to Gretl daemon: #{e}"
end
|
._resolve(query) ⇒ Object
62
63
64
65
66
67
68
69
70
|
# File 'lib/gretl.rb', line 62
def _resolve(query)
all = _fetch_ports
if query.match?(/\A\d+\z/)
exact = all.select { |p| p.port == query.to_i }
return exact unless exact.empty?
end
q = query.downcase
all.select { |p| p.name.downcase.include?(q) }
end
|