aaalex-detect_mobiles 0.0.2
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/MIT-LICENSE +20 -0
- data/README +27 -0
- data/lib/detect_mobiles.rb +35 -0
- data/test/detect_mobiles_test.rb +8 -0
- metadata +57 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 [name of plugin creator]
|
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
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
DetectMobiles
|
2
|
+
=============
|
3
|
+
|
4
|
+
!!! Also detects iPhone and iPod Touch, now !!!
|
5
|
+
|
6
|
+
This plugin provides an instance method to ActionController::Base which
|
7
|
+
can be used as a before_filter. The filter scans the request for
|
8
|
+
indication of a mobile browser or an iPhone/iPod and then sets
|
9
|
+
request.format to either :mobile or :iphone. This can be used in the
|
10
|
+
responds_to block and in the views.
|
11
|
+
|
12
|
+
Usage Example
|
13
|
+
=============
|
14
|
+
install the gem
|
15
|
+
add the gem to your config/environment.rb
|
16
|
+
-> config.gem "detect_mobiles"
|
17
|
+
add the before_filter to your controller
|
18
|
+
|
19
|
+
class ApplicationController < ActionController::Base
|
20
|
+
before_filter :detect_mobiles
|
21
|
+
end
|
22
|
+
|
23
|
+
now, the request.format will be changed to :mobile/:iphone depending on
|
24
|
+
the browser.
|
25
|
+
|
26
|
+
|
27
|
+
Copyright (c) 2008 Alex P, released under the MIT license
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# DetectMobiles
|
2
|
+
|
3
|
+
module DetectMobiles
|
4
|
+
def detect_mobiles
|
5
|
+
case
|
6
|
+
when is_request_from_iphone?
|
7
|
+
request.format = :iphone
|
8
|
+
logger.debug "detected iPhone -> request.format = :iphone"
|
9
|
+
when is_request_from_mobile?
|
10
|
+
request.format = :mobile
|
11
|
+
logger.debug "detected mobile browser -> request.format = :mobile"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def is_request_from_iphone?
|
16
|
+
request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/(Mobile\/.+Safari)/]
|
17
|
+
end
|
18
|
+
|
19
|
+
def is_request_from_mobile?
|
20
|
+
return true if request.env["HTTP_X_WAP_PROFILE"]
|
21
|
+
return true if request.env["HTTP_ACCEPT"] && /wap\.|\.wap/ =~ request.env["HTTP_ACCEPT"]
|
22
|
+
reg_arr = %w{midp j2me avantg docomo novarra palmos palmsource 240x320 opwv chtml pda windows\ ce mmp\/ blackberry}
|
23
|
+
reg_arr.concat %w{mib\/ symbian wireless nokia hand mobi phone cdm up\.b audio SIE\- SEC\- samsung HTC mot\- mitsu sagem}
|
24
|
+
reg_arr.concat %w{sony alcatel lg eric vx NEC philips mmm xx panasonic sharp wap sch rover pocket benq java pt pg vox}
|
25
|
+
reg_arr.concat %w{amoi bird compal kg voda sany kdd dbt sendo sgh gradi jb \d\d\di moto"}
|
26
|
+
reg_arr.each do |reg_exp|
|
27
|
+
return true if Regexp.new(reg_exp) =~ request.env["HTTP_USER_AGENT"]
|
28
|
+
end
|
29
|
+
return false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
ActionController::Base.class_eval do
|
34
|
+
include DetectMobiles
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aaalex-detect_mobiles
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Peuchert
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-07-07 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: RoR before_filter to detect mobile browsers
|
17
|
+
email: apeuchert@googlemail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
- MIT-LICENSE
|
25
|
+
files:
|
26
|
+
- lib/detect_mobiles.rb
|
27
|
+
- README
|
28
|
+
- MIT-LICENSE
|
29
|
+
has_rdoc: true
|
30
|
+
homepage: http://github.com/aaalex/detect_mobile
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options:
|
33
|
+
- --main
|
34
|
+
- README
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: "0"
|
42
|
+
version:
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
requirements: []
|
50
|
+
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.2.0
|
53
|
+
signing_key:
|
54
|
+
specification_version: 2
|
55
|
+
summary: RoR before_filter to detect mobile browsers
|
56
|
+
test_files:
|
57
|
+
- test/detect_mobiles_test.rb
|