Class: Os

Inherits:
Object
  • Object
show all
Defined in:
lib/active_device/os.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_agent) ⇒ Os

Initialize with user agent string.



10
11
12
# File 'lib/active_device/os.rb', line 10

def initialize user_agent
  @user_agent = user_agent.strip
end

Instance Attribute Details

#user_agentObject (readonly)

User agent string.



5
6
7
# File 'lib/active_device/os.rb', line 5

def user_agent
  @user_agent
end

Class Method Details

.is_os?(user_agent, os) ⇒ Boolean

Is string. Device

Returns:

  • (Boolean)


85
86
87
88
89
90
# File 'lib/active_device/os.rb', line 85

def self.is_os? user_agent, os
  os_sym = os user_agent
  os_sym = os_sym.to_s.downcase
  os = os.to_s.downcase
  os_sym.include? os
end

.os(user_agent) ⇒ Object

Return the os for user agent user_agent.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_device/os.rb', line 17

def self.os user_agent
  case user_agent
  when /Android/i              ; :Android
  when /windows ce/i           ; :'Windows CE'
  when /windows nt 6\.0/i      ; :'Windows Vista'
  when /windows nt 6\.\d+/i    ; :'Windows 7'
  when /windows nt 5\.2/i      ; :'Windows 2003'
  when /windows nt 5\.1/i      ; :'Windows XP'
  when /windows nt 5\.0/i      ; :'Windows 2000'
  when /windows nt 4\.0/i      ; :Windows
  when /os x (\d+)[._](\d+)/i  ; :'Mac OS'
  when /Mac/i                  ; :'Mac OS'
  when /linux/i                ; :Linux
  when /wii/i                  ; :Wii
  when /playstation 3/i        ; :Playstation
  when /playstation portable/i ; :Playstation
  when /SymbianOS/i            ; :SymbianOS
  when /Symbian/i              ; :Symbian
  when /Palm/i                 ; :Palm
  when /PalmOS/i               ; :PalmOS
  when /webOS/i                ; :webOS
  when /iPhone/i               ; :iPhone
  else                         ; :Unknown
  end
end

.os_series(user_agent) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/active_device/os.rb', line 74

def self.os_series user_agent
  case os(user_agent)
  when :SymbianOS  ; :"#{user_agent[/series[\w\-\.\/]*/i]}" if user_agent =~ /series[\w\-\.\/]*/i
  when :Symbian    ; $1 if user_agent =~ /\ ([0-9\d\w\.\-]+)\)?\s*$/i
  else           $1 if user_agent =~ /[\/ ]([\d\w\.\-]+)/i
  end
end

.os_version(user_agent) ⇒ Object

Return version for user agent user_agent.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/active_device/os.rb', line 46

def self.os_version user_agent
  os = os(user_agent)
  case os
  when :Chrome     ; $1 if user_agent =~ /chrome\/([\d\w\.\-]+)/i
  when :Safari;
    if user_agent =~ /version\/([\d\w\.\-]+)/i
      $1
    elsif user_agent =~ /Safari([\d0-9\.\-]+)/i
      $1
    elsif user_agent =~ /Safari\/([\d0-9\.\-]+)/i
      $1
    end
  when :PS3        ; $1 if user_agent =~ /([\d\w\.\-]+)\)\s*$/i
  when :PSP        ; $1 if user_agent =~ /([\d\w\.\-]+)\)?\s*$/i
  when :SymbianOS;
    if user_agent =~ /symbianos\/([\d\w\.\-]+)/i
      $1
    elsif user_agent =~ /SymbianOS([\d0-9\.\-]+)/i
      $1
    end
  when :Symbian    ; $1 if user_agent =~ /\ ([0-9\d\w\.\-]+)\)?\s*$/i
  when :iPhone     ; $1 if user_agent =~ /OS\ ([0-9\.\-\_]+)/i
  when :IE         ; $1 if user_agent =~ /IEMobile[\/ ]([\d\w\.\-]+)/i
  when :'Mac OS'   ; "#{$1}.#{$2}" if  user_agent =~ /os x (\d+)[._](\d+)/i
  else           $1 if user_agent =~ /#{os}[\/ ]([\d\w\.\-]+)/i
  end
end