rack-mobile-detect 0.1.1 → 0.2.0

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/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ ### 0.2.0: Mar 14, 2010 ###
2
+ * Added redirect_to option from github.com/joren
3
+ * Added redirect_map option
4
+ * Added dependency on rack
5
+ * Added more test for redirect options
6
+ * Renamed rb file to mobile-detect.rb
7
+ * Updated tests, echo_env.rb to reference new file
data/README.md CHANGED
@@ -3,6 +3,10 @@ Install
3
3
 
4
4
  sudo gem install rack-mobile-detect -s http://gemcutter.org
5
5
 
6
+ In your code:
7
+
8
+ require 'rack-mobile-detect'
9
+
6
10
  Overview
7
11
  ========
8
12
 
@@ -77,6 +81,32 @@ Configuration/CLDC-1.1 VendorID/102' connects, the value of
77
81
  This allows you to limit the catchall expression to only the device
78
82
  list you choose.
79
83
 
84
+ Redirects
85
+ =========
86
+
87
+ use Rack::MobileDetect, :redirect_to => '/mobile'
88
+
89
+ This allows you to choose a custom redirect path any time a mobile
90
+ device is detected.
91
+
92
+ use Rack::MobileDetect, :targeted => /BlackBerry|iPhone/,
93
+ :redirect_map => { 'BlackBerry' => '/m/blackberry', 'iPhone' => '/m/iphone' }
94
+
95
+ This allows you to map specific redirect URLs to targeted devices. The
96
+ key in the redirect_map should be the value of the matched pattern.
97
+
98
+ use Rack::MobileDetect, :targeted => /BlackBerry|iPhone/,
99
+ :redirect_map => { 'BlackBerry' => '/m/blackberry', 'iPhone' => '/m/iphone' },
100
+ :redirect_to => '/mobile'
101
+
102
+ This allows you to map targeted devices to specific URLs. Non-targeted
103
+ mobile devices will redirect to the url specified by redirect_to.
104
+
105
+ In the example above, BlackBerrys and iPhones get redirected to
106
+ device-specific URLs. All other mobile devices get redirected to
107
+ '/mobile'.
108
+
109
+
80
110
  Utils
81
111
  =====
82
112
 
data/Rakefile CHANGED
@@ -8,11 +8,12 @@ begin
8
8
  gem.summary = %Q{Rack middleware for ruby webapps to detect mobile devices.}
9
9
  gem.description = %Q{Rack::MobileDetect detects mobile devices and adds an
10
10
  X_MOBILE_DEVICE header to the request if a mobile device is detected. Specific
11
- devices can be targeted with custom Regexps.}
11
+ devices can be targeted with custom Regexps and redirect support is available.}
12
12
  gem.email = "accounts@majortom.fastmail.us"
13
13
  gem.homepage = "http://github.com/talison/rack-mobile-detect"
14
- gem.authors = ["Tom Alison", "Michael Wood"]
15
- gem.add_development_dependency "shoulda", ">= 0"
14
+ gem.authors = ["Tom Alison"]
15
+ gem.add_development_dependency("shoulda", ">= 0")
16
+ gem.add_dependency("rack")
16
17
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
18
  end
18
19
  Jeweler::GemcutterTasks.new
data/TODO CHANGED
@@ -1 +1,3 @@
1
1
  * Allow options to disable UAProf detection, Accept detection
2
+
3
+
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
- :build:
3
- :minor: 1
4
- :patch: 1
5
2
  :major: 0
3
+ :minor: 2
4
+ :build:
5
+ :patch: 0
@@ -20,6 +20,8 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
+ require 'rack'
24
+
23
25
  module Rack
24
26
  #
25
27
  # Full project at http://github.com/talison/rack-mobile-detect
@@ -121,6 +123,11 @@ module Rack
121
123
  '240x320|320x320|mobileexplorer|j2me|sgh|portable|sprint|vodafone|' +
122
124
  'docomo|kddi|softbank|pdxgw|j-phone|astel|minimo|plucker|netfront|' +
123
125
  'xiino|mot-v|mot-e|portalmmm|sagem|sie-s|sie-m|android|ipod', true)
126
+
127
+ # A URL that specifies a single redirect-url for any device
128
+ @redirect_to = options[:redirect_to]
129
+ # A mapping of devices to redirect URLs, for targeted devices
130
+ @redirect_map = options[:redirect_map]
124
131
  end
125
132
 
126
133
  # Because the web app may be multithreaded, this method must
@@ -142,9 +149,28 @@ module Rack
142
149
  # Fall back on catch-all User-Agent regex
143
150
  device ||= Regexp.new(@regex_ua_catchall).match(user_agent) != nil
144
151
 
