mobile_detect 0.0.1 → 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/README.md CHANGED
@@ -33,6 +33,10 @@ you can run that without second argument, but then gem cannot get device from cl
33
33
 
34
34
  => "iPad"
35
35
 
36
+ detect.os
37
+
38
+ => "iOS"
39
+
36
40
  ## Contributing
37
41
 
38
42
  1. Fork it
@@ -2,6 +2,33 @@ class MobileDetect
2
2
  attr_accessor :user_agent
3
3
 
4
4
 
5
+ # os, tablet and phone devices list is from php-mobile-detect
6
+ OperatingSystems = {
7
+ 'AndroidOS' => 'Android',
8
+ 'BlackBerryOS' => 'blackberry|rim tablet os',
9
+ 'PalmOS' => 'PalmOS|avantgo|blazer|elaine|hiptop|palm|plucker|xiino',
10
+ 'SymbianOS' => 'Symbian|SymbOS|Series60|Series40|SYB-[0-9]+|\bS60\b',
11
+ # @reference: http://en.wikipedia.org/wiki/Windows_Mobile
12
+ 'WindowsMobileOS' => 'Windows CE.*(PPC|Smartphone|Mobile|[0-9]{3}x[0-9]{3})|Window Mobile|Windows Phone [0-9.]+|WCE;',
13
+ # @reference: http://en.wikipedia.org/wiki/Windows_Phone
14
+ # http://wifeng.cn/?r=blog&a=view&id=106
15
+ # http://nicksnettravels.builttoroam.com/post/2011/01/10/Bogus-Windows-Phone-7-User-Agent-String.aspx
16
+ 'WindowsPhoneOS' => 'Windows Phone OS|XBLWP7|ZuneWP7',
17
+ 'iOS' => 'iphone|ipod|ipad',
18
+ #'FlashLiteOS' => '',
19
+ # http://en.wikipedia.org/wiki/MeeGo
20
+ # @todo: research MeeGo in UAs
21
+ 'MeeGoOS' => 'MeeGo',
22
+ # http://en.wikipedia.org/wiki/Maemo
23
+ # @todo: research Maemo in UAs
24
+ 'MaemoOS' => 'Maemo',
25
+ 'JavaOS' => 'J2ME/MIDP|Java/',
26
+ 'webOS' => 'webOS|hpwOS',
27
+ 'badaOS' => '\bBada\b',
28
+ 'BREWOS' => 'BREW'
29
+ }
30
+
31
+
5
32
  TabletDevices = {
6
33
  'BlackBerryTablet' => 'PlayBook|RIM Tablet',
7
34
  'iPad' => 'iPad|iPad.*Mobile',
@@ -39,6 +66,7 @@ class MobileDetect
39
66
  @device = nil
40
67
  @is_tablet = nil
41
68
  @is_mobile = nil
69
+ @os = nil
42
70
  end
43
71
 
44
72
  def tablet?
@@ -60,11 +88,19 @@ class MobileDetect
60
88
  @device
61
89
  end
62
90
 
91
+ def os
92
+ @os.nil? ? detect_os : @os
93
+ end
94
+
63
95
  private
64
96
  def is_tablet
65
97
  @is_tablet = regexp_device(TabletDevices)
66
98
  end
67
99
 
100
+ def detect_os
101
+ @os = regexp_os(OperatingSystems)
102
+ end
103
+
68
104
 
69
105
  def is_mobile
70
106
  @is_mobile = (is_mobile_headers || is_mobile_regexp)
@@ -75,7 +111,7 @@ class MobileDetect
75
111
  end
76
112
 
77
113
  def is_mobile_headers
78
- return false if @req_headers.blank?
114
+ return false unless @req_headers
79
115
  if @req_headers['HTTP_X_WAP_PROFILE'] ||
80
116
  @req_headers['HTTP_WAP_CONNECTION'] ||
81
117
  @req_headers['HTTP_X_WAP_CLIENTID'] ||
@@ -94,12 +130,31 @@ class MobileDetect
94
130
  end
95
131
  end
96
132
 
133
+ def regexp_os(reg_hash)
134
+ res = regexp(reg_hash)
135
+ if res
136
+ @os = res
137
+ return @os
138
+ else
139
+ return false
140
+ end
141
+ end
97
142
 
98
143
  def regexp_device(reg_hash)
144
+ res = regexp(reg_hash)
145
+ if res
146
+ @device = res
147
+ return true
148
+ else
149
+ return false
150
+ end
151
+ end
152
+
153
+
154
+ def regexp(reg_hash)
99
155
  reg_hash.each do |key,value|
100
156
  if @user_agent =~ /#{value}/i
101
- @device = key
102
- return true
157
+ return key
103
158
  end
104
159
  end
105
160
  false
@@ -1,3 +1,3 @@
1
1
  class MobileDetect
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,33 @@
1
+ require 'mobile_detect'
2
+
3
+ describe "Test iphone user_agent" do
4
+ it 'return mobile and device' do
5
+ md = MobileDetect.new('iphone mobile')
6
+ md.mobile?.should be true
7
+ md.tablet?.should be false
8
+ md.device.should eq 'iPhone'
9
+ end
10
+ end
11
+
12
+
13
+ describe "Test ipad user_agent" do
14
+ it 'return mobile and device' do
15
+ md = MobileDetect.new('ipad mobile')
16
+ md.mobile?.should be false
17
+ md.tablet?.should be true
18
+ md.device.should eq 'iPad'
19
+ end
20
+ end
21
+
22
+
23
+
24
+ describe "Test os detection" do
25
+ it 'ios test' do
26
+ md = MobileDetect.new('ipad mobile')
27
+ md.os.should == 'iOS'
28
+ end
29
+ it 'android test' do
30
+ md = MobileDetect.new('android tablet')
31
+ md.os.should == 'AndroidOS'
32
+ end
33
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobile_detect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -43,6 +43,7 @@ files:
43
43
  - lib/mobile_detect/mobile_detect.rb
44
44
  - lib/mobile_detect/version.rb
45
45
  - mobile_detect.gemspec
46
+ - spec/mobile_detect_spec.rb
46
47
  homepage: ''
47
48
  licenses: []
48
49
  post_install_message:
@@ -68,4 +69,5 @@ signing_key:
68
69
  specification_version: 3
69
70
  summary: Mobile_Detect is a lightweight gem for detecting mobile devices. It uses
70
71
  the user-agent string combined with specific HTTP headers to detect the mobile environment.
71
- test_files: []
72
+ test_files:
73
+ - spec/mobile_detect_spec.rb