Class: FetchUtil::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/fetch_util/extractor.rb

Constant Summary collapse

INLINE_ASSET_PATHS =
%w[vendor/readability.js vendor/turndown.js extract.js].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reader_mode: true, asset_root: nil) ⇒ Extractor

Returns a new instance of Extractor.



21
22
23
24
25
# File 'lib/fetch_util/extractor.rb', line 21

def initialize(reader_mode: true, asset_root: nil)
  @reader_mode = reader_mode
  @asset_root = asset_root || File.join(__dir__, "assets")
  @extraction_call = nil
end

Class Method Details

.inline_asset_scripts(asset_root) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/fetch_util/extractor.rb', line 12

def inline_asset_scripts(asset_root)
  @cache_mutex.synchronize do
    @asset_cache[asset_root] ||= INLINE_ASSET_PATHS.map do |relative_path|
      File.read(File.join(asset_root, relative_path), encoding: "UTF-8").freeze
    end.freeze
  end
end

Instance Method Details

#extract(page) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/fetch_util/extractor.rb', line 27

def extract(page)
  payload = extract_payload(page)
  raise ExtractionError, "Page extraction returned no content" unless payload.is_a?(Hash)

  payload
rescue Ferrum::JavaScriptError, Ferrum::StatusError, Ferrum::TimeoutError => e
  raise ExtractionError, e.message
end