schmobile 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +4 -0
- data/VERSION +1 -1
- data/lib/rack/schmobile.rb +14 -10
- data/schmobile.gemspec +1 -1
- data/test/test_schmobile.rb +23 -12
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -19,6 +19,10 @@ require session access.
|
|
19
19
|
|
20
20
|
Finally the middleware provides Rack::Request#is_mobile?
|
21
21
|
|
22
|
+
== Rolling out
|
23
|
+
|
24
|
+
use Rack::Schmobile, :redirect_to => "/mobile", :if => Proc.new { |request| request.host =~ /staging/ }
|
25
|
+
|
22
26
|
== User agent detection
|
23
27
|
|
24
28
|
You can add/remove user agents like so:
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.5
|
data/lib/rack/schmobile.rb
CHANGED
@@ -36,8 +36,10 @@ module Rack
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def call(env)
|
39
|
-
|
40
|
-
|
39
|
+
request = Rack::Request.new(env)
|
40
|
+
|
41
|
+
if is_mobile_session?(env, request) && redirect?(request)
|
42
|
+
return [ 301, { "Location" => redirect(request) }, [] ]
|
41
43
|
end
|
42
44
|
|
43
45
|
@app.call(env)
|
@@ -45,10 +47,9 @@ module Rack
|
|
45
47
|
|
46
48
|
# Checks if this session has been forced into mobile mode and returns that if that is the case. Otherwise
|
47
49
|
# checks the user agent via request.is_mobile?
|
48
|
-
def is_mobile_session?(env)
|
50
|
+
def is_mobile_session?(env, request)
|
49
51
|
session = env['rack.session'] ||= {}
|
50
52
|
|
51
|
-
request = Rack::Request.new(env)
|
52
53
|
if request.params[SCHMOBILE_MODE]
|
53
54
|
session[SCHMOBILE_MODE] = request.params[SCHMOBILE_MODE]
|
54
55
|
end
|
@@ -62,13 +63,16 @@ module Rack
|
|
62
63
|
|
63
64
|
# Returns true if this middleware has been configured with a redirect_to and the requested path is not already
|
64
65
|
# below the configured redirect_to
|
65
|
-
def redirect?(
|
66
|
-
|
66
|
+
def redirect?(request)
|
67
|
+
if @options[:if].is_a?(Proc)
|
68
|
+
return false unless @options[:if].call(request)
|
69
|
+
end
|
70
|
+
|
71
|
+
!redirect(request).empty? && request.path !~ /^#{redirect(request)}/
|
67
72
|
end
|
68
73
|
|
69
|
-
def redirect(
|
74
|
+
def redirect(request)
|
70
75
|
destination = @options[:redirect_to].to_s
|
71
|
-
request = Rack::Request.new(env)
|
72
76
|
build_path(destination, request)
|
73
77
|
end
|
74
78
|
|
@@ -76,12 +80,12 @@ module Rack
|
|
76
80
|
|
77
81
|
def build_path(destination, request)
|
78
82
|
final_destination = destination.dup
|
79
|
-
destination.scan(/\{\{\w+\}\}/)
|
83
|
+
destination.scan(/\{\{\w+\}\}/) do |call|
|
80
84
|
func = call.scan(/\w+/).to_s
|
81
85
|
if request.respond_to?(func)
|
82
86
|
final_destination.sub!(/\{\{#{func}\}\}/, request.send(func))
|
83
87
|
end
|
84
|
-
|
88
|
+
end
|
85
89
|
final_destination
|
86
90
|
end
|
87
91
|
|
data/schmobile.gemspec
CHANGED
data/test/test_schmobile.rb
CHANGED
@@ -53,40 +53,40 @@ class TestSchmobile < Test::Unit::TestCase
|
|
53
53
|
context "#is_mobile_session?" do
|
54
54
|
should "return false for regular browsers" do
|
55
55
|
Rack::Request.any_instance.expects(:params).returns({})
|
56
|
-
assert !@rack.is_mobile_session?(environment)
|
56
|
+
assert !@rack.is_mobile_session?(environment, request)
|
57
57
|
end
|
58
58
|
|
59
59
|
should "return true for a mobile browser" do
|
60
60
|
Rack::Request.any_instance.expects(:params).returns({})
|
61
|
-
assert @rack.is_mobile_session?(environment("HTTP_USER_AGENT" => iphone))
|
61
|
+
assert @rack.is_mobile_session?(environment, request("HTTP_USER_AGENT" => iphone))
|
62
62
|
end
|
63
63
|
|
64
64
|
should "return false when forced in the session" do
|
65
|
-
Rack::Request.any_instance.
|
65
|
+
Rack::Request.any_instance.stubs(:params).returns({})
|
66
66
|
Rack::Request.any_instance.expects(:is_mobile?).never
|
67
67
|
|
68
|
-
assert !@rack.is_mobile_session?(environment("HTTP_USER_AGENT" => iphone, "rack.session" => { Rack::Schmobile::SCHMOBILE_MODE => "disabled" }))
|
68
|
+
assert !@rack.is_mobile_session?(environment("HTTP_USER_AGENT" => iphone, "rack.session" => { Rack::Schmobile::SCHMOBILE_MODE => "disabled" }), request)
|
69
69
|
end
|
70
70
|
|
71
71
|
should "return true when forced in the session" do
|
72
72
|
Rack::Request.any_instance.expects(:params).returns({})
|
73
73
|
Rack::Request.any_instance.expects(:is_mobile?).never
|
74
74
|
|
75
|
-
assert @rack.is_mobile_session?(environment("rack.session" => { Rack::Schmobile::SCHMOBILE_MODE => "enabled" }))
|
75
|
+
assert @rack.is_mobile_session?(environment("rack.session" => { Rack::Schmobile::SCHMOBILE_MODE => "enabled" }), request)
|
76
76
|
end
|
77
77
|
|
78
78
|
should "return false when forced via a request parameter" do
|
79
79
|
Rack::Request.any_instance.stubs(:params).returns({ Rack::Schmobile::SCHMOBILE_MODE => "disabled" })
|
80
80
|
Rack::Request.any_instance.expects(:is_mobile?).never
|
81
81
|
|
82
|
-
assert !@rack.is_mobile_session?(environment("HTTP_USER_AGENT" => iphone))
|
82
|
+
assert !@rack.is_mobile_session?(environment, request("HTTP_USER_AGENT" => iphone))
|
83
83
|
end
|
84
84
|
|
85
85
|
should "return true when forced via a request parameter" do
|
86
86
|
Rack::Request.any_instance.stubs(:params).returns({ Rack::Schmobile::SCHMOBILE_MODE => "enabled" })
|
87
87
|
Rack::Request.any_instance.expects(:is_mobile?).never
|
88
88
|
|
89
|
-
assert @rack.is_mobile_session?(environment)
|
89
|
+
assert @rack.is_mobile_session?(environment, request)
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
@@ -113,26 +113,33 @@ class TestSchmobile < Test::Unit::TestCase
|
|
113
113
|
|
114
114
|
context "#redirect?" do
|
115
115
|
should "return true when not on mobile path" do
|
116
|
-
assert @rack.redirect?(
|
116
|
+
assert @rack.redirect?(request("PATH_INFO" => "/somewhere"))
|
117
117
|
end
|
118
118
|
|
119
119
|
should "return true on base path" do
|
120
|
-
assert @rack.redirect?(
|
120
|
+
assert @rack.redirect?(request("PATH_INFO" => "/"))
|
121
121
|
end
|
122
122
|
|
123
123
|
should "return false when already on path" do
|
124
|
-
assert !@rack.redirect?(
|
124
|
+
assert !@rack.redirect?(request("PATH_INFO" => "/wonderland"))
|
125
|
+
assert !@rack.redirect?(request("PATH_INFO" => "/wonderland/more/stuff"))
|
126
|
+
end
|
127
|
+
|
128
|
+
should "return false when :if resolves to false" do
|
129
|
+
@rack = Rack::Schmobile.new(@app, :if => Proc.new { |request| false })
|
130
|
+
assert !@rack.redirect?(request("PATH_INFO" => "/somewhere"))
|
125
131
|
end
|
126
132
|
end
|
127
133
|
|
128
134
|
context "#redirect" do
|
129
135
|
should "interpolate the argument string" do
|
130
136
|
@rack = Rack::Schmobile.new(@app, :redirect_to => "/wonderland/{{path}}")
|
131
|
-
assert_equal "/wonderland/wiffle", @rack.redirect(
|
137
|
+
assert_equal "/wonderland/wiffle", @rack.redirect(request("PATH_INFO" => "wiffle"))
|
132
138
|
end
|
139
|
+
|
133
140
|
should "interpolate a multipart argument string" do
|
134
141
|
@rack = Rack::Schmobile.new(@app, :redirect_to => "/wonderland/{{path}}/lemurs/{{path}}")
|
135
|
-
assert_equal "/wonderland/wiffle/lemurs/wiffle", @rack.redirect(
|
142
|
+
assert_equal "/wonderland/wiffle/lemurs/wiffle", @rack.redirect(request("PATH_INFO" => "wiffle"))
|
136
143
|
end
|
137
144
|
end
|
138
145
|
end
|
@@ -160,6 +167,10 @@ class TestSchmobile < Test::Unit::TestCase
|
|
160
167
|
'Mozilla/4.0 (compatible; MSIE 6.0; BREW 3.1.5; en )/800x480 Samsung SCH-U960'
|
161
168
|
end
|
162
169
|
|
170
|
+
def request(overwrite = {})
|
171
|
+
Rack::Request.new(environment(overwrite))
|
172
|
+
end
|
173
|
+
|
163
174
|
def environment(overwrite = {})
|
164
175
|
{
|
165
176
|
'GATEWAY_INTERFACE'=> 'CGI/1.2',
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schmobile
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 21
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Morten Primdahl
|