Class: Shutterbug::Rackapp

Inherits:
Object
  • Object
show all
Defined in:
lib/shutterbug/rackapp.rb

Constant Summary collapse

DefaultHandlers =
[
  Shutterbug::Handlers::ConvertHandler ,
  Shutterbug::Handlers::JsFileHandler,
  Shutterbug::Handlers::FileHandlers::PngFile,
  Shutterbug::Handlers::FileHandlers::HtmlFile
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app = nil) {|@config| ... } ⇒ Rackapp

Returns a new instance of Rackapp.

Yields:

  • (@config)


23
24
25
26
27
28
29
30
# File 'lib/shutterbug/rackapp.rb', line 23

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

Instance Attribute Details

#handlersObject

Returns the value of attribute handlers.



12
13
14
# File 'lib/shutterbug/rackapp.rb', line 12

def handlers
  @handlers
end

Instance Method Details

#add_default_handlersObject



19
20
21
# File 'lib/shutterbug/rackapp.rb', line 19

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

#add_handler(klass) ⇒ Object



13
14
15
16
17
# File 'lib/shutterbug/rackapp.rb', line 13

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

#call(env) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/shutterbug/rackapp.rb', line 32

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

#good_response(content, type, cachable = true) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/shutterbug/rackapp.rb', line 43

def good_response(content, type, 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 must be enumerable.
  content = [content] if content.kind_of? String
  return [200, headers, content]
end

#log(string) ⇒ Object



54
55
56
# File 'lib/shutterbug/rackapp.rb', line 54

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

#skip(env) ⇒ Object



58
59
60
61
# File 'lib/shutterbug/rackapp.rb', line 58

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