class Simple::Scraper::Parser

Attributes

attributes[R]

Public Class Methods

new(attributes) click to toggle source
# File lib/simple/scraper/parser.rb, line 6
def initialize(attributes)
  @attributes = attributes || {}
end

Public Instance Methods

parse(urls, query: {}, headers: {}) click to toggle source
# File lib/simple/scraper/parser.rb, line 10
def parse(urls, query: {}, headers: {})
  Parallel.map(Array(urls), in_threads: Simple::Scraper.configuration.number_of_threads) do |url|
    Finder.find(url: url, query: query, headers: headers) do |page|
      attributes.each_with_object({}) do |(key, options), hsh|
        hsh[key] = options[:handler].call(page.xpath(options[:selector]))
      rescue StandardError => e
        Simple::Scraper.logger&.error e
        hsh[key] = options[:default]
      end
    end
  end.compact
end