145
- env[X_HEADER] = device.to_s if device
152
+ if device
153
+ device = device.to_s
154
+
155
+ env[X_HEADER] = device
156
+ redirect = check_for_redirect(device)
157
+
158
+ if redirect
159
+ path = Rack::Utils.unescape(env['PATH_INFO'])
160
+ return [301, {'Location' => redirect}, []] if redirect && path !~ /^#{redirect}/
161
+ end
162
+ end
146
163
 
147
164
  @app.call(env)
148
165
  end
166
+
167
+ # Checks to see if any redirect options were passed in
168
+ # and returns the appropriate redirect or nil (if no redirect requested)
169
+ def check_for_redirect(device)
170
+ # Find the device-specific redirect in the map, if exists
171
+ return @redirect_map[device] if @redirect_map && @redirect_map.has_key?(device)
172
+ # Return global redirect, or nil
173
+ return @redirect_to
174
+ end
149
175
  end
150
176
  end
@@ -5,28 +5,30 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rack-mobile-detect}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Tom Alison", "Michael Wood"]
12
- s.date = %q{2009-10-30}
11
+ s.authors = ["Tom Alison"]
12
+ s.date = %q{2010-03-14}
13
13
  s.description = %q{Rack::MobileDetect detects mobile devices and adds an
14
14
  X_MOBILE_DEVICE header to the request if a mobile device is detected. Specific
15
- devices can be targeted with custom Regexps.}
15
+ devices can be targeted with custom Regexps and redirect support is available.}
16
16
  s.email = %q{accounts@majortom.fastmail.us}
17
17
  s.extra_rdoc_files = [
18
18
  "LICENSE",
19
- "README.md"
19
+ "README.md",
20
+ "TODO"
20
21
  ]
