class Browser

Constants

OPTIONS

Attributes

cache_dir[R]
config_file[R]
hydra[R]

Public Class Methods

instance(options = {}) click to toggle source

@param [ Hash ] options

@return [ Browser ]

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

Public Instance Methods

forge_request(url, params = {}) click to toggle source

@param [ String ] url @param [ Hash ] params

@return [ Typhoeus::Request ]

# File lib/common/browser.rb, line 90
def forge_request(url, params = {})
  Typhoeus::Request.new(url, merge_request_params(params))
end
load_config(config_file = nil) click to toggle source

If an option was set but is not in the new #config_file it’s value is kept

@param [ String ] #config_file

@return [ void ]

# File lib/common/browser.rb, line 68
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

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

    unless data[option_name].nil?
      self.send(:"#{option_name}=", data[option_name])
    end
  end
end
merge_request_params(params = {}) click to toggle source

@param [ Hash ] params

@return [ Hash ]

# File lib/common/browser.rb, line 97
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.merge!(ssl_verifypeer: false)
  params.merge!(ssl_verifyhost: 0)

  params.merge!(cookiejar: @cache_dir + '/cookie-jar')
  params.merge!(cookiefile: @cache_dir + '/cookie-jar')

  params
end