active_device 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [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,36 @@
1
+ ActiveDevice : Open Mobile Device Detector
2
+ ============
3
+
4
+ Mobile and Devices User Agent Detector, For Mobile Handsets, Devices and Desktop Browsers.
5
+ For Detect the Mobile and Recognize its Brand, Model, Engine, OS and Browser.
6
+
7
+ Usage
8
+ =======
9
+
10
+ Add this one line to your Application controller.
11
+
12
+ class ApplicationController < ActionController::Base
13
+ include ActiveDevice
14
+ end
15
+
16
+ Once this is in place, any request that comes from a mobile device will be be
17
+ set as :mobile format. It is up to you to determine how you want to handle
18
+ these requests. It is also up to you to create the .mobile.erb versions of
19
+ your views that are to be requested.
20
+
21
+ Then add the line below to config/initializers/mime_types.rb
22
+
23
+ Mime::Type.register_alias "text/html", :mobile
24
+
25
+ For Skiping setting the Format to :mobile
26
+ Add this line to your Application Controller
27
+
28
+ skip_before_filter :set_mobile_format
29
+
30
+
31
+ Example
32
+ =======
33
+
34
+ More will Coming Soon, Wait for MobiThought
35
+
36
+ Copyright (c) 2009 Shenouda Bertel, MobiThought, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+ require 'rake/gempackagetask'
6
+
7
+ desc 'Default: run unit tests.'
8
+ task :default => :test
9
+
10
+ desc 'Test the active_device plugin.'
11
+ Rake::TestTask.new(:test) do |t|
12
+ t.libs << 'lib'
13
+ t.libs << 'test'
14
+ t.pattern = 'test/**/*_test.rb'
15
+ t.verbose = true
16
+ end
17
+
18
+ desc 'Generate documentation for the active_device plugin.'
19
+ Rake::RDocTask.new(:rdoc) do |rdoc|
20
+ rdoc.rdoc_dir = 'rdoc'
21
+ rdoc.title = 'ActiveDevice'
22
+ rdoc.options << '--line-numbers' << '--inline-source'
23
+ rdoc.rdoc_files.include('README')
24
+ rdoc.rdoc_files.include('lib/**/*.rb')
25
+ end
26
+
27
+ PKG_FILES = FileList[ '[a-zA-Z]*', 'generators/**/*', 'lib/**/*', 'rails/**/*', 'tasks/**/*', 'test/**/*' ]
28
+
29
+ spec = Gem::Specification.new do |s|
30
+ s.name = "active_device"
31
+ s.version = "1.0.0"
32
+ s.authors = ["Shenouda Bertel"]
33
+ s.description = "Device UserAgent Detector"
34
+ s.email = "sbertel@mobithought.com"
35
+ s.homepage = "http://mobithought.com/"
36
+ s.files = PKG_FILES.to_a
37
+ s.require_paths = ["lib"]
38
+ s.rubyforge_project = "active_device"
39
+ s.rubygems_version = "1.3.5"
40
+ s.summary = "Mobile Device Detector"
41
+ s.platform = Gem::Platform::RUBY
42
+ s.has_rdoc = false
43
+ s.extra_rdoc_files = ["README"]
44
+ end
45
+
46
+ desc 'Turn this plugin into a gem.'
47
+ Rake::GemPackageTask.new(spec) do |pkg|
48
+ pkg.gem_spec = spec
49
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init.rb"
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
data/lib/active_device.rb CHANGED
@@ -22,27 +22,117 @@
22
22
  #++
23
23
 
24
24
  require 'active_device/user_agent'
25
- require 'active_device/agent_spec'
26
25
  require 'active_device/brand'
27
26
  require 'active_device/model'
28
27
  require 'active_device/handset'
28
+ require 'active_device/bot'
29
+ require 'active_device/engine'
30
+ require 'active_device/browser'
31
+ require 'active_device/os'
29
32
 
30
- #user_agent = 'Mozilla/5.0 (SymbianOS/9.3; U; Series60/3.2 NokiaE75-1/110.48.125 Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413'
31
- #agent = UserAgent.new user_agent
32
- #puts agent
33
- #puts "Name:#{agent.name}"
34
- #puts "Version:#{agent.version}"
35
- #puts "Engine:#{agent.engine}"
36
- #puts "EVersion:#{agent.engine_version}"
37
- #puts "OS:#{agent.os}"
38
- #puts "OSVersion:#{agent.os_version}"
39
- #if agent.os == :SymbianOS
40
- # puts "OSS:#{agent.os_series}"
41
- #end
42
- #puts "Brand:#{agent.brand}"
43
- #puts "Model:#{agent.model}"
44
- #puts "================================="
45
- #
46
- #puts agent.is_mobile?
47
- #puts '============================'
48
- #puts agent.is_handset? 'e75'
33
+ module ActiveDevice
34
+
35
+ def self.included(base)
36
+ base.before_filter :set_mobile_format
37
+ base.helper_method :is_mobile_device?, :is_mobile_browser?, :is_desktop_browser?, :is_bot?
38
+ base.helper_method :is_mobile_view?
39
+ base.helper_method :is_device?, :is_handset?, :is_brand?, :is_model?, :is_os?, :is_engine?, :is_browser?
40
+ end
41
+
42
+ # Determines the request format based on whether the device is mobile or if
43
+ # the user has opted to use either the 'Standard' view or 'Mobile' view.
44
+
45
+ def set_mobile_format
46
+ if is_mobile_device?
47
+ request.format = session[:mobile_view] == false ? :html : :mobile
48
+ session[:mobile_view] = true if session[:mobile_view].nil?
49
+ end
50
+ end
51
+
52
+ # Returns either true or false depending on whether or not the format of the
53
+ # request is either :mobile or not.
54
+
55
+ def is_mobile_view?
56
+ request.format.to_sym == :mobile
57
+ end
58
+
59
+ # Returns either true or false depending on whether or not the user agent of
60
+ # the device making the request is matched to a device in our regex.
61
+
62
+ def is_mobile_device?
63
+ Handset.is_mobile? request.user_agent
64
+ end
65
+
66
+ # Returns either true or false depending on whether or not the user agent of
67
+ # the device making the request is matched to a device in our regex.
68
+
69
+ def is_mobile_browser?
70
+ Handset.is_mobile? request.user_agent
71
+ end
72
+
73
+ # Returns either true or false depending on whether or not the user agent of
74
+ # the device making the request is matched to a device in our regex.
75
+
76
+ def is_desktop_browser?
77
+ !Handset.is_mobile? request.user_agent
78
+ end
79
+
80
+ # Returns either true or false depending on whether or not the user agent of
81
+ # the device making the request is matched to a bot in our regex.
82
+
83
+ def is_bot?
84
+ Bot.is_bot? request.user_agent
85
+ end
86
+
87
+ # Can check for a specific user agent
88
+ # e.g., is_device?('iphone') or is_device?('mobileexplorer')
89
+
90
+ def is_device? type
91
+ request.user_agent.to_s.downcase.include?(type.to_s.downcase)
92
+ end
93
+
94
+ # Can check for a specific user agent
95
+ # e.g., is_device?('iphone') or is_device?('mobileexplorer')
96
+
97
+ def is_handset? type
98
+ Handset.is_handset? request.user_agent, type
99
+ end
100
+
101
+ # Can check for a specific user agent
102
+ # e.g., is_device?('iphone') or is_device?('mobileexplorer')
103
+
104
+ def is_brand? brand
105
+ Handset.is_brand? request.user_agent, brand
106
+ end
107
+
108
+ # Can check for a specific user agent
109
+ # e.g., is_device?('iphone') or is_device?('mobileexplorer')
110
+
111
+ def is_model? model
112
+ Handset.is_model? request.user_agent, model
113
+ end
114
+
115
+ # Can check for a specific user agent
116
+ # e.g., is_device?('iphone') or is_device?('mobileexplorer')
117
+
118
+ def is_os? os
119
+ Os.is_os? request.user_agent, os
120
+ end
121
+
122
+ # Can check for a specific user agent
123
+ # e.g., is_device?('iphone') or is_device?('mobileexplorer')
124
+
125
+ def is_engine? engine
126
+ Engine.is_engine? request.user_agent, engine
127
+ end
128
+
129
+ # Can check for a specific user agent
130
+ # e.g., is_device?('iphone') or is_device?('mobileexplorer')
131
+
132
+ def is_browser? browser
133
+ Browser.is_browser? request.user_agent, browser
134
+ end
135
+
136
+ end
137
+
138
+ ActionController::Base.send(:include, ActiveDevice)
@@ -36,6 +36,20 @@ module ActiveDevice
36
36
  :moz_platform => /#{ID[:moz_platform] * "|"}/,
37
37
  :bot => /#{ID[:bot] * "|"}/ }
38
38
 
39
+ def is_bot?
40
+ self.is_bot? user_agent
41
+ end
42
+
43
+ ##
44
+ # User agent brand symbol.
45
+ def self.is_bot? user_agent
46
+ case user_agent
47
+ when /bot/i ; true
48
+ when /Spider/i ; true
49
+ else ; false
50
+ end
51
+ end
52
+
39
53
  def robot?(user_agent)
40
54
  !browser?(user_agent)
41
55
  end
@@ -73,4 +87,5 @@ module ActiveDevice
73
87
  end
74
88
  end
75
89
  end
90
+
76
91
  end
@@ -234,4 +234,5 @@ class Brand
234
234
  else ; :UnknownMobile
235
235
  end
236
236
  end
237
+
237
238
  end
@@ -0,0 +1,76 @@
1
+ # To change this template, choose Tools | Templates
2
+ # and open the template in the editor.
3
+
4
+ class Browser
5
+ ##
6
+ # User agent string.
7
+
8
+ attr_reader :user_agent
9
+
10
+ ##
11
+ # Initialize with user agent _string_.
12
+
13
+ def initialize user_agent
14
+ @user_agent = user_agent.strip
15
+ end
16
+
17
+ ##
18
+ # Return name for user agent _user_agent_.
19
+
20
+ def self.name user_agent
21
+ case user_agent
22
+ when /Konqueror/i ; :Konqueror
23
+ when /Safari/i ; :Safari
24
+ when /MSIE/i ; :IE
25
+ when /Chrome/i ; :Chrome
26
+ when /Android/i ; :Android
27
+ when /Opera Mini/i ; :'Opera Mini'
28
+ when /Opera Mobi/i ; :'Opera Mobile'
29
+ when /Opera/i ; :Opera
30
+ when /playstation 3/i ; :PS3
31
+ when /playstation portable/i ; :PSP
32
+ when /SymbianOS/i ; :SymbianOS
33
+ when /Symbian/i ; :Symbian
34
+ when /iPhone/i ; :iPhone
35
+ when /Firefox/i ; :Firefox
36
+ when /Mozilla/i ; :Mozilla
37
+ else ; :Unknown
38
+ end
39
+ end
40
+
41
+ ##
42
+ # Return version for user agent _user_agent_.
43
+
44
+ def self.version user_agent
45
+ name = name user_agent
46
+ case name
47
+ when :Chrome ; $1 if user_agent =~ /chrome\/([\d\w\.\-]+)/i
48
+ when :Safari ;
49
+ if user_agent =~ /version\/([\d\w\.\-]+)/i
50
+ $1
51
+ elsif user_agent =~ /Safari([0-9\.\-]+)/i
52
+ $1
53
+ else user_agent =~ /Safari\/([0-9\.\-]+)/i
54
+ $1
55
+ end
56
+ when :PS3 ; $1 if user_agent =~ /([\d\w\.\-]+)\)\s*$/i
57
+ when :PSP ; $1 if user_agent =~ /([\d\w\.\-]+)\)?\s*$/i
58
+ # when :SymbianOS ; $1 if user_agent =~ /\/([\d\w\.\-]+)/i #\)?\s*$
59
+ # when :Symbian ; $1 if user_agent =~ /\ ([0-9\d\w\.\-]+)\)?\s*$/i
60
+ # when :iPhone ; $1 if user_agent =~ /([\d\w\.\-]+)\)?\s*$/i
61
+ #when name[/safari([0-9\-\.\_]+)/i] ; $1 if name =~ /([0-9\-\.\_]+)/i
62
+ else $1 if user_agent =~ /#{name.to_s}[\/ ]([\d\w\.\-]+)/i
63
+ end
64
+ end
65
+
66
+ ##
67
+ # Is _string_. Device
68
+
69
+ def self.is_browser? user_agent, browser
70
+ browser_sym = name user_agent
71
+ browser_sym = browser_sym.to_s.downcase
72
+ browser = browser.to_s.downcase
73
+ browser_sym.include? browser
74
+ end
75
+
76
+ end
@@ -0,0 +1,74 @@
1
+ class Engine
2
+
3
+ ##
4
+ # User agent string.
5
+
6
+ attr_reader :user_agent
7
+
8
+ ##
9
+ # Initialize with user agent _string_.
10
+
11
+ def initialize user_agent
12
+ @user_agent = user_agent.strip
13
+ end
14
+
15
+
16
+ ##
17
+ # Return engine symbol for user agent _user_agent_.
18
+
19
+ def self.engine user_agent
20
+ case user_agent
21
+ when /Webkit/i ; :Webkit
22
+ when /Khtml/i ; :khtml
23
+ when /Konqueror/i ; :Konqueror
24
+ when /Presto/i ; :Presto
25
+ when /Trident/i ; :Trident
26
+ when /Gecko/i ; :Gecko
27
+ when /MSIE/i ; :MSIE
28
+ when /Chrome/i ; :Chrome
29
+ when /Darwin/i ; :Darwin
30
+ when /NetFront/i ; :NetFront
31
+ when /UP.Browser/i; :'UP.Browser'
32
+ else :Unknown
33
+ end
34
+ end
35
+
36
+ ##
37
+ # Return engine version for user agent _user_agent_.
38
+
39
+ def self.engine_version user_agent
40
+ engine = engine user_agent
41
+ case engine
42
+ when :Webkit;
43
+ if user_agent =~ /#{engine}[\/ ]([\d\w\.\-]+)/i
44
+ $1
45
+ elsif user_agent =~ /#{engine}([0-9\.\-]+)/i
46
+ $1
47
+ end
48
+ when :NetFront;
49
+ if user_agent =~ /#{engine}[\/ ]([\d\w\.\-]+)/i
50
+ $1
51
+ elsif user_agent =~ /#{engine}([0-9\.\-]+)/i
52
+ $1
53
+ end
54
+ when :'UP.Browser';
55
+ if user_agent =~ /#{engine}[\/ ]([\d\w\.\-]+)/i
56
+ $1
57
+ elsif user_agent =~ /#{engine}([0-9\.\-]+)/i
58
+ $1
59
+ end
60
+ else $1 if user_agent =~ /#{engine user_agent}[\/ ]([\d\w\.\-]+)/i
61
+ end
62
+ end
63
+
64
+ ##
65
+ # Is _string_. Device
66
+
67
+ def self.is_engine? user_agent, engine
68
+ engine_sym = engine user_agent
69
+ engine_sym = engine_sym.to_s.downcase
70
+ engine = engine.to_s.downcase
71
+ engine_sym.include? engine
72
+ end
73
+
74
+ end
@@ -37,4 +37,24 @@ class Handset
37
37
  model_sym.include? model
