Class: Shutterbug::Rackapp

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

Constant Summary collapse

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}$/

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Rackapp



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

def initialize app
  @app = app
  @shutterbug = Service.new()
  log "initialized"
end

Instance Method Details

#base_url(req) ⇒ Object



25
26
27
# File 'lib/shutterbug/rackapp.rb', line 25

def base_url(req)
  req.POST()['base_url'] ||  req.referrer || "#{req.scheme}://#{req.host_with_port}"
end

#call(env) ⇒ Object



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

def call env
  req = Rack::Request.new(env)
  case req.path
  when CONVERT_REGEX
    do_convert(req)
  when GET_PNG_REGEX
    good_response(@shutterbug.get_png_file($1),'image/png')
  when GET_HTML_REGEX
    good_response(@shutterbug.get_html_file($1),'text/html')
  when JS_PATH
    good_response(@shutterbug.get_shutterbug_file, 'application/javascript')
  else
    skip(env)
  end
end

#do_convert(req) ⇒ Object



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

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}'>"
  return good_response(response_text,'text/plain')
end