Class: AgentOrange::Device

Inherits:
Base
  • Object
show all
Defined in:
lib/agent_orange/device.rb

Constant Summary collapse

DEVICES =
{
  :computer => 'windows|macintosh|x11|linux',
  :mobile => 'ipod|ipad|iphone|palm|android|opera mini|hiptop|windows ce|smartphone|mobile|treo|psp'
}
BOTS =
{
  :bot => 'alexa|bot|crawl(er|ing)|facebookexternalhit|feedburner|google web preview|nagios|postrank|pingdom|slurp|spider|yahoo!|yandex'
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#analysis, #debug_content, #debug_raw_content, #determine_type, #initialize, #parse_comment, #parse_user_agent_string_into_groups

Constructor Details

This class inherits a constructor from AgentOrange::Base

Instance Attribute Details

#botBoolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/agent_orange/device.rb', line 19

def bot
  @bot
end

#engineAgentOrange::Engine

Returns:



28
29
30
# File 'lib/agent_orange/device.rb', line 28

def engine
  @engine
end

#nameString

Returns:

  • (String)


13
14
15
# File 'lib/agent_orange/device.rb', line 13

def name
  @name
end

#operating_systemAgentOrange::OperatingSystem



25
26
27
# File 'lib/agent_orange/device.rb', line 25

def operating_system
  @operating_system
end

#platformAgentOrange::Platform



22
23
24
# File 'lib/agent_orange/device.rb', line 22

def platform
  @platform
end

#typeSymbol

Returns one of the keys from DEVICES.

Returns:

  • (Symbol)

    one of the keys from DEVICES



10
11
12
# File 'lib/agent_orange/device.rb', line 10

def type
  @type
end

#versionAgentOrange::Version



16
17
18
# File 'lib/agent_orange/device.rb', line 16

def version
  @version
end

Instance Method Details

#is_bot?(name = nil) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/agent_orange/device.rb', line 102

def is_bot?(name=nil)
  if name
    case name
    when String
      return name.downcase.include?(name.downcase)
    when Symbol
      return name.downcase.include?(name.to_s.downcase)
    end
  else
    bot
  end
end

#is_computer?(name = nil) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/agent_orange/device.rb', line 74

def is_computer?(name=nil)
  if name
    case name
    when String
      return platform.name.downcase.include?(name.downcase)
    when Symbol
      return platform.name.downcase.include?(name.to_s.downcase)
    end
  else
    (type == :computer)
  end
end

#is_mobile?(name = nil) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/agent_orange/device.rb', line 88

def is_mobile?(name=nil)
  if !name.nil? && !platform.name.nil?
    case name
    when String
      return platform.name.downcase.include?(name.downcase) || platform.version.downcase.include?(name.downcase)
    when Symbol
      return platform.name.downcase.include?(name.to_s.downcase) || platform.version.to_s.downcase.include?(name.to_s.downcase)
    end
  else
    (type == :mobile)
  end
end

#parse(user_agent) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/agent_orange/device.rb', line 39

def parse(user_agent)
  AgentOrange.debug "DEVICE PARSING", 2

  groups = parse_user_agent_string_into_groups(user_agent)
  groups.each_with_index do |content,i|
    devices_and_bots = DEVICES.merge(BOTS)
    if content[:comment] =~ /(#{devices_and_bots.collect{|cat,regex| regex}.join(')|(')})/i
      # Matched group against name
      self.populate(content)
    end
  end

  analysis

  self.platform         = AgentOrange::Platform.new(user_agent)
  self.operating_system = AgentOrange::OperatingSystem.new(user_agent)
  self.engine           = AgentOrange::Engine.new(user_agent)
end

#populate(content = {}) ⇒ Device

Returns:



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/agent_orange/device.rb', line 59

def populate(content={})
  debug_raw_content(content)
  AgentOrange.debug "", 2

  self.type = determine_type(DEVICES, content[:comment])
  self.bot  = determine_type(BOTS, content[:comment]) == :bot
  if (bot && type == "other")
    self.type = :bot # backwards compatability
  end
  self.name = type.to_s.capitalize
  self.version = nil
  self
end

#to_sString

Returns:

  • (String)


116
117
118
# File 'lib/agent_orange/device.rb', line 116

def to_s
  [name, version].compact.join(' ')
end