Class: Rack::Embed

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/embed.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Embed

Returns a new instance of Embed.



6
7
8
9
10
11
12
# File 'lib/rack/embed.rb', line 6

def initialize(app, opts = {})
  @app = app
  @max_size = opts[:max_size] || 1024
  @mime_types = opts[:mime_types] || %w(text/css application/xhtml+xml text/html)
  @threaded = opts[:threaded] || false
  @timeout = opts[:timeout] || 0.5
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rack/embed.rb', line 14

def call(env)
  ua = env['HTTP_USER_AGENT']

  # Replace this with something more sophisticated
  # Supported according to http://en.wikipedia.org/wiki/Data_URI_scheme
  if !ua || ua !~ /WebKit|Gecko|Opera|Konqueror|MSIE 8.0/
    return @app.call(env)
  end

  original_env = env.clone
  response = @app.call(env)
  return response if !applies_to?(response)

  status, header, body = response
  body = css?(header) ? css_embed_images(body.first, original_env) : html_embed_images(body.first, original_env)
  header['Content-Length'] = Rack::Utils.bytesize(body).to_s

  [status, header, [body]]
rescue Exception => ex
  env['rack.errors'].write("#{ex.message}\n") if env['rack.errors']
  [500, {}, ex.message]
end