38
38
  end
39
39
 
40
+ ##
41
+ # Is _string_. Device
42
+
43
+ def self.is_model? user_agent, model
44
+ model_sym = Model.brand_model user_agent
45
+ model_sym = model_sym.to_s.downcase
46
+ model = model.to_s.downcase
47
+ model_sym.include? model
48
+ end
49
+
50
+ ##
51
+ # Is _string_. Device
52
+
53
+ def self.is_brand? user_agent, brand
54
+ brand_sym = Brand.mobile_brand user_agent
55
+ brand_sym = brand_sym.to_s.downcase
56
+ brand = brand.to_s.downcase
57
+ brand_sym.include? brand
58
+ end
59
+
40
60
  end
@@ -0,0 +1,89 @@
1
+ class Os
2
+ ##
3
+ # User agent string.
4
+
5
+ attr_reader :user_agent
6
+
7
+ ##
8
+ # Initialize with user agent _string_.
9
+
10
+ def initialize user_agent
11
+ @user_agent = user_agent.strip
12
+ end
13
+
14
+ ##
15
+ # Return the os for user agent _user_agent_.
16
+
17
+ def self.os user_agent
18
+ case user_agent
19
+ when /windows ce/i ; :'Windows CE'
20
+ when /windows nt 6\.0/i ; :'Windows Vista'
21
+ when /windows nt 6\.\d+/i ; :'Windows 7'
22
+ when /windows nt 5\.2/i ; :'Windows 2003'
23
+ when /windows nt 5\.1/i ; :'Windows XP'
24
+ when /windows nt 5\.0/i ; :'Windows 2000'
25
+ when /windows nt 4\.0/i ; :Windows
26
+ when /os x (\d+)[._](\d+)/i ; :'Mac OS'
27
+ when /Mac/i ; :'Mac OS'
28
+ when /linux/i ; :Linux
29
+ when /wii/i ; :Wii
30
+ when /playstation 3/i ; :Playstation
31
+ when /playstation portable/i ; :Playstation
32
+ when /SymbianOS/i ; :SymbianOS
33
+ when /Symbian/i ; :Symbian
34
+ when /Palm/i ; :Palm
35
+ when /iPhone/i ; :iPhone
36
+ else ; :Unknown
37
+ end
38
+ end
39
+
40
+ ##
41
+ # Return version for user agent _user_agent_.
42
+
43
+ def self.os_version user_agent
44
+ os = os(user_agent)
45
+ case os
46
+ when :Chrome ; $1 if user_agent =~ /chrome\/([\d\w\.\-]+)/i
47
+ when :Safari;
48
+ if user_agent =~ /version\/([\d\w\.\-]+)/i
49
+ $1
50
+ elsif user_agent =~ /Safari([\d0-9\.\-]+)/i
51
+ $1
52
+ elsif user_agent =~ /Safari\/([\d0-9\.\-]+)/i
53
+ $1
54
+ end
55
+ when :PS3 ; $1 if user_agent =~ /([\d\w\.\-]+)\)\s*$/i
56
+ when :PSP ; $1 if user_agent =~ /([\d\w\.\-]+)\)?\s*$/i
57
+ when :SymbianOS;
58
+ if user_agent =~ /symbianos\/([\d\w\.\-]+)/i
59
+ $1
60
+ elsif user_agent =~ /SymbianOS([\d0-9\.\-]+)/i
61
+ $1
62
+ end
63
+ when :Symbian ; $1 if user_agent =~ /\ ([0-9\d\w\.\-]+)\)?\s*$/i
64
+ when :iPhone ; $1 if user_agent =~ /OS\ ([0-9\.\-\_]+)/i
65
+ when :IE ; $1 if user_agent =~ /IEMobile[\/ ]([\d\w\.\-]+)/i
66
+ when :'Mac OS' ; "#{$1}.#{$2}" if user_agent =~ /os x (\d+)[._](\d+)/i
67
+ else $1 if user_agent =~ /#{os}[\/ ]([\d\w\.\-]+)/i
68
+ end
69
+ end
70
+
71
+ def self.os_series user_agent
72
+ case os(user_agent)
73
+ when :SymbianOS ; :"#{user_agent[/series[\w\-\.\/]*/i]}" if user_agent =~ /series[\w\-\.\/]*/i
74
+ when :Symbian ; $1 if user_agent =~ /\ ([0-9\d\w\.\-]+)\)?\s*$/i
75
+ else $1 if user_agent =~ /[\/ ]([\d\w\.\-]+)/i
76
+ end
77
+ end
78
+
79
+ ##
80
+ # Is _string_. Device
81
+
82
+ def self.is_os? user_agent, os
83
+ os_sym = os user_agent
84
+ os_sym = os_sym.to_s.downcase
85
+ os = os.to_s.downcase
86
+ os_sym.include? os
87
+ end
88
+
89
+ end
@@ -10,88 +10,6 @@ class UserAgent
10
10
  def initialize user_agent
