lib/rom/http/dataset.rb in rom-http-0.6.0 vs lib/rom/http/dataset.rb in rom-http-0.7.0

- old
+ new

@@ -1,51 +1,54 @@ +require 'uri' +require 'dry-configurable' +require 'dry/core/deprecations' require 'rom/initializer' +require 'rom/http/dataset/class_interface' module ROM module HTTP # HTTP Dataset # # Represents a specific HTTP collection resource # # @api public class Dataset PATH_SEPARATOR = '/'.freeze - STRIP_PATH = ->(path) { path.sub(%r{\A/}, '') }.freeze - include Enumerable - include Dry::Equalizer(:config, :options) extend ::ROM::Initializer + extend ::Dry::Configurable + extend ::ROM::HTTP::Dataset::ClassInterface + include ::Enumerable + include ::Dry::Equalizer(:config, :options) + setting :default_request_handler + setting :default_response_handler + setting :param_encoder, ->(params) { URI.encode_www_form(params) } + param :config option :request_method, type: Types::Symbol, default: proc { :get }, reader: true option :base_path, type: Types::String, default: proc { name } - option :path, type: Types::String, default: proc { '' } + option :path, type: Types::String, default: proc { '' }, reader: false option :params, type: Types::Hash, default: proc { {} }, reader: true option :headers, type: Types::Hash, default: proc { {} } - class << self - def default_request_handler(handler = Undefined) - return @default_request_handler if Undefined === handler - @default_request_handler = handler - end - - def default_response_handler(handler = Undefined) - return @default_response_handler if Undefined === handler - @default_response_handler = handler - end - end - # Return the gateway's URI # # @return [String] # # @raise [Error] if the configuration does not contain a URI # # @api public def uri - config.fetch(:uri) { fail Error, ':uri configuration missing' } + uri = config.fetch(:uri) { fail Error, '+uri+ configuration missing' } + uri = URI(join_path(uri, path)) + if request_method == :get && params.any? + uri.query = self.class.config.param_encoder.call(params) + end + + uri end # Return request headers # # Merges default headers from the Gateway configuration and the @@ -81,11 +84,11 @@ # # @return [String] the dataset path, without a leading slash # # @api public def base_path - STRIP_PATH.call(super) + strip_path(super) end # Return the dataset path # # @example @@ -94,11 +97,11 @@ # # @return [String] the dataset path, without a leading slash # # @api public def path - STRIP_PATH.call(join_path(base_path, super)) + join_path(base_path, strip_path(options[:path].to_s)) end # Return the dataset path # # @example @@ -232,10 +235,29 @@ # @api public def with_params(params) with_options(params: params) end + # Return a new dataset with merged request parameters + # + # @param [Hash] params the new request parameters to add + # + # @example + # users = Dataset.new(config, params: { uid: 33 }) + # users.add_params(login: 'jdoe').params + # # => { uid: 33, :login => 'jdoe' } + # + # @return [Dataset] + # + # @api public + def add_params(new_params) + # TODO: Should we merge arrays? + with_options( + params: ::ROM::HTTP::Transformer[:deep_merge][params, new_params] + ) + end + # Iterate over each response value # # @yield [Hash] a dataset tuple # # @return [Enumerator] if no block is given @@ -297,26 +319,36 @@ end private def response_handler - response_handler = config.fetch(:response_handler, self.class.default_response_handler) - fail Error, ':response_handler configuration missing' if response_handler.nil? + response_handler = config.fetch( + :response_handler, + self.class.config.default_response_handler + ) + fail Error, '+default_response_handler+ configuration missing' if response_handler.nil? response_handler end def request_handler - request_handler = config.fetch(:request_handler, self.class.default_request_handler) - fail Error, ':response_handler configuration missing' if request_handler.nil? + request_handler = config.fetch( + :request_handler, + self.class.config.default_request_handler + ) + fail Error, '+default_response_handler+ configuration missing' if request_handler.nil? request_handler end def __new__(*args, &block) self.class.new(*args, &block) end def join_path(*paths) paths.reject(&:empty?).join(PATH_SEPARATOR) + end + + def strip_path(path) + path.sub(%r{\A/}, '') end end end end