Class: UserAgentGenerator::Logic

Inherits:
Object
  • Object
show all
Defined in:
lib/User_Agent_Generator/logic.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Logic

Returns a new instance of Logic.



4
5
6
# File 'lib/User_Agent_Generator/logic.rb', line 4

def initialize(options)
  @options = options
end

Instance Method Details

#androidObject



25
26
27
28
# File 'lib/User_Agent_Generator/logic.rb', line 25

def android
  string = 'Android / '
  string + browser
end

#browserObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/User_Agent_Generator/logic.rb', line 35

def browser
  if @options[:chrome]
    chrome(@options[:version])
  elsif @options[:firefox]
    firefox(@options[:version])
  elsif @options[:safari]
    safari(@options[:version])
  else
    ''
  end
end

#chrome(version) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/User_Agent_Generator/logic.rb', line 47

def chrome(version)
  string = "Chrome #{version[0, 2]}: Mozilla/5.0 () AppleWebKit/537.36 (KHTML, like Gecko) Chrome/#{version} Safari/537.36"
  os_index = string.index('()') + 1
  string = @options[:windows] ? string.insert(os_index, 'Windows NT 6.1; WOW64') : string
  string = @options[:android] ? string.insert(os_index, 'Linux; 4.4.2; Nexus 4 Build/KOT49H') : string
  if @options[:iOS]
    string.insert(os_index, 'iPad; CPU OS 7_0_4 like Mac OS X')
    mobile_index = (string.index('Safari') - 1)
    string.insert(mobile_index, ' Mobile')
  else
    string
  end
end

#firefox(version) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/User_Agent_Generator/logic.rb', line 61

def firefox(version)
  fail ArgumentError.new('Firefox not compatible with iOS devices') if @options[:iOS]
  string = "Firefox #{version[0, 2]}: Mozilla/5.0 () Gecko/ Firefox/#{version}"
  os_index = string.index('()') + 1
  if @options[:windows]
    string = string.insert(os_index, "Windows NT 6.1; WOW64; rv:#{version[0, 2]}.0")
    gecko_index = string.index('Gecko/') + 6
    string.insert(gecko_index, '20100101')
  elsif @options[:android]
    string = string.insert(os_index, "Android; Mobile; rv:#{version[0, 2]}.0")
    gecko_index = string.index('Gecko/') + 6
    string.insert(gecko_index, "#{version}")
  else
    string
  end
end

#iOSObject



30
31
32
33
# File 'lib/User_Agent_Generator/logic.rb', line 30

def iOS
  string = 'iOS / '
  string + browser
end

#runObject



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/User_Agent_Generator/logic.rb', line 8

def run
  if @options[:windows]
    windows
  elsif @options[:android]
    android
  elsif @options[:iOS]
    iOS
  else
    ''
  end
end

#safari(version) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/User_Agent_Generator/logic.rb', line 78

def safari(version)
  fail ArgumentError.new('Safari not compatible with Windows') \
    if @options[:windows]
  fail ArgumentError.new('Safari not compatible with Android devices') \
    if @options[:android]
  "Safari #{version[0]} (iPad; CPU OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/#{version} Mobile/11B554a Safari/9537.53"
end

#windowsObject



20
21
22
23
# File 'lib/User_Agent_Generator/logic.rb', line 20

def windows
  string = 'Windows / '
  string + browser
end