11
11
  @user_agent = user_agent.strip
12
12
  end
13
-
14
- #--
15
- # Instance methods
16
- #++
17
-
18
- ##
19
- # User agent name symbol.
20
-
21
- def name
22
- AgentSpec.name_for_user_agent user_agent
23
- end
24
-
25
- ##
26
- # User agent version.
27
-
28
- def version
29
- AgentSpec.version_for_user_agent user_agent
30
- end
31
-
32
- ##
33
- # User agent engine symbol.
34
-
35
- def engine
36
- AgentSpec.engine_for_user_agent user_agent
37
- end
38
-
39
- ##
40
- # User agent engine version user_agent.
41
-
42
- def engine_version
43
- AgentSpec.engine_version_for_user_agent user_agent
44
- end
45
-
46
- ##
47
- # User agent os symbol.
48
-
49
- def os
50
- AgentSpec.os_for_user_agent user_agent
51
- end
52
-
53
- ##
54
- # User agent engine version user_agent.
55
-
56
- def os_version
57
- AgentSpec.os_version_for_user_agent user_agent
58
- end
59
-
60
- ##
61
- # User agent engine version user_agent.
62
-
63
- def os_series
64
- AgentSpec.os_series_for_user_agent user_agent
65
- end
66
-
67
- ##
68
- # User agent model symbol.
69
-
70
- def model
71
- Model.brand_model user_agent
72
- end
73
-
74
- ##
75
- # User agent model Reselution symbol.
76
-
77
- def model_reselution
78
- Model.model_reselution user_agent
79
- end
80
-
81
- ##
82
- # User agent brand symbol.
83
-
84
- def brand
85
- Brand.mobile_brand user_agent
86
- end
87
-
88
- def is_mobile?
89
- Handset.is_mobile? user_agent
90
- end
91
-
92
- def is_handset? model
93
- Handset.is_handset? user_agent, model
94
- end
95
13
 
