Class: Nukitori::SchemaExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/nukitori/schema_extractor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ SchemaExtractor

Returns a new instance of SchemaExtractor.

Parameters:

  • schema (Hash) —

    XPath schema



8
9
10
# File 'lib/nukitori/schema_extractor.rb', line 8

def initialize(schema)
  @schema = deep_stringify_keys(schema)
end

Instance Attribute Details

#schema ⇒ Object (readonly)

Returns the value of attribute schema.



5
6
7
# File 'lib/nukitori/schema_extractor.rb', line 5

def schema
  @schema
end

Instance Method Details

#extract(html) ⇒ Hash

Extract data from HTML using the XPath schema

Parameters:

  • html (String, Nokogiri::HTML::Document) —

    HTML string or Nokogiri document

Returns:

  • (Hash) —

    Extracted data



15
16
17
18
# File 'lib/nukitori/schema_extractor.rb', line 15

def extract(html)
  doc = html.is_a?(Nokogiri::HTML::Document) ? html : Nokogiri::HTML(html)
  extract_fields(doc, schema)
end