Class: AgentOrange::UserAgent

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_agent_string) ⇒ UserAgent

Returns a new instance of UserAgent.

Parameters:

  • user_agent_string (String)


17
18
19
# File 'lib/agent_orange/user_agent.rb', line 17

def initialize(user_agent_string)
  parse(user_agent_string)
end

Instance Attribute Details

#deviceAgentOrange::Device

Returns:



14
15
16
# File 'lib/agent_orange/user_agent.rb', line 14

def device
  @device
end

#user_agent_stringString

Returns:

  • (String)


9
10
11
# File 'lib/agent_orange/user_agent.rb', line 9

def user_agent_string
  @user_agent_string
end

#user_languageObject

Returns the value of attribute user_language.



11
12
13
# File 'lib/agent_orange/user_agent.rb', line 11

def user_language
  @user_language
end

Instance Method Details

#is_bot?(type = nil) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/agent_orange/user_agent.rb', line 45

def is_bot?(type=nil)
  device.is_bot?(type)
end

#is_computer?(type = nil) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/agent_orange/user_agent.rb', line 35

def is_computer?(type=nil)
  device.is_computer?(type)
end

#is_mobile?(type = nil) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/agent_orange/user_agent.rb', line 40

def is_mobile?(type=nil)
  device.is_mobile?(type)
end

#parse(user_agent) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/agent_orange/user_agent.rb', line 21

def parse(user_agent)
  self.user_agent_string = user_agent
  self.device = AgentOrange::Device.new(user_agent_string)

  AgentOrange.debug "Device   = #{device}"
  AgentOrange.debug "Platform = #{device.platform}"
  AgentOrange.debug "OS       = #{device.operating_system}"
  AgentOrange.debug "Engine   = #{device.engine}"
  AgentOrange.debug "Browser  = #{device.engine.browser}"

  summary
end

#summaryObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/agent_orange/user_agent.rb', line 69

def summary
  AgentOrange.debug
  AgentOrange.debug "SUMMARY"
  AgentOrange.debug "  Is computer? #{is_computer?}"
  if is_computer?
    AgentOrange.debug "    Is a Mac? #{is_computer? :mac}"
    AgentOrange.debug "    Is a PC? #{is_computer? :pc}"
  end
  AgentOrange.debug "  Is mobile? #{is_mobile?}"
  if is_mobile?
    AgentOrange.debug "    Is an iPhone? #{is_mobile? :iphone}"
    AgentOrange.debug "    Is an iPad? #{is_mobile? :ipad}"
    AgentOrange.debug "    Is an iPod? #{is_mobile? :ipod}"
    AgentOrange.debug "    Is an Android? #{is_mobile? :android}"
  end
  AgentOrange.debug "  Is bot? #{is_bot?}"
  AgentOrange.debug
end

#to_human_stringString

Returns:

  • (String)


61
62
63
64
65
66
67
# File 'lib/agent_orange/user_agent.rb', line 61

def to_human_string
  if device && device.engine && device.engine.browser
    "User has a #{device} running #{device.engine.browser} (which is based on #{device.engine})."
  else
    "User has some kind of device that I've never seen."
  end
end

#to_sString

Returns:

  • (String)


50
51
52
53
54
55
56
57
58
# File 'lib/agent_orange/user_agent.rb', line 50

def to_s
  [
    device,
    device.platform,
    device.operating_system,
    device.engine,
    device.engine.browser
  ].compact.join(", ")
end