Class: Humr::Handler::UserAgent

Inherits:
Humr::Handler show all
Defined in:
lib/humr/handler/user_agent.rb

Constant Summary collapse

UA_LIKE_PATTERN =
%r<^(?:[\w-]+(?:/[\w.-]+)?(?:\s*\([^\)]+\))?\s*)+$>

Instance Method Summary collapse

Methods inherited from Humr::Handler

[], #name, register

Instance Method Details

#replace(s, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/humr/handler/user_agent.rb', line 14

def replace(s, &block)
  return unless UA_LIKE_PATTERN.match(s)

  ua = ::UserAgent.parse(s)

  return unless ua.version

  readable = if ua.bot?
    ua.os
  else
    '%s %s%s' % [
      ua.browser,
      rough_version(ua.version.to_s),
      if ua.os and not ua.os.empty?
        " (#{rough_version(ua.os)})"
      end
    ]
  end

  readable.sub(/.*/, &block) if readable
end

#rough_version(s) ⇒ Object



8
9
10
# File 'lib/humr/handler/user_agent.rb', line 8

def rough_version(s)
  s.sub(/(\d+\.\d+)(?:\.\d+)*/, '\1').sub(/\.0$/, '')
end