96
14
  ##
97
15
  # User agent user_agent.
@@ -113,5 +31,14 @@ class UserAgent
113
31
  def == other
114
32
  user_agent == other.user_agent
115
33
  end
34
+
35
+ @agents = []
36
+
37
+ ##
38
+ # Map agent _name_ to _options_.
39
+
40
+ def self.map name, options = {}
41
+ @agents << [name, options]
42
+ end
116
43
 
117
44
  end
@@ -0,0 +1,108 @@
1
+ #--
2
+ # Copyright (c) 2009 Shenouda Bertel <sbertel@mobithought.com>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ require 'active_device/user_agent'
25
+ require 'active_device/brand'
26
+ require 'active_device/model'
27
+ require 'active_device/handset'
28
+ require 'active_device/bot'
29
+ require 'active_device/engine'
30
+ require 'active_device/browser'
31
+ require 'active_device/os'
32
+
33
+ module ActiveDevice
34
+
35
+ module Helper
36
+
37
+ ##
38
+ # User agent name symbol.
39
+
40
+ def agent_name
41
+ Browser.name request.user_agent
42
+ end
43
+
44
+ ##
45
+ # User agent version.
46
+
47
+ def agent_version
48
+ Browser.version request.user_agent
49
+ end
50
+
51
+ ##
52
+ # User agent engine symbol.
53
+
54
+ def agent_engine
55
+ Engine.engine request.user_agent
56
+ end
57
+
58
+ ##
59
+ # User agent engine version user_agent.
60
+
61
+ def agent_engine_version
62
+ Engine.engine_version request.user_agent
63
+ end
64
+
65
+ ##
66
+ # User agent os symbol.
67
+
68
+ def agent_os
69
+ Os.os request.user_agent
70
+ end
71
+
72
+ ##
73
+ # User agent engine version user_agent.
74
+
75
+ def agent_os_version
76
+ Os.os_version request.user_agent
77
+ end
78
+
79
+ ##
80
+ # User agent engine version user_agent.
81
+
82
+ def agent_os_series
83
+ Os.os_series request.user_agent
84
+ end
85
+
86
+ ##
87
+ # User agent model symbol.
88
+
89
+ def model
90
+ Model.brand_model request.user_agent
91
+ end
92
+
93
+ ##
94
+ # User agent model Reselution symbol.
95
+
96
+ def model_reselution
97
+ Model.model_reselution request.user_agent
98
+ end
99
+
100
+ ##
101
+ # User agent brand symbol.
102
+
103
+ def brand
104
+ Brand.mobile_brand request.user_agent
105
+ end
106
+
107
+ end
108
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'active_device'
2
+ require 'active_device_helper'
3
+
4
+ ActionView::Base.send :include, ActiveDevice::Helper
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :active_device do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class ActiveDeviceTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_device
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shenouda Bertel
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-11-20 00:00:00 +02:00
12
+ date: 2009-11-26 00:00:00 +02:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -20,29 +20,35 @@ executables: []
20
20
  extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
