Class: Simple::Scraper::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/simple/scraper/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Parser

Returns a new instance of Parser.



6
7
8
# File 'lib/simple/scraper/parser.rb', line 6

def initialize(attributes)
  @attributes = attributes || {}
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



4
5
6
# File 'lib/simple/scraper/parser.rb', line 4

def attributes
  @attributes
end

Instance Method Details

#parse(urls, query: {}, headers: {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# 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