Class: Rox::Core::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/rox/core/network/request.rb

Instance Method Summary collapse

Instance Method Details

#send(request, uri, payload) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rox/core/network/request.rb', line 23

def send(request, uri, payload)
  request['Accept-Encoding'] = 'gzip'

  unless payload.nil?
    request['Content-Type'] = 'application/json'
    request.body = JSON.dump(payload)
  end

  resp = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
    http.request(request)
  end

  resp_body = resp.body
  if resp['Content-Encoding'].eql?('gzip')
    sio = StringIO.new(resp.body)
    gz = Zlib::GzipReader.new(sio)
    resp_body = gz.read
  end

  Response.new(resp.code.to_i, resp_body, resp['Content-Type'])
end

#send_get(request_data) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/rox/core/network/request.rb', line 8

def send_get(request_data)
  uri = URI(request_data.url)
  uri.query = URI.encode_www_form(request_data.query_params)
  req = Net::HTTP::Get.new(uri)

  send(req, uri, nil)
end

#send_post(url, payload) ⇒ Object



16
17
18
19
20
21
# File 'lib/rox/core/network/request.rb', line 16

def send_post(url, payload)
  uri = URI(url)
  req = Net::HTTP::Post.new(uri)

  send(req, uri, payload)
end