Class: Stretchr::JSONTransporter
- Inherits:
-
Object
- Object
- Stretchr::JSONTransporter
- Defined in:
- lib/stretchr/transporters/json_transporter.rb
Instance Method Summary collapse
-
#generate_request(request) ⇒ Object
Generates the actual request, including the method.
-
#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.
Instance Method Details
#generate_request(request) ⇒ Object
Generates the actual request, including the method
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/stretchr/transporters/json_transporter.rb', line 29 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
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/stretchr/transporters/json_transporter.rb', line 11 def make_request(request) response = nil #convert to a json string unless the user already did it... request[:body] = request[:body].to_json unless request[:body].is_a?(String) Net::HTTP.start(request[:uri].host, request[:uri].port, {use_ssl: true}) 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 |