Sha256: d721a9924458e6c7f76a6b8da94cef3a5f42fb346822ed16a373157320d893aa

Contents?: true

Size: 1 KB

Versions: 11

Compression:

Stored size: 1 KB

Contents

module ScrewUnit
  class Dispatcher
    attr_reader :root, :resource_locators

    def initialize(configuration)
      @resource_locators = configuration.resource_locators
      @root = Resources::Root.new(configuration)
    end

    def call(env)
      request = Rack::Request.new(env)
      resource = locate_resource(request.path_info)
      case(request.request_method)
      when "GET":
        resource.get
      when "POST":
        resource.post(request)
      else
        raise "unrecognized HTTP method #{request.request_method}"
      end
    rescue Exception => e
      puts e.message
      puts e.backtrace
    end

    def locate_resource(path)
      resource_locators.each do |locator|
        resource = locator.locate_resource(path)
        return resource if resource
      end

      path_parts(path).inject(root) do |resource, child_resource_name|
        resource.locate(child_resource_name)
      end
    end

    def path_parts(path)
      path.split('/').reject { |part| part == "" }
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
coolerator.vision-0.2.10 spec/support/vendor/screw-unit/lib/screw_unit/dispatcher.rb
coolerator.vision-0.2.9 spec/support/vendor/screw-unit/lib/screw_unit/dispatcher.rb
coolerator.vision-0.2.8 spec/support/vendor/screw-unit/lib/screw_unit/dispatcher.rb
coolerator.vision-0.2.7 spec/support/vendor/screw-unit/lib/screw_unit/dispatcher.rb
coolerator.vision-0.2.6 spec/support/vendor/screw-unit/lib/screw_unit/dispatcher.rb
coolerator.vision-0.2.5 spec/support/vendor/screw-unit/lib/screw_unit/dispatcher.rb
coolerator.vision-0.2.4 spec/support/vendor/screw-unit/lib/screw_unit/dispatcher.rb
coolerator.vision-0.2.3 spec/support/vendor/screw-unit/lib/screw_unit/dispatcher.rb
coolerator.vision-0.2.2 spec/support/vendor/screw-unit/lib/screw_unit/dispatcher.rb
coolerator.vision-0.2.1 spec/support/vendor/screw-unit/lib/screw_unit/dispatcher.rb
coolerator.vision-0.2.0 spec/support/vendor/screw-unit/lib/screw_unit/dispatcher.rb