Class: UserAgentRandomizer::UserAgent

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ UserAgent

Returns a new instance of UserAgent.



7
8
9
10
# File 'lib/user_agent_randomizer/user_agent.rb', line 7

def initialize(options = {})
  @type = options[:type]      
  @string = options[:string]      
end

Instance Attribute Details

#stringObject

Returns the value of attribute string.



5
6
7
# File 'lib/user_agent_randomizer/user_agent.rb', line 5

def string
  @string
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/user_agent_randomizer/user_agent.rb', line 5

def type
  @type
end

Class Method Details

.fetch(options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/user_agent_randomizer/user_agent.rb', line 12

def self.fetch(options = {})
  type = options[:type]
  ua_types = UserAgentRandomizer.user_agent_types
  user_agents_hash = UserAgentRandomizer.user_agents_hash
  if ua_types.include?(type)
    UserAgentRandomizer::UserAgent.new(type: type, string: user_agents_hash[type].sample)
  else
    ua = UserAgentRandomizer.user_agents_array.sample
    UserAgentRandomizer::UserAgent.new(type: ua[:type], string: ua[:string])
  end
end