Sha256: be101adbfa1cf16efd117d7035da83acb1070095fbb786b7d3f4acf7bc02601d

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

require 'active_record'
require 'rack'

require 'murlsh'

module Murlsh

  # Dispatch requests.
  class Dispatch

    # Set up database connection and dispatch table.
    def initialize(config)
      @config = config

      ActiveRecord::Base.establish_connection(
        :adapter => 'sqlite3', :database => @config.fetch('db_file'))
      ActiveRecord::Base.include_root_in_json = false

      db = ActiveRecord::Base.connection.instance_variable_get(:@connection)

      url_server = Murlsh::UrlServer.new(@config, db)
      config_server = Murlsh::ConfigServer.new(@config)

      root_path = URI(@config.fetch('root_url')).path

      @dispatch = [
        [%r{^HEAD #{root_path}(url)?$}, url_server.method(:head)],
        [%r{^GET #{root_path}(url)?$}, url_server.method(:get)],
        [%r{^POST #{root_path}(url)?$}, url_server.method(:post)],
        [%r{^HEAD #{root_path}config$}, config_server.method(:head)],
        [%r{^GET #{root_path}config$}, config_server.method(:get)],
      ]
    end

    # Figure out which method will handle request.
    def dispatch(req)
      method_match = @dispatch.find do |rule|
        rule[0].match("#{req.request_method} #{req.path}")
      end

      method_match ? method_match[1] : self.method(:not_found)
    end

    # Rack call.
    def call(env)
      req = Rack::Request.new(env)
      dispatch(req).call(req).finish
    end

    # Called if the request is not found.
    def not_found(req)
      Rack::Response.new "<p>#{req.url} not found</p>

<p><a href=\"#{@config['root_url']}\">root<a></p>
",
        404, { 'Content-Type' => 'text/html' }
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
murlsh-1.1.0 lib/murlsh/dispatch.rb