lib/rom/http/dataset.rb in rom-http-0.1.0 vs lib/rom/http/dataset.rb in rom-http-0.1.1

- old
+ new

@@ -1,24 +1,38 @@ module ROM module HTTP class Dataset include Enumerable include Equalizer.new(:config, :options) + include ROM::Options - attr_reader :config, :options + attr_reader :config + 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: {} + + 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 + def initialize(config, options = {}) @config = config - @options = { - request_method: :get, - path: '', - params: {} - }.merge(options) + super(options) end def uri - config[:uri] + config.fetch(:uri) { fail Error, ':uri configuration missing' } end def headers config.fetch(:headers, {}).merge(options.fetch(:headers, {})) end @@ -33,28 +47,20 @@ def absolute_path '/' + path end - def request_method - options[:request_method] - end - - def params - options[:params] - end - def with_headers(headers) - self.class.new(config, options.merge(headers: headers)) + __new__(config, options.merge(headers: headers)) end def add_header(header, value) with_headers(headers.merge(header => value)) end def with_options(opts) - self.class.new(config, options.merge(opts)) + __new__(config, options.merge(opts)) end def with_path(path) with_options(path: path) end @@ -101,14 +107,30 @@ end private def response_handler - config[:response_handler] + config.fetch(:response_handler, default_response_handler).tap do |response_handler| + fail Error, ':response_handler configuration missing' if response_handler.nil? + end end def request_handler - config[:request_handler] + config.fetch(:request_handler, default_request_handler).tap do |request_handler| + fail Error, ':response_handler configuration missing' if request_handler.nil? + end + end + + def default_response_handler + self.class.default_response_handler + end + + def default_request_handler + self.class.default_request_handler + end + + def __new__(*args, &block) + self.class.new(*args, &block) end end end end