Sha256: 74db8a27dc06cafca6f61762f7e0d904e80b2dcc74a421bd04b756c9bca4dcd4
Contents?: true
Size: 1.49 KB
Versions: 1
Compression:
Stored size: 1.49 KB
Contents
# frozen_string_literal: true require "uri" require "net/http" require "json" require "rom/support/inflector" module ROM module HTTP # Default request/response handlers # # @api public class Handlers # Default handler for JSON requests # # @api public class JSONRequest # Handle JSON request for the provided dataset # # @param [Dataset] dataset # # @return [Array<Hash>] # # @api public 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), request| request[header.to_s] = value end request.body = JSON.dump(dataset.body_params) if dataset.body_params.any? http.request(request) end end # Default handler for JSON responses # # @api public class JSONResponse # Handle JSON responses # # @param [Net::HTTP::Response] response # @param [Dataset] dataset # # @return [Array<Hash>] # # @api public def self.call(response, _dataset) Array([JSON.parse(response.body)]).flatten(1) end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rom-http-0.9.0 | lib/rom/http/handlers/json.rb |