Class: Browscapper::UserAgent

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/browscapper/user_agent.rb

Constant Summary collapse

ACCESSORS =
[
  :parent, :user_agent, :comment, :browser, :version, :major_version,
  :minor_version, :platform, :platform_version, :alpha, :beta, :win16, :win32, :win64,
  :frames, :iframes, :tables, :cookies, :background_sounds, :vbscript,
  :javascript, :java_applets, :activex_controls, :banned, :mobile_device, :tablet,
  :syndication_reader, :crawler, :css_version, :aol_version, :user_agent_id,
  :css, :cdf, :aol, :device_name, :device_maker, :rendering_engine_name,
  :rendering_engine_version, :rendering_engine_description
]
MAP =
{
  'propertyname'         => :property_name,
  'useragent'            => :user_agent,
  'version'              => :version,
  'majorver'             => :major_version,
  'minorver'             => :minor_version,
  'majorversion'         => :major_version,
  'minorversion'         => :minor_version,
  'backgroundsounds'     => :background_sounds,
  'javaapplets'          => :java_applets,
  'activexcontrols'      => :activex_controls,
  'isbanned'             => :banned,
  'ismobiledevice'       => :mobile_device,
  'mobiledevice'         => :mobile_device,
  'issyndicationreader'  => :syndication_reader,
  'syndicationreader'    => :syndication_reader,
  'cssversion'           => :css_version,
  'aolversion'           => :aol_version,
  'useragentid'          => :user_agent_id,
  'supportscss'          => :css,
  'renderingengine_name' => :rendering_engine_name,
  'renderingengine_version' => :rendering_engine_version,
  'renderingengine_description' => :rendering_engine_description,
  'istablet'             => :tablet
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ UserAgent

Returns a new instance of UserAgent.



51
52
53
# File 'lib/browscapper/user_agent.rb', line 51

def initialize(hash = {})
  self.merge!(hash)
end

Instance Attribute Details

#patternObject

Returns the value of attribute pattern.



23
24
25
# File 'lib/browscapper/user_agent.rb', line 23

def pattern
  @pattern
end

Instance Method Details

#==(other) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/browscapper/user_agent.rb', line 93

def ==(other)
  return false unless other.is_a?(self.class)

  ACCESSORS.each do |accessor|
    return false if self.send(accessor) != other.send(accessor)
  end
  true
end

#[](key) ⇒ Object



74
75
76
77
78
# File 'lib/browscapper/user_agent.rb', line 74

def [](key)
  key = key.to_s.downcase
  key = MAP[key] || key
  self.send(key) if self.respond_to?(key)
end

#[]=(key, value) ⇒ Object



68
69
70
71
72
# File 'lib/browscapper/user_agent.rb', line 68

def []=(key, value)
  key = key.to_s.downcase
  key = "#{MAP[key] || key}="
  self.send(key, value) if self.respond_to?(key)
end

#eachObject



55
56
57
58
59
# File 'lib/browscapper/user_agent.rb', line 55

def each
  self.to_hash.each do |k, v|
    yield k, v
  end
end

#merge!(parent) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/browscapper/user_agent.rb', line 80

def merge!(parent)
  parent = parent.to_hash if parent.is_a?(UserAgent)
  ACCESSORS.each do |k, v|
    if parent.has_key?(k)
      val = self.send(k)
      if val == 'default' || val.nil?
        self.send("#{k}=", parent[k])
      end
    end
  end
  self
end

#to_hashObject



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

def to_hash
  @to_hash ||= ACCESSORS.inject({}) do |memo, accessor|
    memo[accessor] = self.send(accessor)
    memo
  end
end