Sha256: 5a3ff01bb42b102ab1d02c4e89020225b9817c8572d8bd2c1c7d557be05efe0c

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

require 'sanford/sanford_runner'

module Sanford

  class Route

    attr_reader :name, :handler_class_name, :handler_class

    def initialize(name, handler_class_name)
      @name = name.to_s
      @handler_class_name = handler_class_name
      @handler_class = nil
    end

    def validate!
      @handler_class = constantize_handler_class(@handler_class_name)
    end

    def run(request, server_data)
      SanfordRunner.new(self.handler_class, request, server_data).run
    end

    private

    def constantize_handler_class(handler_class_name)
      constantize(handler_class_name).tap do |handler_class|
        raise(NoHandlerClassError.new(handler_class_name)) if !handler_class
      end
    end

    def constantize(class_name)
      names = class_name.to_s.split('::').reject{ |name| name.empty? }
      klass = names.inject(Object){ |constant, name| constant.const_get(name) }
      klass == Object ? false : klass
    rescue NameError
      false
    end

  end

  class NoHandlerClassError < RuntimeError
    def initialize(handler_class_name)
      super "Sanford couldn't find the service handler '#{handler_class_name}'" \
            " - it doesn't exist or hasn't been required in yet."
    end
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sanford-0.11.1 lib/sanford/route.rb
sanford-0.11.0 lib/sanford/route.rb