Class: Lono::Api::Proxy

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/lono/api/proxy.rb

Instance Method Summary collapse

Instance Method Details

#get(path) ⇒ Object



9
10
11
# File 'lib/lono/api/proxy.rb', line 9

def get(path)
  request(Net::HTTP::Get, path)
end

#http(url) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/lono/api/proxy.rb', line 17

def http(url)
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = http.read_timeout = 30
  http.use_ssl = true if uri.scheme == 'https'
  http
end

#post(path, data = {}) ⇒ Object



13
14
15
# File 'lib/lono/api/proxy.rb', line 13

def post(path, data={})
  request(Net::HTTP::Post, path, data)
end

#request(klass, path, data = {}) ⇒ Object



26
27
28
29
30
31
# File 'lib/lono/api/proxy.rb', line 26

def request(klass, path, data={})
  url = url(path)
  http = http(url)
  req = send_request(klass, url, data)
  http.request(req) # send request
end

#send_request(klass, url, data = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lono/api/proxy.rb', line 33

def send_request(klass, url, data={})
  data.merge!(
    lono_version: Lono::VERSION,
    lono_command: lono_command,
  )
  req = klass.new(url) # url includes query string and uri.path does not, must used url
  if [Net::HTTP::Post, Net::HTTP::Put].include?(klass)
    text = JSON.dump(data)
    req.body = text
    req.content_length = text.bytesize
  end
  req
end

#url(path) ⇒ Object

Lono::API does not include the /. IE: localhost:8888 path includes the /. IE: “/blueprints”



49
50
51
# File 'lib/lono/api/proxy.rb', line 49

def url(path)
  "#{Lono::API}/#{path}"
end