Module: YuiRestClient::Http::HttpClient

Defined in:
lib/yui_rest_client/http/http_client.rb

Class Method Summary collapse

Class Method Details

.compose_uri(host, port, path, params = {}) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/yui_rest_client/http/http_client.rb', line 26

def compose_uri(host, port, path, params = {})
  URI::HTTP.build(
    host: host,
    port: port,
    path: path,
    query: URI.encode_www_form(params)
  )
end

.http_get(uri) ⇒ Object



8
9
10
11
12
13
# File 'lib/yui_rest_client/http/http_client.rb', line 8

def http_get(uri)
  YuiRestClient.logger.debug("Request: [GET] #{uri}")
  res = Net::HTTP.get_response(uri)
  YuiRestClient.logger.debug("Response: [#{res.code}]\n#{res.body}")
  res
end

.http_post(uri) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/yui_rest_client/http/http_client.rb', line 15

def http_post(uri)
  YuiRestClient.logger.debug("Request: [POST] #{uri}")
  # a trick how to add query parameters to a POST request,
  # the usual Net::HTTP.post(uri, data) does not allow using a query
  req = Net::HTTP::Post.new("#{uri.path}?#{uri.query}")
  http = Net::HTTP.new(uri.host, uri.port)
  res = http.request(req)
  YuiRestClient.logger.debug("Response: [#{res.code}]\n#{res.body}")
  res
end