lib/rom/http/dataset.rb in rom-http-0.5.0 vs lib/rom/http/dataset.rb in rom-http-0.6.0.rc1
- old
+ new
@@ -1,27 +1,29 @@
-require 'rom/http/dataset/response_transformers/schemad'
-require 'rom/http/dataset/response_transformers/schemaless'
+require 'rom/initializer'
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)
- include ROM::Options
+ extend ::ROM::Initializer
- attr_reader :config
+ param :config
- option :projections, type: ::Array, default: [], reader: true
- option :request_method, type: ::Symbol, default: :get, reader: true
- option :path, type: ::String, default: ''
- option :params, type: ::Hash, default: {}, reader: true
- option :headers, type: ::Hash, default: {}
+ 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 :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
@@ -31,43 +33,10 @@
return @default_response_handler if Undefined === handler
@default_response_handler = handler
end
end
-
- # HTTP Dataset interface
- #
- # @param [Hash] config the Gateway's HTTP server configuration
- #
- # @param [Hash] options dataset configuration options
- # @option options [Symbol] :request_method (:get) The HTTP verb to use
- # @option options [Array] :projections ([]) Keys to select from response tuple
- # @option options [String] :path ('') URI request path
- # @option options [Hash] :headers ({}) Additional request headers
- # @option options [Hash] :params ({}) HTTP request parameters
- #
- # @api public
- def initialize(config, options = {})
- @config = config
- @response_transformer = ResponseTransformers::Schemaless.new
- super(options)
- end
-
- # @overload response_transformer
- # Return the current response transformer
- #
- # @overload response_transformer(transformer)
- # Set a new response transformer
- #
- # @param transformer [#call] The new response transformer
- #
- # @api private
- def response_transformer(transformer = Undefined)
- return @response_transformer if Undefined === transformer
- @response_transformer = transformer
- end
-
# Return the gateway's URI
#
# @return [String]
#
# @raise [Error] if the configuration does not contain a URI
@@ -102,21 +71,34 @@
# @api public
def name
config[:name].to_s
end
+ # Return the base path
+ #
+ # @example
+ # Dataset.new(config, base_path: '/users').base_path
+ # # => 'users'
+ #
+ # @return [String] the dataset path, without a leading slash
+ #
+ # @api public
+ def base_path
+ STRIP_PATH.call(super)
+ end
+
# Return the dataset path
#
# @example
# Dataset.new(config, path: '/users').path
# # => 'users'
#
# @return [String] the dataset path, without a leading slash
#
# @api public
def path
- options[:path].to_s.sub(%r{\A/}, '')
+ STRIP_PATH.call(join_path(base_path, super))
end
# Return the dataset path
#
# @example
@@ -125,11 +107,11 @@
#
# @return [string] the dataset path, with leading slash
#
# @api public
def absolute_path
- '/' + path
+ PATH_SEPARATOR + path
end
# Return a new dataset with given headers
#
# @param headers [Hash] The new headers
@@ -175,28 +157,23 @@
# @api public
def with_options(opts)
__new__(config, options.merge(opts))
end
- # Add a new set projection to the result set
+ # Return a new dataset with a different base path
#
- # @param [Array, *args] projections the keys to add to the projections
+ # @param base_path [String] the new base request path
#
# @example
- # users = Dataset.new(config, projections: [:login])
- # users.project(:email, :name).projections
- # # => [:login, :email, :name]
+ # users.with_base_path('/profiles').base_path
+ # # => 'profiles'
#
# @return [Dataset]
#
# @api public
- def project(*args)
- projections = args.first.is_a?(::Array) ? args.first : args
-
- with_options(
- projections: (self.projections + projections)
- )
+ def with_base_path(base_path)
+ with_options(base_path: base_path)
end
# Return a new dataset with a different path
#
# @param path [String] the new request path
@@ -221,12 +198,12 @@
# # => users/profiles
#
# @return [Dataset]
#
# @api public
- def append_path(path)
- with_options(path: options[:path] + '/' + path)
+ def append_path(append_path)
+ with_options(path: join_path(path, append_path))
end
# Return a new dataset with a different request method
#
# @param [Symbol] request_method the new HTTP verb
@@ -314,14 +291,11 @@
#
# @return [Array<hash>]
#
# @api public
def response
- response_transformer.call(
- response_handler.call(request_handler.call(self), self),
- self
- )
+ response_handler.call(request_handler.call(self), self)
end
private
def response_handler
@@ -336,9 +310,13 @@
request_handler
end
def __new__(*args, &block)
self.class.new(*args, &block)
+ end
+
+ def join_path(*paths)
+ paths.reject(&:empty?).join(PATH_SEPARATOR)
end
end
end
end