rack-smartphone_detector 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog.md +7 -1
- data/README.md +11 -1
- data/lib/rack/smartphone_detector/checker.rb +5 -0
- data/lib/rack/smartphone_detector/version.rb +1 -1
- data/lib/rack/smartphone_detector.rb +12 -3
- data/spec/rack-smartphone_detector_spec.rb +58 -11
- metadata +4 -4
data/Changelog.md
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Rack::SmartphoneDetector [![Build Status](https://secure.travis-ci.org/ihara2525/rack-smartphone_detector.png?branch=master)](http://travis-ci.org/ihara2525/rack-smartphone_detector)
|
2
2
|
|
3
|
-
It's a very simple Rack middleware which detect the request comes from smartphones (iPhone/iPad/Android/Windows Phone).
|
3
|
+
It's a very simple Rack middleware which detect the request comes from smartphones (iPhone/iPad/Android/Android Tablet/Windows Phone).
|
4
4
|
|
5
5
|
You can use #from_smartphone? to detect the request.
|
6
6
|
|
@@ -8,6 +8,16 @@ You can use #from_smartphone? to detect the request.
|
|
8
8
|
request.from_smartphone? # true if it comes from such devices
|
9
9
|
```
|
10
10
|
|
11
|
+
or, use methods for each devices.
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
request.from_iphone? # true if it comes from iphone
|
15
|
+
request.from_ipad? # true if it comes from ipad
|
16
|
+
request.from_android? # true if it comes from android mobile
|
17
|
+
request.from_android_tablet? # true if it comes from android tablet
|
18
|
+
request.from_windows_phone? # true if it comes from windows phone
|
19
|
+
```
|
20
|
+
|
11
21
|
What this middleware does is just matching HTTP_USER_AGENT with identifier of smartphones.
|
12
22
|
|
13
23
|
## Installation
|
@@ -6,6 +6,11 @@ module Rack
|
|
6
6
|
def from_smartphone?
|
7
7
|
!env['rack.smartphone_detector.device'].nil?
|
8
8
|
end
|
9
|
+
|
10
|
+
SMARTPHONE_IDENTIFIERS.each do |info|
|
11
|
+
name = %Q{from_#{info[:identifier].downcase.gsub(' ', '_')}?}
|
12
|
+
define_method(name) { env['rack.smartphone_detector.device'] == info[:identifier] }
|
13
|
+
end
|
9
14
|
end
|
10
15
|
end
|
11
16
|
end
|
@@ -6,15 +6,24 @@ require 'rack/smartphone_detector/railtie' if defined?(Rails::Railtie)
|
|
6
6
|
|
7
7
|
module Rack
|
8
8
|
class SmartphoneDetector
|
9
|
-
|
9
|
+
SMARTPHONE_IDENTIFIERS = [
|
10
|
+
{ identifier: 'iPhone', regexp: /iPhone/ },
|
11
|
+
{ identifier: 'iPad', regexp: /iPad/ },
|
12
|
+
{ identifier: 'Android', regexp: /Android.+Mobile/ },
|
13
|
+
{ identifier: 'Android Tablet', regexp: /Android/ },
|
14
|
+
{ identifier: 'Windows Phone', regexp: /Windows Phone/ },
|
15
|
+
]
|
10
16
|
|
11
17
|
def initialize(app, options = {})
|
12
18
|
@app = app
|
13
19
|
end
|
14
20
|
|
15
21
|
def call(env)
|
16
|
-
|
17
|
-
env['
|
22
|
+
SMARTPHONE_IDENTIFIERS.each do |i|
|
23
|
+
if env['HTTP_USER_AGENT'] =~ i[:regexp]
|
24
|
+
env['rack.smartphone_detector.device'] = i[:identifier]
|
25
|
+
break
|
26
|
+
end
|
18
27
|
end
|
19
28
|
@app.call(env)
|
20
29
|
end
|
@@ -7,10 +7,11 @@ describe 'Rack::SmartphoneDetector' do
|
|
7
7
|
include Rack::Test::Methods
|
8
8
|
|
9
9
|
SMARTPHONES = [
|
10
|
-
{
|
11
|
-
{
|
12
|
-
{
|
13
|
-
{
|
10
|
+
{ name: 'iPhone', user_agent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25' },
|
11
|
+
{ name: 'iPad', user_agent: 'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A403 Safari/8536.25' },
|
12
|
+
{ name: 'Android', user_agent: 'Mozilla/5.0 (Linux; U; Android 4.0.1; ja-jp; Galaxy Nexus Build/ITL41D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30' },
|
13
|
+
{ name: 'Android Tablet', user_agent: 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03S) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19' },
|
14
|
+
{ name: 'Windows Phone', user_agent: 'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; FujitsuToshibaMobileCommun; IS12T; KDDI)' }
|
14
15
|
]
|
15
16
|
|
16
17
|
let(:app) do
|
@@ -19,18 +20,20 @@ describe 'Rack::SmartphoneDetector' do
|
|
19
20
|
end
|
20
21
|
|
21
22
|
it 'sets environment variable if the request comes from smartphone' do
|
22
|
-
SMARTPHONES.each do |
|
23
|
-
header 'User-Agent',
|
23
|
+
SMARTPHONES.each do |info|
|
24
|
+
header 'User-Agent', info[:user_agent]
|
24
25
|
get '/'
|
25
|
-
last_request.env['rack.smartphone_detector.device'].must_equal
|
26
|
+
last_request.env['rack.smartphone_detector.device'].must_equal info[:name]
|
26
27
|
end
|
27
28
|
end
|
28
29
|
|
29
30
|
describe '#from_smartphone?' do
|
30
31
|
it 'returns true if the request comes from smartphone' do
|
31
|
-
|
32
|
-
|
33
|
-
|
32
|
+
SMARTPHONES.each do |info|
|
33
|
+
header 'User-Agent', info[:user_agent]
|
34
|
+
get '/'
|
35
|
+
last_request.from_smartphone?.must_equal true
|
36
|
+
end
|
34
37
|
end
|
35
38
|
|
36
39
|
it 'returns false if the request does not come from smartphone' do
|
@@ -39,9 +42,53 @@ describe 'Rack::SmartphoneDetector' do
|
|
39
42
|
last_request.from_smartphone?.must_equal false
|
40
43
|
end
|
41
44
|
|
42
|
-
it 'returns false if the request comes from
|
45
|
+
it 'returns false if the request comes from device without user-agent' do
|
43
46
|
get '/'
|
44
47
|
last_request.from_smartphone?.must_equal false
|
45
48
|
end
|
46
49
|
end
|
50
|
+
|
51
|
+
describe '#from_iphone?' do
|
52
|
+
it 'returns true if the request comes from iphone' do
|
53
|
+
header 'User-Agent', user_agent('iPhone')
|
54
|
+
get '/'
|
55
|
+
last_request.from_iphone?.must_equal true
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe '#from_ipad?' do
|
60
|
+
it 'returns true if the request comes from ipad' do
|
61
|
+
header 'User-Agent', user_agent('iPad')
|
62
|
+
get '/'
|
63
|
+
last_request.from_ipad?.must_equal true
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe '#from_android?' do
|
68
|
+
it 'returns true if the request comes from android mobile' do
|
69
|
+
header 'User-Agent', user_agent('Android')
|
70
|
+
get '/'
|
71
|
+
last_request.from_android?.must_equal true
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe '#from_android_tablet?' do
|
76
|
+
it 'returns true if the request comes from android tablet' do
|
77
|
+
header 'User-Agent', user_agent('Android Tablet')
|
78
|
+
get '/'
|
79
|
+
last_request.from_android_tablet?.must_equal true
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe '#from_windows_phone?' do
|
84
|
+
it 'returns true if the request comes from windows phone' do
|
85
|
+
header 'User-Agent', user_agent('Windows Phone')
|
86
|
+
get '/'
|
87
|
+
last_request.from_windows_phone?.must_equal true
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def user_agent(name)
|
92
|
+
SMARTPHONES.find { |info| info[:name] == name }[:user_agent]
|
93
|
+
end
|
47
94
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-smartphone_detector
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
@@ -110,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
110
110
|
version: '0'
|
111
111
|
segments:
|
112
112
|
- 0
|
113
|
-
hash:
|
113
|
+
hash: 4226079495124680780
|
114
114
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
115
|
none: false
|
116
116
|
requirements:
|
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
119
119
|
version: '0'
|
120
120
|
segments:
|
121
121
|
- 0
|
122
|
-
hash:
|
122
|
+
hash: 4226079495124680780
|
123
123
|
requirements: []
|
124
124
|
rubyforge_project:
|
125
125
|
rubygems_version: 1.8.23
|