schmobile 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +23 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +51 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/lib/rack/schmobile.rb +83 -0
- data/schmobile.gemspec +67 -0
- data/test/helper.rb +12 -0
- data/test/test_schmobile.rb +193 -0
- metadata +156 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rack", "1.1.0"
|
10
|
+
gem "mocha", "0.9.9"
|
11
|
+
gem "shoulda", ">= 0"
|
12
|
+
gem "bundler", "~> 1.0.0"
|
13
|
+
gem "jeweler", "~> 1.5.2"
|
14
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
git (1.2.5)
|
5
|
+
jeweler (1.5.2)
|
6
|
+
bundler (~> 1.0.0)
|
7
|
+
git (>= 1.2.5)
|
8
|
+
rake
|
9
|
+
mocha (0.9.9)
|
10
|
+
rake
|
11
|
+
rack (1.1.0)
|
12
|
+
rake (0.8.7)
|
13
|
+
shoulda (2.11.3)
|
14
|
+
|
15
|
+
PLATFORMS
|
16
|
+
ruby
|
17
|
+
|
18
|
+
DEPENDENCIES
|
19
|
+
bundler (~> 1.0.0)
|
20
|
+
jeweler (~> 1.5.2)
|
21
|
+
mocha (= 0.9.9)
|
22
|
+
rack (= 1.1.0)
|
23
|
+
shoulda
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Morten Primdahl
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
= schmobile
|
2
|
+
|
3
|
+
A mobile user agent detection Rack middleware.
|
4
|
+
|
5
|
+
== Redirecting
|
6
|
+
|
7
|
+
It can be configured to return the user to an explicit destination:
|
8
|
+
|
9
|
+
use Rack::Schmobile, :redirect_to => "/mobile"
|
10
|
+
|
11
|
+
It supports string interpolation for dynamic destinations:
|
12
|
+
|
13
|
+
use Rack::Schmobile, :redirect_to => "/mobile/#!/{{path}}"
|
14
|
+
|
15
|
+
== Forcing mobile mode
|
16
|
+
|
17
|
+
You can force toggle mobile mode by sending +schmobile_mode=enabled+ and +schmobile_mode=disabled+ - note that these
|
18
|
+
require session access.
|
19
|
+
|
20
|
+
Finally the middleware provides Rack::Request#is_mobile?
|
21
|
+
|
22
|
+
== User agent detection
|
23
|
+
|
24
|
+
You can add/remove user agents like so:
|
25
|
+
|
26
|
+
Rack::Schmobile.add_user_agent_pattern("wibble")
|
27
|
+
Rack::Schmobile.remote_user_agent_pattern("ipad")
|
28
|
+
|
29
|
+
== Thanks
|
30
|
+
|
31
|
+
Not quite what we were looking for, but good inspiration and may work for you:
|
32
|
+
|
33
|
+
https://github.com/talison/rack-mobile-detect
|
34
|
+
https://github.com/brendanlim/mobile-fu
|
35
|
+
|
36
|
+
|
37
|
+
== Contributing to schmobile
|
38
|
+
|
39
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
40
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
41
|
+
* Fork the project
|
42
|
+
* Start a feature/bugfix branch
|
43
|
+
* Commit and push until you are happy with your contribution
|
44
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
45
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
46
|
+
|
47
|
+
== Copyright
|
48
|
+
|
49
|
+
Copyright (c) 2011 Morten Primdahl. See LICENSE.txt for
|
50
|
+
further details.
|
51
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "schmobile"
|
16
|
+
gem.homepage = "http://github.com/morten/schmobile"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{A Rack middleware for detecting mobile user agents}
|
19
|
+
gem.description = %Q{Used to determine if a request is from a mobile client, and possibly redirect it if that's the case}
|
20
|
+
gem.email = "morten@zendesk.com"
|
21
|
+
gem.authors = ["Morten Primdahl"]
|
22
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
23
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
24
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
25
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rake/testtask'
|
30
|
+
Rake::TestTask.new(:test) do |test|
|
31
|
+
test.libs << 'lib' << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
|
36
|
+
task :default => :test
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "schmobile #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.3
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'rack'
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
|
5
|
+
# Rack::Schmobile is a minimalist mobile UA detection middleware
|
6
|
+
class Schmobile
|
7
|
+
MOBILE_USER_AGENTS = %w(
|
8
|
+
palm blackberry nokia phone midp mobi symbian chtml ericsson minimo audiovox motorola samsung telit upg1 windows ce
|
9
|
+
ucweb astel plucker x320 x240 j2me sgh portable sprint docomo kddi softbank android mmp pdxgw netfront xiino vodafone
|
10
|
+
portalmmm sagem mot- sie- ipod up.b webos amoi novarra cdm alcatel pocket ipad iphone mobileexplorer mobile
|
11
|
+
)
|
12
|
+
|
13
|
+
SCHMOBILE_MODE = 'schmobile_mode'
|
14
|
+
|
15
|
+
def self.remove_user_agent_pattern(pattern)
|
16
|
+
MOBILE_USER_AGENTS.delete(pattern)
|
17
|
+
@mobile_agent_matcher = nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.add_user_agent_pattern(pattern)
|
21
|
+
MOBILE_USER_AGENTS.push(*pattern)
|
22
|
+
@mobile_agent_matcher = nil
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.is_mobile_request?(request)
|
26
|
+
request.user_agent.to_s.downcase =~ mobile_agent_matcher
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.mobile_agent_matcher
|
30
|
+
@mobile_agent_matcher ||= Regexp.new(MOBILE_USER_AGENTS.uniq.compact.map { |v| Regexp.escape(v) }.join("|"))
|
31
|
+
end
|
32
|
+
|
33
|
+
def initialize(app, options = {})
|
34
|
+
@app = app
|
35
|
+
@options = options
|
36
|
+
end
|
37
|
+
|
38
|
+
def call(env)
|
39
|
+
if is_mobile_session?(env) && redirect?(env)
|
40
|
+
return [ 301, { "Location" => redirect(env) }, [] ]
|
41
|
+
end
|
42
|
+
|
43
|
+
@app.call(env)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Checks if this session has been forced into mobile mode and returns that if that is the case. Otherwise
|
47
|
+
# checks the user agent via request.is_mobile?
|
48
|
+
def is_mobile_session?(env)
|
49
|
+
session = env['rack.session'] ||= {}
|
50
|
+
|
51
|
+
request = Rack::Request.new(env)
|
52
|
+
if request.params[SCHMOBILE_MODE]
|
53
|
+
session[SCHMOBILE_MODE] = request.params[SCHMOBILE_MODE]
|
54
|
+
end
|
55
|
+
|
56
|
+
unless session[SCHMOBILE_MODE].nil?
|
57
|
+
return session[SCHMOBILE_MODE] == 'enabled'
|
58
|
+
end
|
59
|
+
|
60
|
+
request.is_mobile?
|
61
|
+
end
|
62
|
+
|
63
|
+
# Returns true if this middleware has been configured with a redirect_to and the requested path is not already
|
64
|
+
# below the configured redirect_to
|
65
|
+
def redirect?(env)
|
66
|
+
!redirect(env).empty? && Rack::Utils.unescape(env["PATH_INFO"]) !~ /^#{redirect(env)}/
|
67
|
+
end
|
68
|
+
|
69
|
+
def redirect(env)
|
70
|
+
destination = @options[:redirect_to].to_s
|
71
|
+
request = Rack::Request.new(env)
|
72
|
+
# Substitutions here
|
73
|
+
destination
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
Rack::Request.class_eval do
|
80
|
+
def is_mobile?
|
81
|
+
Rack::Schmobile.is_mobile_request?(self)
|
82
|
+
end
|
83
|
+
end
|
data/schmobile.gemspec
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{schmobile}
|
8
|
+
s.version = "0.0.3"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Morten Primdahl"]
|
12
|
+
s.date = %q{2011-02-17}
|
13
|
+
s.description = %q{Used to determine if a request is from a mobile client, and possibly redirect it if that's the case}
|
14
|
+
s.email = %q{morten@zendesk.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lib/rack/schmobile.rb",
|
28
|
+
"schmobile.gemspec",
|
29
|
+
"test/helper.rb",
|
30
|
+
"test/test_schmobile.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/morten/schmobile}
|
33
|
+
s.licenses = ["MIT"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.7}
|
36
|
+
s.summary = %q{A Rack middleware for detecting mobile user agents}
|
37
|
+
s.test_files = [
|
38
|
+
"test/helper.rb",
|
39
|
+
"test/test_schmobile.rb"
|
40
|
+
]
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_development_dependency(%q<rack>, ["= 1.1.0"])
|
48
|
+
s.add_development_dependency(%q<mocha>, ["= 0.9.9"])
|
49
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
50
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
51
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
52
|
+
else
|
53
|
+
s.add_dependency(%q<rack>, ["= 1.1.0"])
|
54
|
+
s.add_dependency(%q<mocha>, ["= 0.9.9"])
|
55
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
56
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
57
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
58
|
+
end
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<rack>, ["= 1.1.0"])
|
61
|
+
s.add_dependency(%q<mocha>, ["= 0.9.9"])
|
62
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
63
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
64
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
data/test/helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
require 'mocha'
|
5
|
+
|
6
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
7
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
8
|
+
|
9
|
+
require 'rack/schmobile'
|
10
|
+
|
11
|
+
class Test::Unit::TestCase
|
12
|
+
end
|
@@ -0,0 +1,193 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestSchmobile < Test::Unit::TestCase
|
4
|
+
context "Rack::Request" do
|
5
|
+
context "#is_mobile?" do
|
6
|
+
should "not detect a regular browser as mobile" do
|
7
|
+
assert !Rack::Request.new(environment).is_mobile?
|
8
|
+
assert !Rack::Request.new("HTTP_USER_AGENT" => nil).is_mobile?
|
9
|
+
end
|
10
|
+
|
11
|
+
should "detect mobile units as mobile" do
|
12
|
+
[ :ipod, :iphone, :android, :blackberry, :samsung ].each do |phone|
|
13
|
+
agent = self.send(phone)
|
14
|
+
assert Rack::Request.new(environment("HTTP_USER_AGENT" => agent)).is_mobile?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "Rack::Schmobile class" do
|
21
|
+
context "#remove_user_agent_pattern" do
|
22
|
+
should "allow removal of a user agent" do
|
23
|
+
request = stub(:user_agent => "novarra")
|
24
|
+
assert Rack::Schmobile.is_mobile_request?(request)
|
25
|
+
Rack::Schmobile.remove_user_agent_pattern("novarra")
|
26
|
+
assert !Rack::Schmobile.is_mobile_request?(request)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "#add_user_agent_pattern" do
|
31
|
+
should "allow adding a user agent" do
|
32
|
+
request = stub(:user_agent => "wibble")
|
33
|
+
assert !Rack::Schmobile.is_mobile_request?(request)
|
34
|
+
Rack::Schmobile.add_user_agent_pattern("wibble")
|
35
|
+
assert Rack::Schmobile.is_mobile_request?(request)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "Rack::Schmobile" do
|
41
|
+
setup do
|
42
|
+
@app = Class.new { def call(app); true; end }.new
|
43
|
+
@rack = Rack::Schmobile.new(@app)
|
44
|
+
end
|
45
|
+
|
46
|
+
should "return an HTTP permanent redirect when given a redirect path and used by a mobile client" do
|
47
|
+
@rack.stubs(:redirect).returns("/hello")
|
48
|
+
@rack.expects(:is_mobile_session?).returns(true)
|
49
|
+
|
50
|
+
assert_equal [301, {"Location"=>"/hello"}, []], @rack.call(environment)
|
51
|
+
end
|
52
|
+
|
53
|
+
context "#is_mobile_session?" do
|
54
|
+
should "return false for regular browsers" do
|
55
|
+
Rack::Request.any_instance.expects(:params).returns({})
|
56
|
+
assert !@rack.is_mobile_session?(environment)
|
57
|
+
end
|
58
|
+
|
59
|
+
should "return true for a mobile browser" do
|
60
|
+
Rack::Request.any_instance.expects(:params).returns({})
|
61
|
+
assert @rack.is_mobile_session?(environment("HTTP_USER_AGENT" => iphone))
|
62
|
+
end
|
63
|
+
|
64
|
+
should "return false when forced in the session" do
|
65
|
+
Rack::Request.any_instance.expects(:params).returns({})
|
66
|
+
Rack::Request.any_instance.expects(:is_mobile?).never
|
67
|
+
|
68
|
+
assert !@rack.is_mobile_session?(environment("HTTP_USER_AGENT" => iphone, "rack.session" => { Rack::Schmobile::SCHMOBILE_MODE => "disabled" }))
|
69
|
+
end
|
70
|
+
|
71
|
+
should "return true when forced in the session" do
|
72
|
+
Rack::Request.any_instance.expects(:params).returns({})
|
73
|
+
Rack::Request.any_instance.expects(:is_mobile?).never
|
74
|
+
|
75
|
+
assert @rack.is_mobile_session?(environment("rack.session" => { Rack::Schmobile::SCHMOBILE_MODE => "enabled" }))
|
76
|
+
end
|
77
|
+
|
78
|
+
should "return false when forced via a request parameter" do
|
79
|
+
Rack::Request.any_instance.stubs(:params).returns({ Rack::Schmobile::SCHMOBILE_MODE => "disabled" })
|
80
|
+
Rack::Request.any_instance.expects(:is_mobile?).never
|
81
|
+
|
82
|
+
assert !@rack.is_mobile_session?(environment("HTTP_USER_AGENT" => iphone))
|
83
|
+
end
|
84
|
+
|
85
|
+
should "return true when forced via a request parameter" do
|
86
|
+
Rack::Request.any_instance.stubs(:params).returns({ Rack::Schmobile::SCHMOBILE_MODE => "enabled" })
|
87
|
+
Rack::Request.any_instance.expects(:is_mobile?).never
|
88
|
+
|
89
|
+
assert @rack.is_mobile_session?(environment)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context "with no redirect_to" do
|
94
|
+
should "not redirect mobile traffic" do
|
95
|
+
Rack::Request.any_instance.expects(:params).returns({})
|
96
|
+
|
97
|
+
@app.expects(:call)
|
98
|
+
@rack.call(environment("HTTP_USER_AGENT" => iphone))
|
99
|
+
end
|
100
|
+
|
101
|
+
should "query the request object to determine if this is a mobile request" do
|
102
|
+
Rack::Request.any_instance.expects(:params).returns({})
|
103
|
+
Rack::Request.any_instance.expects(:is_mobile?).returns(true)
|
104
|
+
|
105
|
+
@rack.call(environment("HTTP_USER_AGENT" => iphone))
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context "with redirect_to" do
|
110
|
+
setup do
|
111
|
+
@rack = Rack::Schmobile.new(@app, :redirect_to => "/wonderland")
|
112
|
+
end
|
113
|
+
|
114
|
+
context "#redirect?" do
|
115
|
+
should "return true when not on mobile path" do
|
116
|
+
assert @rack.redirect?(environment("PATH_INFO" => "/somewhere"))
|
117
|
+
end
|
118
|
+
|
119
|
+
should "return true on base path" do
|
120
|
+
assert @rack.redirect?(environment("PATH_INFO" => "/"))
|
121
|
+
end
|
122
|
+
|
123
|
+
should "return false when already on path" do
|
124
|
+
assert !@rack.redirect?(environment("PATH_INFO" => "/wonderland"))
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
context "#redirect" do
|
129
|
+
should_eventually "interpolate the argument string" do
|
130
|
+
@rack = Rack::Schmobile.new(@app, :redirect_to => "/wonderland/{{path}}")
|
131
|
+
assert_equal "/wonderland/wiffle", @rack.redirect(environment("PATH_INFO" => "wiffle"))
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
137
|
+
|
138
|
+
# User agents for testing
|
139
|
+
def ipod
|
140
|
+
'Mozilla/5.0 (iPod; U; CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20'
|
141
|
+
end
|
142
|
+
|
143
|
+
def iphone
|
144
|
+
'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_1 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7C144 Safari/528.16'
|
145
|
+
end
|
146
|
+
|
147
|
+
def android
|
148
|
+
'Mozilla/5.0 (Linux; U; Android 2.0; ld-us; sdk Build/ECLAIR) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17'
|
149
|
+
end
|
150
|
+
|
151
|
+
def blackberry
|
152
|
+
'BlackBerry9000/4.6.0.167 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/102'
|
153
|
+
end
|
154
|
+
|
155
|
+
def samsung
|
156
|
+
'Mozilla/4.0 (compatible; MSIE 6.0; BREW 3.1.5; en )/800x480 Samsung SCH-U960'
|
157
|
+
end
|
158
|
+
|
159
|
+
def environment(overwrite = {})
|
160
|
+
{
|
161
|
+
'GATEWAY_INTERFACE'=> 'CGI/1.2',
|
162
|
+
'HTTP_ACCEPT'=> 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
163
|
+
'HTTP_ACCEPT_CHARSET'=> 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
|
164
|
+
'HTTP_ACCEPT_ENCODING'=> 'gzip,deflate',
|
165
|
+
'HTTP_ACCEPT_LANGUAGE'=> 'en-us,en;q=0.5',
|
166
|
+
'HTTP_CONNECTION'=> 'keep-alive',
|
167
|
+
'HTTP_HOST'=> 'localhost:4567',
|
168
|
+
'HTTP_KEEP_ALIVE'=> 300,
|
169
|
+
'HTTP_USER_AGENT'=> 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.3) Gecko/20090920 Firefox/3.5.3 (Swiftfox)',
|
170
|
+
'HTTP_VERSION'=> 'HTTP/1.1',
|
171
|
+
'PATH_INFO'=> '/',
|
172
|
+
'QUERY_STRING'=> '',
|
173
|
+
'REMOTE_ADDR'=> '127.0.0.1',
|
174
|
+
'REQUEST_METHOD'=> 'GET',
|
175
|
+
'REQUEST_PATH'=> '/',
|
176
|
+
'REQUEST_URI'=> '/',
|
177
|
+
'SCRIPT_NAME'=> '',
|
178
|
+
'SERVER_NAME'=> 'localhost',
|
179
|
+
'SERVER_PORT'=> '4567',
|
180
|
+
'SERVER_PROTOCOL'=> 'HTTP/1.1',
|
181
|
+
'SERVER_SOFTWARE'=> 'Mongrel 1.1.5',
|
182
|
+
'rack.multiprocess'=> false,
|
183
|
+
'rack.multithread'=> true,
|
184
|
+
'rack.request.form_hash'=> '',
|
185
|
+
'rack.request.form_vars'=> '',
|
186
|
+
'rack.request.query_hash'=> '',
|
187
|
+
'rack.request.query_string'=> '',
|
188
|
+
'rack.run_once'=> false,
|
189
|
+
'rack.url_scheme'=> 'http',
|
190
|
+
'rack.version'=> '1: 0'
|
191
|
+
}.merge(overwrite)
|
192
|
+
end
|
193
|
+
end
|
metadata
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: schmobile
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Morten Primdahl
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-02-17 00:00:00 -08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
prerelease: false
|
23
|
+
type: :development
|
24
|
+
name: rack
|
25
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - "="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 19
|
31
|
+
segments:
|
32
|
+
- 1
|
33
|
+
- 1
|
34
|
+
- 0
|
35
|
+
version: 1.1.0
|
36
|
+
requirement: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
prerelease: false
|
39
|
+
type: :development
|
40
|
+
name: mocha
|
41
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - "="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 41
|
47
|
+
segments:
|
48
|
+
- 0
|
49
|
+
- 9
|
50
|
+
- 9
|
51
|
+
version: 0.9.9
|
52
|
+
requirement: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
prerelease: false
|
55
|
+
type: :development
|
56
|
+
name: shoulda
|
57
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
requirement: *id003
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
prerelease: false
|
69
|
+
type: :development
|
70
|
+
name: bundler
|
71
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ~>
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 23
|
77
|
+
segments:
|
78
|
+
- 1
|
79
|
+
- 0
|
80
|
+
- 0
|
81
|
+
version: 1.0.0
|
82
|
+
requirement: *id004
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
prerelease: false
|
85
|
+
type: :development
|
86
|
+
name: jeweler
|
87
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ~>
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
hash: 7
|
93
|
+
segments:
|
94
|
+
- 1
|
95
|
+
- 5
|
96
|
+
- 2
|
97
|
+
version: 1.5.2
|
98
|
+
requirement: *id005
|
99
|
+
description: Used to determine if a request is from a mobile client, and possibly redirect it if that's the case
|
100
|
+
email: morten@zendesk.com
|
101
|
+
executables: []
|
102
|
+
|
103
|
+
extensions: []
|
104
|
+
|
105
|
+
extra_rdoc_files:
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.rdoc
|
108
|
+
files:
|
109
|
+
- .document
|
110
|
+
- Gemfile
|
111
|
+
- Gemfile.lock
|
112
|
+
- LICENSE.txt
|
113
|
+
- README.rdoc
|
114
|
+
- Rakefile
|
115
|
+
- VERSION
|
116
|
+
- lib/rack/schmobile.rb
|
117
|
+
- schmobile.gemspec
|
118
|
+
- test/helper.rb
|
119
|
+
- test/test_schmobile.rb
|
120
|
+
has_rdoc: true
|
121
|
+
homepage: http://github.com/morten/schmobile
|
122
|
+
licenses:
|
123
|
+
- MIT
|
124
|
+
post_install_message:
|
125
|
+
rdoc_options: []
|
126
|
+
|
127
|
+
require_paths:
|
128
|
+
- lib
|
129
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
130
|
+
none: false
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
hash: 3
|
135
|
+
segments:
|
136
|
+
- 0
|
137
|
+
version: "0"
|
138
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
139
|
+
none: false
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
hash: 3
|
144
|
+
segments:
|
145
|
+
- 0
|
146
|
+
version: "0"
|
147
|
+
requirements: []
|
148
|
+
|
149
|
+
rubyforge_project:
|
150
|
+
rubygems_version: 1.3.7
|
151
|
+
signing_key:
|
152
|
+
specification_version: 3
|
153
|
+
summary: A Rack middleware for detecting mobile user agents
|
154
|
+
test_files:
|
155
|
+
- test/helper.rb
|
156
|
+
- test/test_schmobile.rb
|