# Class: Browser — Documentation by YARD 0.8.6.1

Class List Method List File List

# Class: Browser

Inherits:
  Object
  - Object
  - Browser
  show all
Extended by:
  Actions
Includes:
  Options
Defined in:
  lib/common/browser.rb,
  lib/common/browser/options.rb,
  lib/common/browser/actions.rb

## Defined Under Namespace

Modules: Actions, Options

## Constant Summary

OPTIONS =

      [
        :available_user_agents,
        :basic_auth,
        :cache_ttl,
        :max_threads,
        :user_agent,
        :user_agent_mode,
        :proxy,
        :proxy_auth
      ]

@@instance =

      nil

### Constants included from Options

Options::USER_AGENT_MODES

## Instance Attribute Summary (collapse)

- - (Object) cache_dir readonly

  Returns the value of attribute cache_dir.

- - (Object) config_file readonly

  Returns the value of attribute config_file.

- - (Object) hydra readonly

  Returns the value of attribute hydra.

### Attributes included from Options

#available_user_agents, #basic_auth, #cache_ttl, #proxy, #proxy_auth, #user_agent, #user_agent_mode

## Class Method Summary (collapse)

- + (Array) append_params_header_field(params = {}, field, field_value) private
- + (Browser) instance(options = {})
- + (Object) reset

## Instance Method Summary (collapse)

- - (Typhoeus::Request) forge_request(url, params = {})
- - (Browser) initialize(options = {}) constructor
- - (void) load_config(config_file = nil)

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

- - (Hash) merge_request_params(params = {})

### Methods included from Actions

get, get_and_follow_location, post, process

### Methods included from Options

#invalid_proxy_auth_format, #max_threads, #max_threads=, #override_config

## Constructor Details

### - (`Browser`) initialize(options = {})

Parameters:

- options (`Hash`) (defaults to: `{}`)

    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44

    # File 'lib/common/browser.rb', line 29

    def initialize(options = {})
      @config_file = options[:config_file] || CONF_DIR + '/browser.conf.json'
      @cache_dir   = options[:cache_dir]   || CACHE_DIR + '/browser'

      load_config()
      override_config(options)

      unless @hydra
        @hydra = Typhoeus::Hydra.new(max_concurrency: self.max_threads)
      end

      @cache = TyphoeusCache.new(@cache_dir)
      @cache.clean

      Typhoeus::Config.cache = @cache
    end

## Instance Attribute Details

### - (`Object`) cache_dir (readonly)

Returns the value of attribute cache_dir

    24
    25
    26

    # File 'lib/common/browser.rb', line 24

    def cache_dir
      @cache_dir
    end

### - (`Object`) config_file (readonly)

Returns the value of attribute config_file

    24
    25
    26

    # File 'lib/common/browser.rb', line 24

    def config_file
      @config_file
    end

### - (`Object`) hydra (readonly)

Returns the value of attribute hydra

    24
    25
    26

    # File 'lib/common/browser.rb', line 24

    def hydra
      @hydra
    end

## Class Method Details

### + (`Array`) append_params_header_field(params = {}, field, field_value) (private)

Parameters:

- params (`Hash`) (defaults to: `{}`)
- field (`String`)
- field_value (`Mixed`)

Returns:

- (`Array`)

    143
    144
    145
    146
    147
    148
    149
    150

    # File 'lib/common/browser.rb', line 143

    def self.append_params_header_field(params = {}, field, field_value)
      if !params.has_key?(:headers)
        params = params.merge(:headers => { field => field_value })
      elsif !params[:headers].has_key?(field)
        params[:headers][field] = field_value
      end
      params
    end

### + (`Browser`) instance(options = {})

Parameters:

- options (`Hash`) (defaults to: `{}`)

Returns:

- (`Browser`)

    51
    52
    53
    54
    55
    56

    # File 'lib/common/browser.rb', line 51

    def self.instance(options = {})
      unless @@instance
        @@instance = new(options)
      end
      @@instance
    end

### + (`Object`) reset

    58
    59
    60

    # File 'lib/common/browser.rb', line 58

    def self.reset
      @@instance = nil
    end

## Instance Method Details

### - (`Typhoeus::Request`) forge_request(url, params = {})

Parameters:

- url (`String`)
- params (`Hash`) (defaults to: `{}`)

Returns:

- (`Typhoeus::Request`)

    91
    92
    93

    # File 'lib/common/browser.rb', line 91

    def forge_request(url, params = {})
      Typhoeus::Request.new(url, merge_request_params(params))
    end

### - (`void`) load_config(config_file = nil)

This method returns an undefined value.

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

Parameters:

- config_file (`String`) (defaults to: `nil`)

    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85

    # File 'lib/common/browser.rb', line 69

    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

### - (`Hash`) merge_request_params(params = {})

Parameters:

- params (`Hash`) (defaults to: `{}`)

Returns:

- (`Hash`)

    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134

    # File 'lib/common/browser.rb', line 98

    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