Class: Rack::ChromeFrame

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

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ ChromeFrame


8
9
10
11
# File 'lib/rack/chrome_frame.rb', line 8

def initialize(app, options={})
  @app = app
  @options = options
end

Instance Method Details

#build_response_body(response) ⇒ Object


42
43
44
45
46
47
48
49
50
# File 'lib/rack/chrome_frame.rb', line 42

def build_response_body(response)
  response_body = ""
  response.each { |part| response_body += part }

  # see: http://johnbintz.github.com/blog/2012/03/05/closing-given-body-in-rack-middleware/
  response.close if response.respond_to?(:close)

  response_body
end

#call(env) ⇒ Object


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

def call(env)

  if env['HTTP_USER_AGENT'] =~ /MSIE/
    if env['HTTP_USER_AGENT'] =~ /chromeframe/
      status, headers, response = @app.call(env)
      new_body = insert_tag(build_response_body(response))
      new_headers = recalculate_body_length(headers, new_body)
      return [status, new_headers, [new_body]]
    elsif @options[:minimum].nil? or ie_version(env['HTTP_USER_AGENT']) < @options[:minimum]
      html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n<head>\n<meta http-equiv=\"content-type\" content=\"text/html;charset=UTF-8\" />\n<title>Diaspora doesn't support your version of Internet Explorer.  Try Chrome, Firefox, or Opera!</title>\n</head>\n<body>\n<div id=\"cf-placeholder\"></div>\n<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js\"></script>\n<script>CFInstall.check({ node: \"cf-placeholder\" \#{', destination: \"' + @options[:destination] + '\" ' if @options[:destination]}});</script>\n</body>\n</html>\n"
      return [200, {'Content-Type' =>  'text/html', 'Content-Length' => html.size.to_s}, Rack::Response.new([html])]
    end
  end
  @app.call(env)
end

#ie_version(ua_string) ⇒ Object


67
68
69
# File 'lib/rack/chrome_frame.rb', line 67

def ie_version(ua_string)
  ua_string.match(/MSIE (\S+)/)[1].to_f
end

#insert_tag(body) ⇒ Object


58
59
60
61
62
63
64
65
# File 'lib/rack/chrome_frame.rb', line 58

def insert_tag(body)
  head = "<meta http-equiv=\"X-UA-Compatible\" content=\"chrome=1\">\n"

  body.gsub!('<head>', "<head>\n" + head )
  body
end

#recalculate_body_length(headers, body) ⇒ Object


52
53
54
55
56
# File 'lib/rack/chrome_frame.rb', line 52

def recalculate_body_length(headers, body)
  new_headers = headers
  new_headers["Content-Length"] = body.length.to_s
  new_headers
end