schmobile 0.4.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +1 -12
- data/README.md +55 -0
- data/Rakefile +5 -39
- data/lib/schmobile/filters/is_mobile_param.rb +13 -0
- data/lib/schmobile/filters/mobile_session.rb +9 -0
- data/lib/schmobile/filters/mobile_user_agent.rb +11 -0
- data/lib/schmobile/filters.rb +30 -0
- data/lib/schmobile/middleware.rb +59 -0
- data/lib/schmobile/request_extension.rb +24 -0
- data/lib/schmobile/user_agents.rb +58 -0
- data/lib/schmobile.rb +10 -1
- data/schmobile.gemspec +19 -75
- data/test/helper.rb +2 -1
- data/test/test_filters.rb +20 -20
- data/test/test_middleware.rb +8 -8
- data/test/test_rack_request.rb +6 -6
- data/test/test_user_agents.rb +14 -14
- metadata +106 -127
- data/Gemfile.lock +0 -23
- data/LICENSE.txt +0 -20
- data/README.rdoc +0 -65
- data/VERSION +0 -1
- data/lib/rack/schmobile/filters/is_mobile_param.rb +0 -15
- data/lib/rack/schmobile/filters/mobile_session.rb +0 -11
- data/lib/rack/schmobile/filters/mobile_user_agent.rb +0 -13
- data/lib/rack/schmobile/filters.rb +0 -33
- data/lib/rack/schmobile/middleware.rb +0 -61
- data/lib/rack/schmobile/request_extension.rb +0 -26
- data/lib/rack/schmobile/user_agents.rb +0 -60
- data/lib/rack/schmobile.rb +0 -10
data/test/test_user_agents.rb
CHANGED
@@ -2,29 +2,29 @@ require 'helper'
|
|
2
2
|
|
3
3
|
class TestUserAgents < Test::Unit::TestCase
|
4
4
|
|
5
|
-
context "
|
5
|
+
context "Schmobile::UserAgents" do
|
6
6
|
context "#remove_user_agent_pattern" do
|
7
7
|
should "allow removal of a user agent" do
|
8
|
-
assert
|
9
|
-
|
10
|
-
assert !
|
8
|
+
assert Schmobile::UserAgents.is_mobile_agent?("novarra")
|
9
|
+
Schmobile::UserAgents.remove_user_agent_pattern("novarra")
|
10
|
+
assert !Schmobile::UserAgents.is_mobile_agent?("novarra")
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
context "#add_user_agent_pattern" do
|
15
15
|
should "allow adding a user agent" do
|
16
|
-
assert !
|
17
|
-
|
18
|
-
assert
|
16
|
+
assert !Schmobile::UserAgents.is_mobile_agent?("wibble")
|
17
|
+
Schmobile::UserAgents.add_user_agent_pattern("wibble")
|
18
|
+
assert Schmobile::UserAgents.is_mobile_agent?("wibble")
|
19
19
|
end
|
20
20
|
|
21
21
|
should "allow changing behavior for a non-mobile agent" do
|
22
|
-
assert !
|
23
|
-
|
24
|
-
assert !
|
25
|
-
|
26
|
-
assert
|
27
|
-
|
22
|
+
assert !Schmobile::UserAgents.is_mobile_agent?("ipad")
|
23
|
+
Schmobile::UserAgents.add_user_agent_pattern("ipad")
|
24
|
+
assert !Schmobile::UserAgents.is_mobile_agent?("ipad")
|
25
|
+
Schmobile::UserAgents.remove_non_mobile_user_agent_pattern("ipad")
|
26
|
+
assert Schmobile::UserAgents.is_mobile_agent?("ipad")
|
27
|
+
Schmobile::UserAgents.add_non_mobile_user_agent_pattern("ipad")
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
@@ -32,7 +32,7 @@ class TestUserAgents < Test::Unit::TestCase
|
|
32
32
|
should "return false for common browsers" do
|
33
33
|
[ :msie6, :msie8, :opera, :chrome ].each do |browser|
|
34
34
|
agent = self.send(browser)
|
35
|
-
assert !
|
35
|
+
assert !Schmobile::UserAgents.is_mobile_agent?(agent), "#{browser} should not detect as mobile, #{agent}"
|
36
36
|
end
|
37
37
|
end
|
38
38
|
end
|
metadata
CHANGED
@@ -1,169 +1,148 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: schmobile
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 4
|
9
|
-
- 1
|
10
|
-
version: 0.4.1
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Morten Primdahl
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
22
|
-
prerelease: false
|
23
|
-
type: :development
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ~>
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
hash: 19
|
30
|
-
segments:
|
31
|
-
- 1
|
32
|
-
- 1
|
33
|
-
- 0
|
34
|
-
version: 1.1.0
|
35
|
-
version_requirements: *id001
|
12
|
+
date: 2012-06-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
36
15
|
name: rack
|
37
|
-
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
38
23
|
prerelease: false
|
39
|
-
|
40
|
-
requirement: &id002 !ruby/object:Gem::Requirement
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
25
|
none: false
|
42
|
-
requirements:
|
43
|
-
- -
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
54
39
|
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: bundler
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
55
54
|
type: :development
|
56
|
-
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
none: false
|
58
|
-
requirements:
|
59
|
-
- -
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
|
62
|
-
|
63
|
-
- 0
|
64
|
-
version: "0"
|
65
|
-
version_requirements: *id003
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
66
63
|
name: shoulda
|
67
|
-
|
68
|
-
prerelease: false
|
69
|
-
type: :development
|
70
|
-
requirement: &id004 !ruby/object:Gem::Requirement
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
71
65
|
none: false
|
72
|
-
requirements:
|
73
|
-
- -
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
|
76
|
-
|
77
|
-
- 1
|
78
|
-
- 0
|
79
|
-
- 0
|
80
|
-
version: 1.0.0
|
81
|
-
version_requirements: *id004
|
82
|
-
name: bundler
|
83
|
-
- !ruby/object:Gem::Dependency
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
84
71
|
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: mocha
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
85
86
|
type: :development
|
86
|
-
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
87
89
|
none: false
|
88
|
-
requirements:
|
89
|
-
- -
|
90
|
-
- !ruby/object:Gem::Version
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
- 5
|
95
|
-
- 2
|
96
|
-
version: 1.5.2
|
97
|
-
version_requirements: *id005
|
98
|
-
name: jeweler
|
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
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
description: A mobile user agent detection Rack middleware. See the README.
|
95
|
+
email: primdahl@me.com
|
101
96
|
executables: []
|
102
|
-
|
103
97
|
extensions: []
|
104
|
-
|
105
|
-
|
106
|
-
- LICENSE.txt
|
107
|
-
- README.rdoc
|
108
|
-
files:
|
98
|
+
extra_rdoc_files: []
|
99
|
+
files:
|
109
100
|
- .document
|
101
|
+
- .gitignore
|
102
|
+
- .travis.yml
|
110
103
|
- Gemfile
|
111
|
-
-
|
112
|
-
- LICENSE.txt
|
113
|
-
- README.rdoc
|
104
|
+
- README.md
|
114
105
|
- Rakefile
|
115
|
-
- VERSION
|
116
|
-
- lib/rack/schmobile.rb
|
117
|
-
- lib/rack/schmobile/filters.rb
|
118
|
-
- lib/rack/schmobile/filters/is_mobile_param.rb
|
119
|
-
- lib/rack/schmobile/filters/mobile_session.rb
|
120
|
-
- lib/rack/schmobile/filters/mobile_user_agent.rb
|
121
|
-
- lib/rack/schmobile/middleware.rb
|
122
|
-
- lib/rack/schmobile/request_extension.rb
|
123
|
-
- lib/rack/schmobile/user_agents.rb
|
124
106
|
- lib/schmobile.rb
|
107
|
+
- lib/schmobile/filters.rb
|
108
|
+
- lib/schmobile/filters/is_mobile_param.rb
|
109
|
+
- lib/schmobile/filters/mobile_session.rb
|
110
|
+
- lib/schmobile/filters/mobile_user_agent.rb
|
111
|
+
- lib/schmobile/middleware.rb
|
112
|
+
- lib/schmobile/request_extension.rb
|
113
|
+
- lib/schmobile/user_agents.rb
|
125
114
|
- schmobile.gemspec
|
126
115
|
- test/helper.rb
|
127
116
|
- test/test_filters.rb
|
128
117
|
- test/test_middleware.rb
|
129
118
|
- test/test_rack_request.rb
|
130
119
|
- test/test_user_agents.rb
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
- MIT
|
120
|
+
homepage: http://github.com/zendesk/schmobile
|
121
|
+
licenses:
|
122
|
+
- Apache License Version 2.0
|
135
123
|
post_install_message:
|
136
124
|
rdoc_options: []
|
137
|
-
|
138
|
-
require_paths:
|
125
|
+
require_paths:
|
139
126
|
- lib
|
140
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
141
128
|
none: false
|
142
|
-
requirements:
|
143
|
-
- -
|
144
|
-
- !ruby/object:Gem::Version
|
145
|
-
|
146
|
-
|
147
|
-
- 0
|
148
|
-
version: "0"
|
149
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ! '>='
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
150
134
|
none: false
|
151
|
-
requirements:
|
152
|
-
- -
|
153
|
-
- !ruby/object:Gem::Version
|
154
|
-
|
155
|
-
segments:
|
156
|
-
- 0
|
157
|
-
version: "0"
|
135
|
+
requirements:
|
136
|
+
- - ! '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
158
139
|
requirements: []
|
159
|
-
|
160
140
|
rubyforge_project:
|
161
|
-
rubygems_version: 1.
|
141
|
+
rubygems_version: 1.8.23
|
162
142
|
signing_key:
|
163
143
|
specification_version: 3
|
164
|
-
summary: A
|
165
|
-
test_files:
|
166
|
-
- test/helper.rb
|
144
|
+
summary: A mobile user agent detection Rack middleware.
|
145
|
+
test_files:
|
167
146
|
- test/test_filters.rb
|
168
147
|
- test/test_middleware.rb
|
169
148
|
- test/test_rack_request.rb
|
data/Gemfile.lock
DELETED
@@ -1,23 +0,0 @@
|
|
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
DELETED
@@ -1,20 +0,0 @@
|
|
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
DELETED
@@ -1,65 +0,0 @@
|
|
1
|
-
= schmobile
|
2
|
-
|
3
|
-
A mobile user agent detection Rack middleware. It does this by providing +#is_mobile?+ to +Rack::Request+
|
4
|
-
|
5
|
-
== Forcing mobile mode
|
6
|
-
|
7
|
-
You can force toggle mobile mode by sending +is_mobile=true+ and +is_mobile=false+ as
|
8
|
-
parameters in the request URL. This setting will be stored in the session for subsequent requests.
|
9
|
-
This entirely overrides the user agent detection.
|
10
|
-
|
11
|
-
== User agent detection
|
12
|
-
|
13
|
-
You can add/remove user agents like so:
|
14
|
-
|
15
|
-
Rack::Schmobile::UserAgents.add_user_agent_pattern("wibble")
|
16
|
-
Rack::Schmobile::UserAgents.remove_user_agent_pattern("ipad")
|
17
|
-
|
18
|
-
== Filters
|
19
|
-
|
20
|
-
The outcome of the +request.is_mobile?+ call is the product of a series of filters getting evaluated
|
21
|
-
against the request. You can manipulate the Rack::Schmobile::Filters::CHAIN array to alter if a
|
22
|
-
request is deemed mobile or not. Add your own filter to bypass the check depending on e.g. location
|
23
|
-
or request format.
|
24
|
-
|
25
|
-
== Redirecting
|
26
|
-
|
27
|
-
It can be configured to return the user to an explicit destination:
|
28
|
-
|
29
|
-
use Rack::Schmobile, :redirect_to => "/mobile"
|
30
|
-
|
31
|
-
It supports string interpolation for dynamic destinations:
|
32
|
-
|
33
|
-
use Rack::Schmobile, :redirect_to => "/mobile/#!/{{path}}"
|
34
|
-
|
35
|
-
Finally the middleware provides a request level method to determine if the client is a mobile device
|
36
|
-
|
37
|
-
Rack::Request#is_mobile?
|
38
|
-
|
39
|
-
== Rolling out
|
40
|
-
|
41
|
-
use Rack::Schmobile, :redirect_to => "/mobile", :if => Proc.new { |request| request.host =~ /staging/ }
|
42
|
-
|
43
|
-
== Thanks
|
44
|
-
|
45
|
-
Not quite what we were looking for, but good inspiration and may work for you:
|
46
|
-
|
47
|
-
https://github.com/talison/rack-mobile-detect
|
48
|
-
https://github.com/brendanlim/mobile-fu
|
49
|
-
|
50
|
-
|
51
|
-
== Contributing to schmobile
|
52
|
-
|
53
|
-
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
54
|
-
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
55
|
-
* Fork the project
|
56
|
-
* Start a feature/bugfix branch
|
57
|
-
* Commit and push until you are happy with your contribution
|
58
|
-
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
59
|
-
* 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.
|
60
|
-
|
61
|
-
== Copyright
|
62
|
-
|
63
|
-
Copyright (c) 2011 Morten Primdahl. See LICENSE.txt for
|
64
|
-
further details.
|
65
|
-
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.4.1
|
@@ -1,15 +0,0 @@
|
|
1
|
-
module Rack
|
2
|
-
module Schmobile
|
3
|
-
module Filters
|
4
|
-
module IsMobileParam
|
5
|
-
def self.call(request)
|
6
|
-
if request.params.key?(Rack::Schmobile::IS_MOBILE)
|
7
|
-
request.session[Rack::Schmobile::IS_MOBILE] = (request.params[Rack::Schmobile::IS_MOBILE] == "true")
|
8
|
-
end
|
9
|
-
|
10
|
-
nil
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
require 'rack/schmobile/user_agents'
|
2
|
-
|
3
|
-
module Rack
|
4
|
-
module Schmobile
|
5
|
-
module Filters
|
6
|
-
module MobileUserAgent
|
7
|
-
def self.call(request)
|
8
|
-
request.session[Rack::Schmobile::IS_MOBILE] = Rack::Schmobile::UserAgents.is_mobile_agent?(request.user_agent)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'rack/schmobile/filters/mobile_session'
|
2
|
-
require 'rack/schmobile/filters/is_mobile_param'
|
3
|
-
require 'rack/schmobile/filters/mobile_user_agent'
|
4
|
-
|
5
|
-
module Rack
|
6
|
-
module Schmobile
|
7
|
-
# Filters are tests that get run against a request to determine if it's a mobile request or not.
|
8
|
-
# A filter can return true, false or nil. The first non-nil value of the filter chain is the
|
9
|
-
# one that gets used and this also gets used to mark the session as a "mobile session"
|
10
|
-
#
|
11
|
-
# You can manipulate the chain to add new conditions that check on e.g. request format.
|
12
|
-
module Filters
|
13
|
-
CHAIN = [
|
14
|
-
Rack::Schmobile::Filters::IsMobileParam, # Must come before session check
|
15
|
-
Rack::Schmobile::Filters::MobileSession,
|
16
|
-
Rack::Schmobile::Filters::MobileUserAgent # Always returns either true or false
|
17
|
-
]
|
18
|
-
|
19
|
-
def self.apply(request)
|
20
|
-
Rack::Schmobile::Filters::CHAIN.each do |filter|
|
21
|
-
result = filter.call(request)
|
22
|
-
unless result.nil?
|
23
|
-
request.session[Rack::Schmobile::IS_MOBILE] = result
|
24
|
-
break
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
request.session[Rack::Schmobile::IS_MOBILE] ||= false
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
@@ -1,61 +0,0 @@
|
|
1
|
-
module Rack
|
2
|
-
module Schmobile
|
3
|
-
class Middleware
|
4
|
-
|
5
|
-
def initialize(app, options = {})
|
6
|
-
@app = app
|
7
|
-
@options = options
|
8
|
-
end
|
9
|
-
|
10
|
-
def call(env)
|
11
|
-
request = Rack::Request.new(env)
|
12
|
-
|
13
|
-
if request.is_mobile? && redirect?(request)
|
14
|
-
[ 301, { "Location" => redirect_location(request) }, [] ]
|
15
|
-
else
|
16
|
-
@app.call(env)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
# Returns true if this middleware has been configured with a redirect_to and the requested path
|
21
|
-
# is not already below the configured redirect_to
|
22
|
-
def redirect?(request)
|
23
|
-
redirecting = true
|
24
|
-
|
25
|
-
if @options.key?(:redirect_if)
|
26
|
-
redirecting = @options[:redirect_if].call(request)
|
27
|
-
end
|
28
|
-
|
29
|
-
if @options.key?(:redirect_to)
|
30
|
-
redirecting &&= request.path !~ /^#{@options[:redirect_to]}/
|
31
|
-
else
|
32
|
-
redirecting = false
|
33
|
-
end
|
34
|
-
|
35
|
-
redirecting ? redirect_location(request) : nil
|
36
|
-
end
|
37
|
-
|
38
|
-
def redirect_location(request)
|
39
|
-
"#{@options[:redirect_to]}#{redirect_with(request)}"
|
40
|
-
end
|
41
|
-
|
42
|
-
def redirect_with(request)
|
43
|
-
build_path(@options[:redirect_with].to_s, request)
|
44
|
-
end
|
45
|
-
|
46
|
-
private
|
47
|
-
|
48
|
-
def build_path(destination, request)
|
49
|
-
final_destination = destination.dup
|
50
|
-
destination.scan(/\{\{\w+\}\}/) do |call|
|
51
|
-
func = call.scan(/\w+/).to_s
|
52
|
-
if request.respond_to?(func)
|
53
|
-
final_destination.sub!(/\{\{#{func}\}\}/, request.send(func))
|
54
|
-
end
|
55
|
-
end
|
56
|
-
final_destination
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
module Rack
|
2
|
-
module Schmobile
|
3
|
-
module Request
|
4
|
-
def is_mobile?
|
5
|
-
if @is_mobile.nil?
|
6
|
-
@is_mobile = Rack::Schmobile::Filters.apply(self)
|
7
|
-
end
|
8
|
-
|
9
|
-
@is_mobile
|
10
|
-
end
|
11
|
-
|
12
|
-
def toggle_mobile_session!
|
13
|
-
session[Rack::Schmobile::IS_MOBILE] = !is_mobile?
|
14
|
-
@is_mobile = nil
|
15
|
-
end
|
16
|
-
|
17
|
-
def is_device?(identifier)
|
18
|
-
user_agent =~ /#{identifier}/i
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
Rack::Request.class_eval do
|
25
|
-
include Rack::Schmobile::Request
|
26
|
-
end
|
@@ -1,60 +0,0 @@
|
|
1
|
-
module Rack
|
2
|
-
module Schmobile
|
3
|
-
class UserAgents
|
4
|
-
|
5
|
-
MOBILE_USER_AGENTS = %w(
|
6
|
-
alcatel amoi android astel audiovox blackberry cdm chtml danger docomo ericsson htc_touch
|
7
|
-
iphone ipod j2me kddi midp minimo mmp mobi mobile mobileexplorer mot- motorola netfront nokia
|
8
|
-
novarra palm pdxgw phone plucker pocket portable portalmmm sagem samsung sgh sie- softbank
|
9
|
-
sprint symbian telit ucweb up.b upg1 vodafone webos windows\ ce x240 x320 xiino
|
10
|
-
)
|
11
|
-
|
12
|
-
NON_MOBILE_USER_AGENTS = %w(
|
13
|
-
ipad
|
14
|
-
)
|
15
|
-
|
16
|
-
def self.remove_user_agent_pattern(pattern)
|
17
|
-
MOBILE_USER_AGENTS.delete(pattern)
|
18
|
-
@mobile_agent_matcher = nil
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.add_user_agent_pattern(pattern)
|
22
|
-
MOBILE_USER_AGENTS.push(*pattern)
|
23
|
-
@mobile_agent_matcher = nil
|
24
|
-
end
|
25
|
-
|
26
|
-
def self.remove_non_mobile_user_agent_pattern(pattern)
|
27
|
-
NON_MOBILE_USER_AGENTS.delete(pattern)
|
28
|
-
@non_mobile_agent_matcher = nil
|
29
|
-
end
|
30
|
-
|
31
|
-
def self.add_non_mobile_user_agent_pattern(pattern)
|
32
|
-
NON_MOBILE_USER_AGENTS.push(*pattern)
|
33
|
-
@non_mobile_agent_matcher = nil
|
34
|
-
end
|
35
|
-
|
36
|
-
def self.is_mobile_agent?(user_agent)
|
37
|
-
agent = user_agent.to_s.downcase
|
38
|
-
mobile = !(agent =~ mobile_agent_matcher).nil?
|
39
|
-
mobile = mobile && agent !~ non_mobile_agent_matcher unless NON_MOBILE_USER_AGENTS.empty?
|
40
|
-
mobile
|
41
|
-
end
|
42
|
-
|
43
|
-
def self.mobile_agent_matcher
|
44
|
-
@mobile_agent_matcher ||= Regexp.new(MOBILE_USER_AGENTS.uniq.compact.map { |v| Regexp.escape(v) }.join("|"))
|
45
|
-
end
|
46
|
-
|
47
|
-
def self.non_mobile_agent_matcher
|
48
|
-
@non_mobile_agent_matcher ||= Regexp.new(NON_MOBILE_USER_AGENTS.uniq.compact.map { |v| Regexp.escape(v) }.join("|"))
|
49
|
-
end
|
50
|
-
|
51
|
-
def self.matched_by?(user_agent)
|
52
|
-
MOBILE_USER_AGENTS.each do |agent|
|
53
|
-
return agent if user_agent =~ /#{agent}/
|
54
|
-
end
|
55
|
-
nil
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|