Sha256: 21284f8bb883b4fbc0b17afc11ea5d3eb68e2f3b421ed9e0379a29960897c187

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 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 = URI(dataset.uri)

          uri.path = dataset.absolute_path
          uri.query = URI.encode_www_form(dataset.params)

          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

          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.8.0 lib/rom/http/handlers/json.rb