Parent

Browser

Attributes

config_file[R]
hydra[R]

Public Class Methods

instance(options = {}) click to toggle source
# File lib/common/browser.rb, line 48
def self.instance(options = {})
  unless @@instance
    @@instance = new(options)
  end
  @@instance
end
reset() click to toggle source
# File lib/common/browser.rb, line 55
def self.reset
  @@instance = nil
end

Public Instance Methods

forge_request(url, params = {}) click to toggle source
# File lib/common/browser.rb, line 146
def forge_request(url, params = {})
  Typhoeus::Request.new(
    url.to_s,
    merge_request_params(params)
  )
end
get(url, params = {}) click to toggle source
# File lib/common/browser.rb, line 126
def get(url, params = {})
  run_request(
    forge_request(url, params.merge(method: :get))
  )
end
get_and_follow_location(url, params = {}) click to toggle source
# File lib/common/browser.rb, line 138
def get_and_follow_location(url, params = {})
  params[:maxredirs] ||= 2

  run_request(
    forge_request(url, params.merge(method: :get, followlocation: true))
  )
end
invalid_proxy_auth_format() click to toggle source
# File lib/common/browser.rb, line 104
def invalid_proxy_auth_format
  'Invalid proxy auth format, expected username:password or {proxy_username: username, proxy_password: password}'
end
load_config(config_file = nil) click to toggle source

TODO reload hydra (if the .load_config is called on a browser object, hydra will not have the new @max_threads and @request_timeout)

# File lib/common/browser.rb, line 110
def load_config(config_file = nil)
  @config_file = config_file || @config_file

  if File.symlink?(@config_file)
    raise "[ERROR] Config file is a symlink."
  else
    data = JSON.parse(File.read(@config_file))
  end

  ACCESSOR_OPTIONS.each do |option|
    option_name = option.to_s

    self.send(:"#{option_name}=", data[option_name])
  end
end
max_threads=(max_threads) click to toggle source
# File lib/common/browser.rb, line 85
def max_threads=(max_threads)
  if max_threads.nil? or max_threads <= 0
    max_threads = 1
  end
  @max_threads = max_threads
end
merge_request_params(params = {}) click to toggle source
# File lib/common/browser.rb, line 153
def merge_request_params(params = {})
  params = Browser.append_params_header_field(
    params,
    'User-Agent',
    self.user_agent
  )

  if @proxy
    params = params.merge(proxy: @proxy)

    if @proxy_auth
      params = params.merge(proxyauth: @proxy_auth)
    end
  end

  if @basic_auth
    params = Browser.append_params_header_field(
      params,
      'Authorization',
      @basic_auth
    )
  end

  # Used to enable the cache system if :cache_ttl > 0
  unless params.has_key?(:cache_ttl)
    params = params.merge(cache_ttl: @cache_ttl)
  end

  # Disable SSL-Certificate checks
  params = params.merge(ssl_verifypeer: false)
  params = params.merge(ssl_verifyhost: 0)

  params
end
post(url, params = {}) click to toggle source
# File lib/common/browser.rb, line 132
def post(url, params = {})
  run_request(
    forge_request(url, params.merge(method: :post))
  )
end
proxy_auth=(auth) click to toggle source
# File lib/common/browser.rb, line 92
def proxy_auth=(auth)
  unless auth.nil?
    if auth.is_a?(Hash) && auth.include?(:proxy_username) && auth.include?(:proxy_password)
      @proxy_auth = auth[:proxy_username] + ':' + auth[:proxy_password]
    elsif auth.is_a?(String) && auth.index(':') != nil
      @proxy_auth = auth
    else
      raise invalid_proxy_auth_format
    end
  end
end
user_agent() click to toggle source

return the user agent, according to the user_agent_mode

# File lib/common/browser.rb, line 73
def user_agent
  case @user_agent_mode
  when 'semi-static'
    unless @user_agent
      @user_agent = @available_user_agents.sample
    end
  when 'random'
    @user_agent = @available_user_agents.sample
  end
  @user_agent
end
user_agent_mode=(ua_mode) click to toggle source
# File lib/common/browser.rb, line 59
def user_agent_mode=(ua_mode)
  ua_mode ||= 'static'

  if USER_AGENT_MODES.include?(ua_mode)
    @user_agent_mode = ua_mode
    # For semi-static user agent mode, the user agent has to
    # be nil the first time (it will be set with the getter)
    @user_agent = nil if ua_mode === 'semi-static'
  else
    raise "Unknow user agent mode : '#{ua_mode}'"
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.