- - README.rdoc
23
+ - README
24
24
  files:
25
+ - install.rb
26
+ - Rakefile
27
+ - README
28
+ - MIT-LICENSE
29
+ - init.rb
30
+ - uninstall.rb
25
31
  - lib/active_device/model.rb
26
- - lib/active_device/agent_spec.rb
32
+ - lib/active_device/os.rb
33
+ - lib/active_device/engine.rb
27
34
  - lib/active_device/brand.rb
28
35
  - lib/active_device/bot.rb
29
36
  - lib/active_device/handset.rb
30
37
  - lib/active_device/user_agent.rb
31
- - lib/active_device_rails.rb
38
+ - lib/active_device/browser.rb
32
39
  - lib/active_device.rb
33
- - README.rdoc
40
+ - lib/active_device_helper.rb
41
+ - rails/init.rb
42
+ - tasks/active_device_tasks.rake
43
+ - test/active_device_test.rb
44
+ - test/test_helper.rb
34
45
  has_rdoc: true
35
- homepage: ""
46
+ homepage: http://mobithought.com/
36
47
  licenses: []
37
48
 
38
49
  post_install_message:
39
- rdoc_options:
40
- - --line-numbers
41
- - --inline-source
42
- - --title
43
- - User-agent
44
- - --main
45
- - README.rdoc
50
+ rdoc_options: []
51
+
46
52
  require_paths:
