Class: HttpReader::Engine
- Inherits:
-
Object
- Object
- HttpReader::Engine
- Defined in:
- lib/http_reader/engine.rb
Defined Under Namespace
Classes: DefaultResponse
Constant Summary collapse
- ReadError =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#browser ⇒ Object
readonly
Returns the value of attribute browser.
-
#browser_keep_running ⇒ Object
readonly
Returns the value of attribute browser_keep_running.
-
#default_parser ⇒ Object
readonly
Returns the value of attribute default_parser.
-
#http_client ⇒ Object
readonly
Returns the value of attribute http_client.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#parsers ⇒ Object
readonly
Returns the value of attribute parsers.
Instance Method Summary collapse
- #close_browser ⇒ Object
-
#initialize(config = {}) ⇒ Engine
constructor
A new instance of Engine.
- #read(url, opts = {}) ⇒ Object
Constructor Details
#initialize(config = {}) ⇒ Engine
Returns a new instance of Engine.
13 14 15 16 17 18 19 20 |
# File 'lib/http_reader/engine.rb', line 13 def initialize(config = {}) @parsers = config.fetch(:parsers, []) @default_parser = config.fetch(:default_parser, HashPageParser) @http_client = config.fetch(:http_client, HTTParty) @browser = config.fetch(:browser, Watir::Browser) @logger = config.fetch(:logger, Logger.new(STDOUT)) @browser_keep_running = config.fetch(:browser_keep_running, true) end |
Instance Attribute Details
#browser ⇒ Object (readonly)
Returns the value of attribute browser.
11 12 13 |
# File 'lib/http_reader/engine.rb', line 11 def browser @browser end |
#browser_keep_running ⇒ Object (readonly)
Returns the value of attribute browser_keep_running.
11 12 13 |
# File 'lib/http_reader/engine.rb', line 11 def browser_keep_running @browser_keep_running end |
#default_parser ⇒ Object (readonly)
Returns the value of attribute default_parser.
11 12 13 |
# File 'lib/http_reader/engine.rb', line 11 def default_parser @default_parser end |
#http_client ⇒ Object (readonly)
Returns the value of attribute http_client.
11 12 13 |
# File 'lib/http_reader/engine.rb', line 11 def http_client @http_client end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
11 12 13 |
# File 'lib/http_reader/engine.rb', line 11 def logger @logger end |
#parsers ⇒ Object (readonly)
Returns the value of attribute parsers.
11 12 13 |
# File 'lib/http_reader/engine.rb', line 11 def parsers @parsers end |
Instance Method Details
#close_browser ⇒ Object
39 40 41 42 43 |
# File 'lib/http_reader/engine.rb', line 39 def close_browser active_browser.close headless.destroy @active_browser = nil end |
#read(url, opts = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/http_reader/engine.rb', line 22 def read(url, opts = {}) parser = opts[:parser] || find_parser(url) response = if parser.use_browser browse_opts = opts.fetch(:browse_opts, {}) browse(url, parser, browse_opts) else request_opts = opts.fetch(:request_opts, {}) request(url, request_opts) end parse_opts = opts.fetch(:parse_opts, {}) parser.parse(response, parse_opts) rescue => e log_error('read', e, "url: #{url}, opts: #{opts.to_json}") raise ReadError.new(e.) end |