rack-smartphone_detector 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c3d807710a9fe9027964d1393d939d4feecfe8a0
4
+ data.tar.gz: dab547033f2b45e5e20995938950f946215c70cd
5
+ SHA512:
6
+ metadata.gz: 18ada9aeaf2b27a8d1bc38153cd0680349baa175a9a883b5588f9b5f30d59bec15cc51cee55fde3190fdb53e5f3bdb5e7974112603d83c99d598cad408935e38
7
+ data.tar.gz: 17cefa2d21551bcd271135d91b9e13267d37cb122c504ec066035ad8fb716a038eee802c032a58ec82fa6f6b21ba0506f2b2eeb718d902760066fd2b721db321
@@ -7,6 +7,10 @@ module Rack
7
7
  !env['rack.smartphone_detector.device'].nil?
8
8
  end
9
9
 
10
+ def smartphone_version
11
+ env['rack.smartphone_detector.device_version']
12
+ end
13
+
10
14
  SMARTPHONE_IDENTIFIERS.each do |info|
11
15
  name = %Q{from_#{info[:identifier].downcase.gsub(' ', '_')}?}
12
16
  define_method(name) { env['rack.smartphone_detector.device'] == info[:identifier] }
@@ -1,6 +1,6 @@
1
1
  # coding: utf-8
2
2
  module Rack
3
3
  class SmartphoneDetector
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.5'
5
5
  end
6
6
  end
@@ -6,12 +6,18 @@ require 'rack/smartphone_detector/railtie' if defined?(Rails::Railtie)
6
6
 
7
7
  module Rack
8
8
  class SmartphoneDetector
9
+ VERSION_PATTERNS = {
10
+ ios: /OS\s+([\d_]+)\s+like Mac OS X/,
11
+ android: /Android\s+([\d\.]+);/,
12
+ windows_phone: /Windows Phone OS\s+([\d\.]+);/,
13
+ }
14
+
9
15
  SMARTPHONE_IDENTIFIERS = [
10
- { identifier: 'iPhone', regexp: /iPhone/ },
11
- { identifier: 'iPad', regexp: /iPad/ },
12
- { identifier: 'Android', regexp: /Android.+Mobi(le)?/ },
13
- { identifier: 'Android Tablet', regexp: /Android/ },
14
- { identifier: 'Windows Phone', regexp: /Windows Phone/ },
16
+ { identifier: 'iPhone', regexp: /iPhone/, version_type: :ios },
17
+ { identifier: 'iPad', regexp: /iPad/, version_type: :ios },
18
+ { identifier: 'Android', regexp: /Android.+Mobi(le)?/, version_type: :android },
19
+ { identifier: 'Android Tablet', regexp: /Android/, version_type: :android },
20
+ { identifier: 'Windows Phone', regexp: /Windows Phone/, version_type: :windows_phone },
15
21
  ]
16
22
 
17
23
  def initialize(app, options = {})
@@ -22,11 +28,20 @@ module Rack
22
28
  SMARTPHONE_IDENTIFIERS.each do |i|
23
29
  if env['HTTP_USER_AGENT'] =~ i[:regexp]
24
30
  env['rack.smartphone_detector.device'] = i[:identifier]
31
+ detect_version(env, i[:version_type])
25
32
  break
26
33
  end
27
34
  end
28
35
  @app.call(env)
29
36
  end
37
+
38
+ private
39
+
40
+ def detect_version(env, version_type)
41
+ if env['HTTP_USER_AGENT'] =~ VERSION_PATTERNS[version_type]
42
+ env['rack.smartphone_detector.device_version'] = Regexp.last_match[1]
43
+ end
44
+ end
30
45
  end
31
46
 
32
47
  class Request
@@ -9,32 +9,56 @@ describe 'Rack::SmartphoneDetector' do
9
9
  SMARTPHONES = [
10
10
  {
11
11
  name: 'iPhone',
12
+ version: '5_1_1',
13
+ user_agents: [
14
+ 'Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3',
15
+ ]
16
+ },
17
+ {
18
+ name: 'iPhone',
19
+ version: '6_0',
12
20
  user_agents: [
13
21
  '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',
14
22
  ]
15
23
  },
16
24
  {
17
25
  name: 'iPad',
26
+ version: '6_0',
18
27
  user_agents: [
19
28
  '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'
20
29
  ]
21
30
  },
22
31
  {
23
32
  name: 'Android',
33
+ version: '4.0.1',
24
34
  user_agents: [
25
35
  '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',
26
- 'Mozilla/5.0 (Android; Mobile; rv:18.0) Gecko/18.0 Firefox/18.0',
36
+ ]
37
+ },
38
+ {
39
+ name: 'Android',
40
+ version: '4.2.1',
41
+ user_agents: [
27
42
  'Opera/9.80 (Android 4.2.1; Linux; Opera Mobi/ADR-1301080958) Presto/2.11.355 Version/12.10'
28
43
  ]
29
44
  },
45
+ {
46
+ name: 'Android',
47
+ version: nil,
48
+ user_agents: [
49
+ 'Mozilla/5.0 (Android; Mobile; rv:18.0) Gecko/18.0 Firefox/18.0',
50
+ ]
51
+ },
30
52
  {
31
53
  name: 'Android Tablet',
54
+ version: '4.1.1',
32
55
  user_agents: [
33
56
  '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'
34
57
  ]
35
58
  },
36
59
  {
37
60
  name: 'Windows Phone',
61
+ version: '7.5',
38
62
  user_agents: [
39
63
  'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; FujitsuToshibaMobileCommun; IS12T; KDDI)'
40
64
  ]
@@ -84,6 +108,18 @@ describe 'Rack::SmartphoneDetector' do
84
108
  end
85
109
  end
86
110
 
111
+ describe '#smartphone_version' do
112
+ it 'returns version number string if it could be detected' do
113
+ SMARTPHONES.each do |info|
114
+ info[:user_agents].each do |ua|
115
+ header 'User-Agent', ua
116
+ get '/'
117
+ last_request.smartphone_version.must_equal info[:version]
118
+ end
119
+ end
120
+ end
121
+ end
122
+
87
123
  describe '#from_iphone?' do
88
124
  it 'returns true if the request comes from iphone' do
89
125
  user_agents('iPhone').each do |ua|
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-smartphone_detector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
5
- prerelease:
4
+ version: 0.0.5
6
5
  platform: ruby
7
6
  authors:
8
7
  - Masahiro Ihara
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-01-09 00:00:00.000000000 Z
11
+ date: 2013-11-08 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rack
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
@@ -30,49 +27,43 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: minitest
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '>='
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rack-test
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - '>='
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - '>='
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0'
78
69
  description: Simple Rack middleware which detect the request comes from smartphone
@@ -98,27 +89,26 @@ files:
98
89
  - spec/rack-smartphone_detector_spec.rb
99
90
  homepage: https://github.com/ihara2525/rack-smartphone_detector
100
91
  licenses: []
92
+ metadata: {}
101
93
  post_install_message:
102
94
  rdoc_options: []
103
95
  require_paths:
104
96
  - lib
105
97
  required_ruby_version: !ruby/object:Gem::Requirement
106
- none: false
107
98
  requirements:
108
- - - ! '>='
99
+ - - '>='
109
100
  - !ruby/object:Gem::Version
110
101
  version: '0'
111
102
  required_rubygems_version: !ruby/object:Gem::Requirement
112
- none: false
113
103
  requirements:
114
- - - ! '>='
104
+ - - '>='
115
105
  - !ruby/object:Gem::Version
116
106
  version: '0'
117
107
  requirements: []
118
108
  rubyforge_project:
119
- rubygems_version: 1.8.23
109
+ rubygems_version: 2.1.10
120
110
  signing_key:
121
- specification_version: 3
111
+ specification_version: 4
122
112
  summary: Simple Rack middleware which detect the request comes from smartphone
123
113
  test_files:
124
114
  - spec/rack-smartphone_detector_spec.rb