Class: Shutterbug::Rackapp
- Inherits:
-
Object
- Object
- Shutterbug::Rackapp
- Defined in:
- lib/shutterbug/rackapp.rb
Constant Summary collapse
- DefaultHandlers =
[ Shutterbug::Handlers::ConvertHandler, Shutterbug::Handlers::DirectUploadHandler, Shutterbug::Handlers::FileHandler, Shutterbug::Handlers::ErrorTrigger ]
Instance Method Summary collapse
- #add_default_handlers ⇒ Object
- #add_handler(klass) ⇒ Object
- #call(env) ⇒ Object
-
#initialize(app = nil) {|@config| ... } ⇒ Rackapp
constructor
A new instance of Rackapp.
- #log(string) ⇒ Object
- #response(content, type, status = 200, cachable = true) ⇒ Object
- #skip(env) ⇒ Object
Constructor Details
#initialize(app = nil) {|@config| ... } ⇒ Rackapp
Returns a new instance of Rackapp.
22 23 24 25 26 27 28 29 |
# File 'lib/shutterbug/rackapp.rb', line 22 def initialize(app=nil, &block) @handlers = {} @config = Configuration.instance yield @config if block_given? @app = app add_default_handlers log "initialized" end |
Instance Method Details
#add_default_handlers ⇒ Object
18 19 20 |
# File 'lib/shutterbug/rackapp.rb', line 18 def add_default_handlers DefaultHandlers.each { |h| add_handler(h) } end |
#add_handler(klass) ⇒ Object
12 13 14 15 16 |
# File 'lib/shutterbug/rackapp.rb', line 12 def add_handler(klass) instance = klass.new log "adding handler for #{klass.regex} ➙ #{klass.name}" @handlers[klass.regex] = instance end |
#call(env) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/shutterbug/rackapp.rb', line 31 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 |
#log(string) ⇒ Object
52 53 54 |
# File 'lib/shutterbug/rackapp.rb', line 52 def log(string) puts "★ shutterbug #{Shutterbug::VERSION} ➙ #{string}" end |
#response(content, type, status = 200, cachable = true) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/shutterbug/rackapp.rb', line 42 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 |
#skip(env) ⇒ Object
56 57 58 59 |
# File 'lib/shutterbug/rackapp.rb', line 56 def skip(env) # call the applicaiton default @app.call(env) if @app end |