Module: Gretl

Defined in:
lib/gretl.rb

Defined Under Namespace

Classes: Client, PortInfo

Constant Summary collapse

BASE =
"http://127.0.0.1:27892"
PA =

Default client

Client.new

Class Method Summary collapse

Class Method Details

._fetch_portsObject



36
37
38
# File 'lib/gretl.rb', line 36

def _fetch_ports
  _request(:get, "/ports").map { |d| PortInfo.new(**d.slice(*PortInfo.members)) }
end

._request(method, path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gretl.rb', line 20

def _request(method, path)
  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
  raise "Cannot connect to Gretl. Make sure the app is running."
end

._resolve(query) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/gretl.rb', line 40

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