Class: Rack::IphoneWebApp

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

Constant Summary collapse

CODE =
%{
  <script type="text/javascript">
  (function(){
    var RESEND_REQUEST = {{RESEND}};

    function isFullScreen(){
      return navigator.userAgent.match(/WebKit.*Mobile/) &&
             !navigator.userAgent.match(/Safari/);
    }

    if(isFullScreen()){
      if(document.cookie == ''){
        var storedValues = localStorage.getItem('__cookie__');
          if(storedValues){
            var values = storedValues.split(';');
            for(var i=0; i < values.length; i++)
              document.cookie = values[i];
          }
          document.cookie = '_cookieset_=1';
        
        if(RESEND_REQUEST){
          window.location.reload();
        }
        
      }  

      var lastCookie = null;
      setInterval(function(){
        if(lastCookie != ''+document.cookie){
          lastCookie = ''+document.cookie;
          localStorage.setItem('__cookie__', ''+document.cookie);
        }
      },1000);

    }
  })()
  </script>
}

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ IphoneWebApp

Returns a new instance of IphoneWebApp.



43
44
45
# File 'lib/rack_iphone_web_app.rb', line 43

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rack_iphone_web_app.rb', line 47

def call(env)
  if iphone_web_app?(env)
    if new_session?(env)
      [200,{'Content-Length' => code(true).length.to_s, 'Content-Type' => 'text/html'}, code(true)]
    else
      status, headers, body = @app.call(env)
      # Put in patch code
      response = Rack::Response.new([], status, headers)
      body.each do |part|
        part.gsub!(/<\/head>/, "#{code}</head>")
        response.write(part)
      end
      response.finish
    end
  else
    @app.call(env)
  end
end