Permalink
Browse files

Initial Commit

  • Loading branch information...
1 parent a722a6a commit 47ed91603c16aa7f45c9c3deb47f8c116dd20b8d @meetme2meat committed Feb 2, 2012
Showing with 136 additions and 4 deletions.
  1. +25 −0 Gemfile.lock
  2. 0 README
  3. +6 −3 Rakefile
  4. +1 −1 VERSION
  5. +104 −0 lib/rack_iphone.rb
View
@@ -0,0 +1,25 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ git (1.2.5)
+ jeweler (1.8.3)
+ bundler (~> 1.0)
+ git (>= 1.2.5)
+ rake
+ rdoc
+ json (1.6.5)
+ rake (0.9.2.2)
+ rcov (1.0.0)
+ rdoc (3.12)
+ json (~> 1.4)
+ shoulda (2.11.3)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ bundler (~> 1.0.0)
+ jeweler (~> 1.8.3)
+ rcov
+ rdoc (~> 3.12)
+ shoulda
View
0 README
No changes.
View
@@ -17,10 +17,13 @@ Jeweler::Tasks.new do |gem|
gem.name = "rack_iphone"
gem.homepage = "http://github.com/meetme2meat/rack_iphone"
gem.license = "MIT"
- gem.summary = %Q{TODO: one-line summary of your gem}
- gem.description = %Q{TODO: longer description of your gem}
+ gem.summary = %Q{Rack Iphone is use for auto login from iphone home screen shortcut}
+ gem.description = %Q{Rack Iphone is use for auto login for web app from iphone when activated from home screen shortcut. The Gem is Inspired by work of Jim Hoskins gem rack_iphone_web_app}
gem.email = "meetme2meat@gmail.com"
- gem.authors = ["meetme2meat"]
+ gem.authors = ["Viren Negi"]
+ gem.files = Dir.glob('lib/**/*.rb')
+ gem.files.include "rack_iphone.rb"
+ gem.add_runtime_dependency 'rack'
# dependencies defined in Gemfile
end
Jeweler::RubygemsDotOrgTasks.new
View
@@ -1 +1 @@
-0.0.0
+1.0.0
View
@@ -0,0 +1,104 @@
+module Rack
+ class Iphone
+ 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.match(/_session_id/)){
+ 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();
+ }
+
+ }
+ }
+ })()
+ </script>
+ }
+ COOKIE = %{
+ <script type="text/javascript">
+ (function(){
+ var COOKIE = "{{COOKIE}}";
+ var lastCookie = null;
+ setInterval(function(){
+ if(lastCookie != ''+COOKIE){
+ lastCookie = ''+COOKIE;
+ localStorage.setItem('__cookie__', ''+COOKIE);
+ }
+ },1000);
+ })()
+ </script>
+ }
+
+ def initialize(app)
+ @app = app
+ end
+
+ 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
+ ##
+ ##
+
+ request = Rack::Request.new(env)
+ response = Rack::Response.new([], status, headers)
+ cookie = String.new
+ request.cookies.each_pair do |key,value|
+ cookie += "#{key}=#{value};"
+ end
+ body.each do |part|
+ part.gsub!(/<\/head>/, "#{set_cookie(cookie)}</head>")
+ response.write(part)
+ end
+ response.finish
+ end
+ else
+ @app.call(env)
+ end
+ end
+
+ protected
+
+ def code(resend=false)
+ CODE.gsub('{{RESEND}}', resend.to_s)
+ end
+
+ def set_cookie(cookie)
+ COOKIE.gsub('{{COOKIE}}',cookie.to_s)
+ end
+
+ def new_session?(env)
+ request = Rack::Request.new(env)
+ if request.cookies['_cookieset_'].nil?
+ true
+ else
+ false
+ end
+ end
+
+ def iphone_web_app?(env)
+ if env['HTTP_USER_AGENT']
+ env['HTTP_USER_AGENT'] =~ /WebKit.*Mobile/ && !(env['HTTP_USER_AGENT'] =~ /Safari/)
+ end
+ end
+ end
+end

0 comments on commit 47ed916

Please sign in to comment.