Class: Shutterbug::Rackapp

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Rackapp.

Yields:

  • (@config)


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

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_handlersObject



17
18
19
# File 'lib/shutterbug/rackapp.rb', line 17

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

#add_handler(klass) ⇒ Object



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

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

#call(env) ⇒ Object



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

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



51
52
53
# File 'lib/shutterbug/rackapp.rb', line 51

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

#response(content, type, status = 200, cachable = true) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/shutterbug/rackapp.rb', line 41

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



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

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