Sha256: 451db18cc63b346838a7ef26b2bb40f3acc22402283254d639a59121691f810d

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

# encoding: utf-8
module Shutterbug
  class Rackapp

    DefaultHandlers = [
      Shutterbug::Handlers::ConvertHandler,
      Shutterbug::Handlers::DirectUploadHandler,
      Shutterbug::Handlers::JsFileHandler,
      Shutterbug::Handlers::FileHandler
    ]

    def add_handler(klass)
      instance = klass.new
      log "adding handler for #{klass.regex} ➙ #{klass.name}"
      @handlers[klass.regex] = instance
    end

    def add_default_handlers
      DefaultHandlers.each { |h| add_handler(h) }
    end

    def initialize(app=nil, &block)
      @handlers = {}
      @config = Configuration.instance
      yield @config if block_given?
      @app = app
      add_default_handlers
      log "initialized"
    end

    def call(env)
      req    = Rack::Request.new(env)
      result = false
      @handlers.keys.each do |path_regex|
        if req.path =~ path_regex
          result = @handlers[path_regex].handle(self, req, env)
        end
      end
      result || skip(env)
    end

    def response(content, type, status=200, cachable=true)
      headers = {}
      size = content.respond_to?(:bytesize) ? content.bytesize : content.size
      headers['Content-Length'] = size.to_s
      headers['Content-Type']   = type
      headers['Cache-Control']  = 'no-cache' unless cachable
      content = [content] unless content.respond_to? 'each'
      return [status, headers, content]
    end

    def log(string)
      puts "★ shutterbug #{Shutterbug::VERSION} ➙ #{string}"
    end

    def skip(env)
      # call the applicaiton default
      @app.call(env) if @app
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shutterbug-0.4.3 lib/shutterbug/rackapp.rb