lib/rom/http/dataset.rb in rom-http-0.8.0 vs lib/rom/http/dataset.rb in rom-http-0.9.0
- old
+ new
@@ -1,17 +1,17 @@
# frozen_string_literal: true
-require 'uri'
+require "uri"
-require 'dry/configurable'
-require 'dry/core/deprecations'
+require "dry/configurable"
+require "dry/core/deprecations"
-require 'rom/support/memoizable'
-require 'rom/constants'
-require 'rom/initializer'
-require 'rom/http/types'
-require 'rom/http/transformer'
+require "rom/support/memoizable"
+require "rom/constants"
+require "rom/initializer"
+require "rom/http/types"
+require "rom/http/transformer"
module ROM
module HTTP
# HTTP Dataset
#
@@ -20,11 +20,11 @@
# response/request handlers or any other configuration that should
# differ from the defaults.
#
# @api public
class Dataset
- PATH_SEPARATOR = '/'.freeze
+ PATH_SEPARATOR = "/"
extend Dry::Configurable
extend ROM::Initializer
include ROM::Memoizable
@@ -57,23 +57,23 @@
#
# MyDataset.default_response_handler # MyResponseHandler
# MyDataset.new(uri: "http://localhost").response_handler # MyResponseHandler
setting :default_response_handler, reader: true
- # @!method self.param_encoder
- # Return configured param encoder
+ # @!method self.query_param_encoder
+ # Return configured query param encoder
#
# @example
# class MyDataset < ROM::HTTP::Dataset
# configure do |config|
- # config.param_encoder = MyParamEncoder
+ # config.query_param_encoder = MyParamEncoder
# end
# end
#
- # MyDataset.param_encoder # MyParamEncoder
- # MyDataset.new(uri: "http://localhost").param_encoder # MyParamEncoder
- setting :param_encoder, URI.method(:encode_www_form), reader: true
+ # MyDataset.query_param_encoder # MyParamEncoder
+ # MyDataset.new(uri: "http://localhost").query_param_encoder # MyParamEncoder
+ setting :query_param_encoder, default: URI.method(:encode_www_form), reader: true
# @!attribute [r] request_handler
# @return [Object]
# @api public
option :request_handler, default: proc { self.class.default_request_handler }
@@ -96,24 +96,29 @@
# @!attribute [r] path
# @return [String]
# @api public
option :path, type: Types::Path, default: proc { EMPTY_STRING }
- # @!attribute [r] params
+ # @!attribute [r] query_params
# @return [Hash]
# @api public
- option :params, type: Types::Hash, default: proc { EMPTY_HASH }
+ option :query_params, type: Types::Hash, default: proc { EMPTY_HASH }
+ # @!attribute [r] body_params
+ # @return [Hash]
+ # @api public
+ option :body_params, type: Types::Hash, default: proc { EMPTY_HASH }
+
# @!attribute [r] headers
# @return [Hash]
# @api public
option :headers, type: Types::Hash, default: proc { EMPTY_HASH }
# @!attribute [r] headers
# @return [Hash]
# @api public
- option :param_encoder, default: proc { self.class.param_encoder }
+ option :query_param_encoder, default: proc { self.class.query_param_encoder }
# @!attribute [r] uri
# @return [String]
# @api public
option :uri, type: Types::String
@@ -124,12 +129,12 @@
#
# @api public
def uri
uri = URI(join_path(super, path))
- if get? && params.any?
- uri.query = param_encoder.call(params)
+ if query_params.any?
+ uri.query = query_param_encoder.call(query_params)
end
uri
end
@@ -237,11 +242,11 @@
#
# @return [Dataset]
#
# @api public
def with_options(opts)
- __new__(options.merge(opts))
+ __new__(**options.merge(opts))
end
# Return a new dataset with a different base path
#
# @param base_path [String] the new base request path
@@ -299,75 +304,116 @@
# @api public
def with_request_method(request_method)
with_options(request_method: request_method)
end
- # Return a new dataset with replaced request parameters
+ # Return a new dataset with replaced request query parameters
#
- # @param [Hash] params the new request parameters
+ # @param [Hash] query_params the new request query parameters
#
# @example
- # users = Dataset.new(params: { uid: 33 })
- # users.with_params(login: 'jdoe').params
+ # users = Dataset.new(query_params: { uid: 33 })
+ # users.with_query_params(login: 'jdoe').query_params
# # => { :login => 'jdoe' }
#
# @return [Dataset]
#
# @api public
- def with_params(params)
- with_options(params: params)
+ def with_query_params(query_params)
+ with_options(query_params: query_params)
end
- # Return a new dataset with merged request parameters
+ # Return a new dataset with merged request query parameters
#
- # @param [Hash] params the new request parameters to add
+ # @param [Hash] query_params the new request query parameters to add
#
# @example
- # users = Dataset.new(params: { uid: 33 })
- # users.add_params(login: 'jdoe').params
+ # users = Dataset.new(query_params: { uid: 33 })
+ # users.add_query_params(login: 'jdoe').query_params
# # => { uid: 33, :login => 'jdoe' }
#
# @return [Dataset]
#
# @api public
- def add_params(new_params)
- with_options(params: ::ROM::HTTP::Transformer[:deep_merge][params, new_params])
+ def add_query_params(new_query_params)
+ with_options(query_params: ::ROM::HTTP::Transformer[:deep_merge][query_params,
+ new_query_params])
end
+ # Return a new dataset with replaced request body parameters
+ #
+ # @param [Hash] body_params the new request body parameters
+ #
+ # @example
+ # users = Dataset.new(body_params: { uid: 33 })
+ # users.with_body_params(login: 'jdoe').body_params
+ # # => { :login => 'jdoe' }
+ #
+ # @return [Dataset]
+ #
+ # @api public
+ def with_body_params(body_params)
+ with_options(body_params: body_params)
+ end
+
+ # Return a new dataset with merged request body parameters
+ #
+ # @param [Hash] body_params the new request body parameters to add
+ #
+ # @example
+ # users = Dataset.new(body_params: { uid: 33 })
+ # users.add_body_params(login: 'jdoe').body_params
+ # # => { uid: 33, :login => 'jdoe' }
+ #
+ # @return [Dataset]
+ #
+ # @api public
+ def add_body_params(new_body_params)
+ with_options(body_params: ::ROM::HTTP::Transformer[:deep_merge][body_params,
+ new_body_params])
+ end
+
# Iterate over each response value
#
# @yield [Hash] a dataset tuple
#
# @return [Enumerator] if no block is given
# @return [Array<Hash>]
#
# @api public
def each(&block)
return to_enum unless block_given?
+
response.each(&block)
end
# Perform an insert over HTTP Post
#
- # @params [Hash] params The request parameters to send
+ # @param [Hash] attributes the attributes to insert
#
# @return [Array<Hash>]
#
# @api public
- def insert(params)
- with_options(request_method: :post, params: params).response
+ def insert(attributes)
+ with_options(
+ request_method: :post,
+ body_params: attributes
+ ).response
end
# Perform an update over HTTP Put
#
- # @params [Hash] params The request parameters to send
+ # @param [Hash] attributes the attributes to update
#
# @return [Array<Hash>]
#
# @api public
- def update(params)
- with_options(request_method: :put, params: params).response
+ def update(attributes)
+ with_options(
+ request_method: :put,
+ body_params: attributes
+ ).response
end
# Perform an delete over HTTP Delete
#
#
@@ -390,11 +436,11 @@
memoize :uri, :absolute_path
private
# @api private
- def __new__(*args, &block)
- self.class.new(*args, &block)
+ def __new__(*args, **kwargs, &block)
+ self.class.new(*args, **kwargs, &block)
end
# @api private
def join_path(*paths)
paths.reject(&:empty?).join(PATH_SEPARATOR)