lib/shutterbug/rackapp.rb in shutterbug-0.0.4 vs lib/shutterbug/rackapp.rb in shutterbug-0.0.6

- old
+ new

@@ -1,54 +1,38 @@ # encoding: utf-8 module Shutterbug class Rackapp - BASE_PATH = "/shutterbug" - CONVERT_PATH = "#{BASE_PATH}/make_snapshot" - CONVERT_REGEX = /#{CONVERT_PATH}/ - - PNG_PATH = "#{BASE_PATH}/get_png" - GET_PNG_REGEX = /#{PNG_PATH}\/([^\/]+)/ - - HTML_PATH = "#{BASE_PATH}/get_html" - GET_HTML_REGEX = /#{HTML_PATH}\/([^\/]+)/ - - JS_PATH = "#{BASE_PATH}/shutterbug.js" - JS_REGEX = /#{JS_PATH}$/ - - def initialize app + def initialize(app, &block) + @config = Configuration.instance + yield @config if block_given? @app = app - @shutterbug = Service.new() + @shutterbug = Service.new(@config) log "initialized" end - def base_url(req) - req.POST()['base_url'] || req.referrer || "#{req.scheme}://#{req.host_with_port}" - end - def do_convert(req) html = req.POST()['content'] || "" width = req.POST()['width'] || 1000 height = req.POST()['height'] || 700 css = req.POST()['css'] || "" - signature = @shutterbug.convert(base_url(req), html, css, width, height) - response_url = "#{PNG_PATH}/#{signature}" - response_text = "<img src='#{response_url}' alt='#{signature}'>" + signature = @shutterbug.convert(@config.base_url(req), html, css, width, height) + response_text = "<img src='#{@config.png_path(signature)}' alt='#{signature}'>" return good_response(response_text,'text/plain') end def call env req = Rack::Request.new(env) case req.path - when CONVERT_REGEX + when @config.convert_regex do_convert(req) - when GET_PNG_REGEX + when @config.png_regex good_response(@shutterbug.get_png_file($1),'image/png') - when GET_HTML_REGEX + when @config.html_regex good_response(@shutterbug.get_html_file($1),'text/html') - when JS_PATH + when @config.js_regex good_response(@shutterbug.get_shutterbug_file, 'application/javascript') else skip(env) end end