Sha256: ea83c6d75b1b7e98ef9ab86865bcb0fdb20238033737dbaa279ec16aa8b4850d

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

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)
      Result.new(request: request, response: response)
    end

    Apitizer.actions.each do |action|
      define_method(action) do |*arguments|
        process(action, *arguments)
      end
    end

    private

    def map
      @map ||= Routing::Map.new(&@block)
    end

    def dispatcher
      @dispatcher ||= Connection::Dispatcher.new(format: @options[:format],
        adaptor: @options[:adaptor], headers: @options[:headers])
    end

    def build_request(*arguments)
      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 = Helper.extract_hash!(path)
      steps = path.flatten.map(&:to_sym)

      [ action, method, steps, parameters ]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
apitizer-0.0.3 lib/apitizer/base.rb