47
53
  - lib
48
54
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -63,6 +69,6 @@ rubyforge_project: active_device
63
69
  rubygems_version: 1.3.5
64
70
  signing_key:
65
71
  specification_version: 3
66
- summary: Device UserAgent Detector
72
+ summary: Mobile Device Detector
67
73
  test_files: []
68
74
 
data/README.rdoc DELETED
@@ -1,41 +0,0 @@
1
-
2
- = Active Device
3
-
4
- Handsets and Device Detector.
5
-
6
- == Example
7
-
8
- agent = Agent.new 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-us) AppleWebKit/531.9 (KHTML, like Gecko) Version/4.0.3 Safari/531.9'
9
- agent.name # => :Safari
10
- agent.version # => '4.0.3'
11
- agent.engine # => :webkit
12
- agent.os # => :'Windows Vista'
13
- agent.engine_version # => '531.9'
14
-
15
- == Supported Agents
16
-
17
-
18
- == License:
19
-
20
- (The MIT License)
21
-
22
- Copyright (c) 2009 Shenouda Bertel <sbertel@mobithought.com>
23
-
24
- Permission is hereby granted, free of charge, to any person obtaining
25
- a copy of this software and associated documentation files (the
26
- 'Software'), to deal in the Software without restriction, including
27
- without limitation the rights to use, copy, modify, merge, publish,
28
- distribute, sublicense, an d/or sell copies of the Software, and to
29
- permit persons to whom the Software is furnished to do so, subject to
30
- the following conditions:
31
-
32
- The above copyright notice and this permission notice shall be
33
- included in all copies or substantial portions of the Software.
34
-
35
- THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
36
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
37
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
38
- IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
39
- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
40
- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
41
- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,181 +0,0 @@
1
- class AgentSpec
2
-
3
- ##
4
- # User agent user_agent.
5
-
6
- attr_reader :user_agent
7
-
8
- ##
9
- # Initialize with user agent _user_agent_.
10
-
11
- def initialize user_agent
12
- @user_agent = user_agent.strip
13
- end
14
-
15
- ##
16
- # Return engine version for user agent _user_agent_.
17
-
18
- def self.engine_version_for_user_agent user_agent
19
- engine = engine_for_user_agent(user_agent)
20
- case engine
21
- when :Webkit;
22
- if user_agent =~ /#{engine}[\/ ]([\d\w\.\-]+)/i
23
- $1
24
- elsif user_agent =~ /#{engine}([0-9\.\-]+)/i
25
- $1
26
- end
27
- when :NetFront;
28
- if user_agent =~ /#{engine}[\/ ]([\d\w\.\-]+)/i
29
- $1
30
- elsif user_agent =~ /#{engine}([0-9\.\-]+)/i
31
- $1
32
- end
33
- when :'UP.Browser';
34
- if user_agent =~ /#{engine}[\/ ]([\d\w\.\-]+)/i
35
- $1
36
- elsif user_agent =~ /#{engine}([0-9\.\-]+)/i
37
- $1
38
- end
39
- else $1 if user_agent =~ /#{engine_for_user_agent(user_agent)}[\/ ]([\d\w\.\-]+)/i
40
- end
41
- end
42
-
43
- ##
44
- # Return version for user agent _user_agent_.
45
-
46
- def self.version_for_user_agent user_agent
47
- name = name_for_user_agent(user_agent)
48
- case name
49
- when :Chrome ; $1 if user_agent =~ /chrome\/([\d\w\.\-]+)/i
50
- when :Safari ;
51
- if user_agent =~ /version\/([\d\w\.\-]+)/i
52
- $1
53
- elsif user_agent =~ /Safari([0-9\.\-]+)/i
54
- $1
55
- else user_agent =~ /Safari\/([0-9\.\-]+)/i
56
- $1
57
- end
58
- when :PS3 ; $1 if user_agent =~ /([\d\w\.\-]+)\)\s*$/i
59
- when :PSP ; $1 if user_agent =~ /([\d\w\.\-]+)\)?\s*$/i
60
- # when :SymbianOS ; $1 if user_agent =~ /\/([\d\w\.\-]+)/i #\)?\s*$
61
- # when :Symbian ; $1 if user_agent =~ /\ ([0-9\d\w\.\-]+)\)?\s*$/i
62
- # when :iPhone ; $1 if user_agent =~ /([\d\w\.\-]+)\)?\s*$/i
63
- #when name[/safari([0-9\-\.\_]+)/i] ; $1 if name =~ /([0-9\-\.\_]+)/i
64
- else $1 if user_agent =~ /#{name.to_s}[\/ ]([\d\w\.\-]+)/i
65
- end
66
- end
67
-
68
- #when /safari([\d\w\.\- ]+)/i ; :"#{user_agent[/[\w\-\.\_]*/i]}"
69
- #when /#{name}([\d\w\.\- ]+)/i ; $1 if user_agent =~ /#{name}([\d\w\.\- ]+)/i
70
-
71
- ##
72
- # Return version for user agent _user_agent_.
73
-
74
- def self.os_version_for_user_agent user_agent
75
- os = os_for_user_agent(user_agent)
76
- case os
77
- when :Chrome ; $1 if user_agent =~ /chrome\/([\d\w\.\-]+)/i
78
- when :Safari;
79
- if user_agent =~ /version\/([\d\w\.\-]+)/i
80
- $1
81
- elsif user_agent =~ /Safari([\d0-9\.\-]+)/i
82
- $1
83
- elsif user_agent =~ /Safari\/([\d0-9\.\-]+)/i
84
- $1
85
- end
86
- when :PS3 ; $1 if user_agent =~ /([\d\w\.\-]+)\)\s*$/i
87
- when :PSP ; $1 if user_agent =~ /([\d\w\.\-]+)\)?\s*$/i
88
- when :SymbianOS;
89
- if user_agent =~ /symbianos\/([\d\w\.\-]+)/i
90
- $1
91
- elsif user_agent =~ /SymbianOS([\d0-9\.\-]+)/i
92
- $1
93
- end
94
- when :Symbian ; $1 if user_agent =~ /\ ([0-9\d\w\.\-]+)\)?\s*$/i
95
- when :iPhone ; $1 if user_agent =~ /OS\ ([0-9\.\-\_]+)/i
96
- when :IE ; $1 if user_agent =~ /IEMobile[\/ ]([\d\w\.\-]+)/i
97
- else $1 if user_agent =~ /#{os}[\/ ]([\d\w\.\-]+)/i
98
- end
99
- end
100
-
101
- def self.os_series_for_user_agent user_agent
102
- case os_for_user_agent(user_agent)
103
- when :SymbianOS ; :"#{user_agent[/series[\w\-\.\/]*/i]}" if user_agent =~ /series[\w\-\.\/]*/i
104
- when :Symbian ; $1 if user_agent =~ /\ ([0-9\d\w\.\-]+)\)?\s*$/i
105
- else $1 if user_agent =~ /[\/ ]([\d\w\.\-]+)/i
106
- end
107
- end
108
-
109
- ##
110
- # Return engine symbol for user agent _user_agent_.
111
-
112
- def self.engine_for_user_agent user_agent
113
- case user_agent
114
- when /webkit/i ; :Webkit
115
- when /khtml/i ; :khtml
116
- when /konqueror/i ; :Konqueror
117
- when /chrome/i ; :Chrome
118
- when /presto/i ; :Presto
119
- when /gecko/i ; :Gecko
120
- when /msie/i ; :MSIE
121
- when /NetFront/i ; :NetFront
122
- when /UP.Browser/i; :'UP.Browser'
123
- else :Unknown
124
- end
125
- end
126
-
127
- ##
128
- # Return the os for user agent _user_agent_.
129
-
130
- def self.os_for_user_agent user_agent
131
- case user_agent
132
- when /windows ce/i ; :'Windows CE'
133
- when /windows nt 6\.0/i ; :'Windows Vista'
134
- when /windows nt 6\.\d+/i ; :'Windows 7'
135
- when /windows nt 5\.2/i ; :'Windows 2003'
136
- when /windows nt 5\.1/i ; :'Windows XP'
137
- when /windows nt 5\.0/i ; :'Windows 2000'
138
- when /windows nt 4\.0/i ; :Windows
139
- when /os x (\d+)[._](\d+)/i ; :"OS X #{$1}.#{$2}"
140
- when /linux/i ; :Linux
141
- when /wii/i ; :Wii
142
- when /playstation 3/i ; :Playstation
143
- when /playstation portable/i ; :Playstation
144
- when /SymbianOS/i ; :SymbianOS
145
- when /Symbian/i ; :Symbian
146
- when /Palm/i ; :Palm
147
- when /iPhone/i ; :iPhone
148
- else ; :Unknown
149
- end
150
- end
151
-
152
-
153
-
154
- ##
155
- # Return name for user agent _user_agent_.
156
-
157
- def self.name_for_user_agent user_agent
158
- case user_agent
159
- when /konqueror/i ; :Konqueror
160
- when /chrome/i ; :Chrome
161
- when /safari/i ; :Safari
162
- when /msie/i ; :IE
163
- when /opera/i ; :Opera
164
- when /playstation 3/i ; :PS3
165
- when /playstation portable/i ; :PSP
166
- when /SymbianOS/i ; :SymbianOS
167
- when /Symbian/i ; :Symbian
168
- when /iPhone/i ; :iPhone
169
- else ; :Unknown
170
- end
171
- end
172
-
173
- @agents = []
174
-
175
- ##
176
- # Map agent _name_ to _options_.
177
-
178
- def self.map name, options = {}
179
- @agents << [name, options]
180
- end
181
- end
@@ -1,93 +0,0 @@
1
- module ActionController
2
- module ActiveDevice
3
-
4
- def self.included(base)
5
- base.extend(ClassMethods)
6
- end
7
-
8
- module ClassMethods
9
-
10
- # Add this to one of your controllers to use ActiveDevice.
11
- #
12
- # class ApplicationController < ActionController::Base
13
- # has_active_handset
14
- # end
15
- #
16
- # You can also force mobile mode by passing in 'true'
17
- #
18
- # class ApplicationController < ActionController::Base
19
- # has_active_handset(true)
20
- # end
21
-
22
- def has_active_handset(test_mode = false)
23
- include ActionController::ActiveDevice::InstanceMethods
24
-
25
- if test_mode
26
- before_filter :force_mobile_format
27
- else
28
- before_filter :set_mobile_format
29
- end
30
-
31
- helper_method :is_mobile_device?
32
- helper_method :in_mobile_view?
33
- helper_method :is_device?
34
- end
35
-
36
- def is_mobile_device?
37
- @@is_mobile_device
38
- end
39
-
40
- def in_mobile_view?
41
- @@in_mobile_view
42
- end
43
-
44
- def is_device?(type)
45
- @@is_device
46
- end
47
- end
48
-
49
- module InstanceMethods
50
-
51
- # Forces the request format to be :mobile
52
-
53
- def force_mobile_format
54
- request.format = :mobile
55
- session[:mobile_view] = true if session[:mobile_view].nil?
56
- end
57
-
58
- # Determines the request format based on whether the device is mobile or if
59
- # the user has opted to use either the 'Standard' view or 'Mobile' view.
60
-
61
- def set_mobile_format
62
- if is_mobile_device?
63
- request.format = session[:mobile_view] == false ? :html : :mobile
64
- session[:mobile_view] = true if session[:mobile_view].nil?
65
- end
66
- end
67
-
68
- # Returns either true or false depending on whether or not the format of the
69
- # request is either :mobile or not.
70
-
71
- def in_mobile_view?
72
- request.format.to_sym == :mobile
73
- end
74
-
75
- # Returns either true or false depending on whether or not the user agent of
76
- # the device making the request is matched to a device in our regex.
77
-
78
- def is_mobile_device?
79
- Handset.is_mobile? request.user_agent
80
- end
81
-
82
- # Can check for a specific user agent
83
- # e.g., is_device?('iphone') or is_device?('mobileexplorer')
84
-
85
- def is_device?(type)
86
- request.user_agent.to_s.downcase.include?(type.to_s.downcase)
87
- end
88
- end
89
-
90
- end
91
- end
92
-
93
- ActionController::Base.send(:include, ActionController::ActiveDevice)