lib/apitizer/base.rb in apitizer-0.0.1 vs lib/apitizer/base.rb in apitizer-0.0.2

- old
+ new

@@ -1,61 +1,50 @@ module Apitizer class Base + extend Forwardable + + def_delegator :map, :define + def initialize(**options, &block) @options = Helper.deep_merge(Apitizer.defaults, options) @block = block end def process(*arguments) request = build_request(*arguments) response = dispatcher.process(request) - content = translator.process(response) - Result.new(request: request, response: response, content: content) + Result.new(request: request, response: response) end Apitizer.actions.each do |action| define_method(action) do |*arguments| process(action, *arguments) end end private - [ :mapper, :dispatcher, :translator ].each do |component| - class_eval <<-METHOD, __FILE__, __LINE__ + 1 - def #{ component } - @#{ component } ||= build_#{ component } - end - METHOD + def map + @map ||= Routing::Map.new(&@block) end - def build_mapper - Routing::Mapper.new(&@block) + def dispatcher + @dispatcher ||= Connection::Dispatcher.new(format: @options[:format], + adaptor: @options[:adaptor], headers: @options[:headers]) end - def build_dispatcher - Connection::Dispatcher.new(adaptor: self.adaptor, - dictionary: self.dictionary, headers: self.headers) - end - - def build_translator - Processing::Translator.new(format: self.format) - end - def build_request(*arguments) - action, steps, parameters = prepare(*arguments) - path = mapper.trace(action, steps) - Connection::Request.new(action: action, path: path, + action, method, steps, parameters = prepare(*arguments) + Connection::Request.new(method: method, path: map.trace(action, steps), parameters: parameters) end def prepare(action, *path) + action = action.to_sym + method = @options[:dictionary][action] or raise Error, 'Unknown action' parameters = path.last.is_a?(Hash) ? path.pop : {} - [ action.to_sym, path.flatten.map(&:to_sym), parameters ] - end + steps = path.flatten.map(&:to_sym) - def method_missing(name, *arguments, &block) - return @options[name] if @options.key?(name) - super + [ action, method, steps, parameters ] end end end