Class: Browser
- Inherits:
-
Object
- Object
- Browser
- Extended by:
- Actions
- Includes:
- Options
- Defined in:
- lib/common/browser.rb,
lib/common/browser/options.rb,
lib/common/browser/actions.rb
Defined Under Namespace
Constant Summary
- OPTIONS =
[ :available_user_agents, :basic_auth, :cache_ttl, :max_threads, :user_agent, :user_agent_mode, :proxy, :proxy_auth, :request_timeout, :connect_timeout ]
- @@instance =
nil
Constants included from Options
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, #connect_timeout, #proxy, #proxy_auth, #request_timeout, #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, head, post, process
Methods included from Options
#invalid_proxy_auth_format, #max_threads, #max_threads=, #override_config
Constructor Details
- (Browser) initialize(options = {})
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/common/browser.rb', line 31 def initialize( = {}) @config_file = [:config_file] || CONF_DIR + '/browser.conf.json' @cache_dir = [:cache_dir] || CACHE_DIR + '/browser' load_config override_config() 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
26 27 28 |
# File 'lib/common/browser.rb', line 26 def cache_dir @cache_dir end |
- (Object) config_file (readonly)
Returns the value of attribute config_file
26 27 28 |
# File 'lib/common/browser.rb', line 26 def config_file @config_file end |
- (Object) hydra (readonly)
Returns the value of attribute hydra
26 27 28 |
# File 'lib/common/browser.rb', line 26 def hydra @hydra end |
Class Method Details
+ (Array) append_params_header_field(params = {}, field, field_value) (private)
153 154 155 156 157 158 159 160 |
# File 'lib/common/browser.rb', line 153 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 = {})
53 54 55 56 57 58 |
# File 'lib/common/browser.rb', line 53 def self.instance( = {}) unless @@instance @@instance = new() end @@instance end |
+ (Object) reset
60 61 62 |
# File 'lib/common/browser.rb', line 60 def self.reset @@instance = nil end |
Instance Method Details
- (Typhoeus::Request) forge_request(url, params = {})
93 94 95 |
# File 'lib/common/browser.rb', line 93 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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/common/browser.rb', line 71 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 = {})
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 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/common/browser.rb', line 100 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 if @request_timeout params = params.merge(timeout: @request_timeout) end if @connect_timeout params = params.merge(connecttimeout: @connect_timeout) 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 |