21
22
  s.files = [
22
23
  ".document",
23
24
  ".gitignore",
25
+ "CHANGELOG.md",
24
26
  "LICENSE",
25
27
  "README.md",
26
28
  "Rakefile",
27
29
  "TODO",
28
30
  "VERSION.yml",
29
- "lib/rack-mobile-detect.rb",
31
+ "lib/rack/mobile-detect.rb",
30
32
  "rack-mobile-detect.gemspec",
31
33
  "test/helper.rb",
32
34
  "test/test_rack-mobile-detect.rb",
@@ -38,8 +40,8 @@ Gem::Specification.new do |s|
38
40
  s.rubygems_version = %q{1.3.5}
39
41
  s.summary = %q{Rack middleware for ruby webapps to detect mobile devices.}
40
42
  s.test_files = [
41
- "test/test_rack-mobile-detect.rb",
42
- "test/helper.rb"
43
+ "test/helper.rb",
44
+ "test/test_rack-mobile-detect.rb"
43
45
  ]
44
46
 
45
47
  if s.respond_to? :specification_version then
@@ -48,11 +50,14 @@ Gem::Specification.new do |s|
48
50
 
49
51
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
52
  s.add_development_dependency(%q<shoulda>, [">= 0"])
53
+ s.add_runtime_dependency(%q<rack>, [">= 0"])
51
54
  else
52
55
  s.add_dependency(%q<shoulda>, [">= 0"])
56
+ s.add_dependency(%q<rack>, [">= 0"])
53
57
  end
54
58
  else
55
59
  s.add_dependency(%q<shoulda>, [">= 0"])
60
+ s.add_dependency(%q<rack>, [">= 0"])
56
61
  end
57
62
  end
58
63
 
data/test/helper.rb CHANGED
@@ -4,7 +4,7 @@ require 'shoulda'
4
4
 
5
5
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
6
  $LOAD_PATH.unshift(File.dirname(__FILE__))
7
- require 'rack-mobile-detect'
7
+ require 'rack/mobile-detect'
8
8
 
9
9
  class Test::Unit::TestCase
10
10
  end
@@ -122,6 +122,92 @@ class TestRackMobileDetect < Test::Unit::TestCase
122
122
  end
123
123
  end
124
124
 
125
+ context "An app with a custom redirect" do
126
+ setup do
127
+ @app = test_app
128
+ # Custom redirect
129
+ @rack = Rack::MobileDetect.new(@app, :redirect_to => '/mobile')
130
+ end
131
+
132
+ should "redirect to mobile website" do
133
+ env = test_env({ 'HTTP_USER_AGENT' => iphone })
134
+ status, headers, body = @rack.call(env)
135
+ assert_equal 'iPhone', env[x_mobile]
136
+
137
+ assert_equal(301, status)
138
+ assert_equal({'Location' => "/mobile"}, headers)
139
+ end
140
+ end
141
+
142
+ context "An app with a custom redirect map" do
143
+ setup do
144
+ @app = test_app
145
+ redirects = { "myphone" => "/m/myphone", "yourphone" => "/m/yourphone" }
146
+ # Target fake devices
147
+ @rack = Rack::MobileDetect.new(@app,
148
+ :targeted => /myphone|yourphone/,
149
+ :redirect_map => redirects)
150
+ end
151
+
152
+ should "redirect to the custom url of the targeted devices" do
153
+ env = test_env({ 'HTTP_USER_AGENT' => 'myphone rocks' })
154
+ status, headers, body = @rack.call(env)
155
+ assert_equal 'myphone', env[x_mobile]
156
+
157
+ assert_equal(301, status)
158
+ assert_equal({'Location' => "/m/myphone"}, headers)
159
+
160
+
161
+ env = test_env({ 'HTTP_USER_AGENT' => 'yourphone sucks' })
162
+ status, headers, body = @rack.call(env)
163
+ assert_equal 'yourphone', env[x_mobile]
164
+
165
+ assert_equal(301, status)
166
+ assert_equal({'Location' => "/m/yourphone"}, headers)
167
+
168
+ end
169
+
170
+ should "not redirect a non-targeted device" do
171
+ env = test_env({ 'HTTP_USER_AGENT' => 'some wap phone' })
172
+ status, headers, body = @rack.call(env)
173
+ assert_equal 'true', env[x_mobile]
174
+
175
+ assert_not_equal(301, status)
176
+ end
177
+ end
178
+
179
+ context "An app with a custom redirect map and redirect_to option" do
180
+ setup do
181
+ @app = test_app
182
+ redirects = { "myphone" => "/m/myphone", "yourphone" => "/m/yourphone" }
183
+ # Target fake devices
184
+ @rack = Rack::MobileDetect.new(@app,
185
+ :targeted => /myphone|yourphone/,
186
+ :redirect_map => redirects,
187
+ :redirect_to => '/m/genericdevice')
188
+ end
189
+
190
+ should "use the redirect value in the redirect map when targeted" do
191
+ env = test_env({ 'HTTP_USER_AGENT' => 'myphone rocks' })
192
+ status, headers, body = @rack.call(env)
193
+ assert_equal 'myphone', env[x_mobile]
194
+
195
+ assert_equal(301, status)
196
+ assert_equal({'Location' => "/m/myphone"}, headers)
197
+
198
+ end
199
+
200
+ should "use redirect_to to redirect a device not in the map" do
201
+ env = test_env({ 'HTTP_USER_AGENT' => 'some wap phone' })
202
+ status, headers, body = @rack.call(env)
203
+ assert_equal 'true', env[x_mobile]
204
+
205
+ assert_equal(301, status)
206
+ assert_equal({'Location' => "/m/genericdevice"}, headers)
207
+ end
208
+ end
209
+
210
+
125
211
  # Expected x_header
126
212
  def x_mobile
127
213
  Rack::MobileDetect::X_HEADER
data/util/echo_env.rb CHANGED
@@ -3,7 +3,7 @@ require 'sinatra'
3
3
 
4
4
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
5
 
6
- require 'rack-mobile-detect'
6
+ require 'rack/mobile-detect'
7
7
 
8
8
  use Rack::MobileDetect
9
9
 
metadata CHANGED
@@ -1,16 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-mobile-detect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Alison
8
- - Michael Wood
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
11
 
13
- date: 2009-10-30 00:00:00 -04:00
12
+ date: 2010-03-14 00:00:00 -05:00
14
13
  default_executable:
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
@@ -23,10 +22,20 @@ dependencies:
23
22
  - !ruby/object:Gem::Version
24
23
  version: "0"
25
24
  version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rack
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
26
35
  description: |-
27
36
  Rack::MobileDetect detects mobile devices and adds an
28
37
  X_MOBILE_DEVICE header to the request if a mobile device is detected. Specific
29
- devices can be targeted with custom Regexps.
38
+ devices can be targeted with custom Regexps and redirect support is available.
30
39
  email: accounts@majortom.fastmail.us
31
40
  executables: []
32
41
 
@@ -35,15 +44,17 @@ extensions: []
35
44
  extra_rdoc_files:
36
45
  - LICENSE
37
46
  - README.md
47
+ - TODO
38
48
  files:
39
49
  - .document
40
50
  - .gitignore
51
+ - CHANGELOG.md
41
52
  - LICENSE
42
53
  - README.md
43
54
  - Rakefile
44
55
  - TODO
45
56
  - VERSION.yml
46
- - lib/rack-mobile-detect.rb
57
+ - lib/rack/mobile-detect.rb
47
58
  - rack-mobile-detect.gemspec
48
59
  - test/helper.rb
49
60
  - test/test_rack-mobile-detect.rb
@@ -77,5 +88,5 @@ signing_key:
77
88
  specification_version: 3
78
89
  summary: Rack middleware for ruby webapps to detect mobile devices.
79
90
  test_files:
80
- - test/test_rack-mobile-detect.rb
81
91
  - test/helper.rb
92
+ - test/test_rack-mobile-detect.rb