Class: ROM::HTTP::Handlers::JSONRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/rom/http/handlers/json.rb

Overview

Default handler for JSON requests

Class Method Summary collapse

Class Method Details

.call(dataset) ⇒ Array<Hash>

Handle JSON request for the provided dataset

Parameters:

Returns:

  • (Array<Hash>)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rom/http/handlers/json.rb', line 26

def self.call(dataset)
  uri = dataset.uri

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true if uri.scheme.eql?("https")

  request_class = Net::HTTP.const_get(ROM::Inflector.classify(dataset.request_method))

  request = request_class.new(uri.request_uri)

  dataset.headers.each_with_object(request) do |(header, value), req|
    req[header.to_s] = value
  end

  request.body = JSON.dump(dataset.body_params) if dataset.body_params.any?

  http.request(request)
end