Class: Stretchr::JSONTransporter

Inherits:
Object
  • Object
show all
Defined in:
lib/stretchr/transporters/json_transporter.rb

Instance Method Summary collapse

Instance Method Details

#generate_request(request) ⇒ Object

Generates the actual request, including the method



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/stretchr/transporters/json_transporter.rb', line 23

def generate_request(request)

  request_uri = request[:uri].request_uri

  case request[:method]
  when :get
    req = Net::HTTP::Get.new request_uri
  when :post
    req = Net::HTTP::Post.new request_uri, {'Content-Type' => "application/json"}
    req.body = request[:body]
    req
  when :put
    req = Net::HTTP::Put.new request_uri, {'Content-Type' => "application/json"}
    req.body = request[:body]
    req
  when :patch
    req = Net::HTTP::Patch.new request_uri, {'Content-Type' => "application/json"}
    req.body = request[:body]
    req
  when :delete
    req = Net::HTTP::Delete.new request_uri
  end
end

#make_request(request) ⇒ Object

Perform the request against stretchr

Parameters

request - The request object Expects: URIOBJECT, body: “body”, method: “PUT/PATCH/POST/DELETE/GET”, client: stretchr client



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/stretchr/transporters/json_transporter.rb', line 9

def make_request(request)
  response = nil
  Net::HTTP.start(request[:uri].host, request[:uri].port) do |http|
    http_request = generate_request(request)
    response = http.request http_request # Net::HTTPResponse object
  end
  if request[:client]
    return Stretchr::Response.new(response.body, {:api_version => request[:client].api_version})
  else
    return Stretchr::Response.new(response.body)
  end
end