Class: Openphar::Parsers::JpHtmlParserBase

Inherits:
Object
  • Object
show all
Defined in:
lib/openphar/parsers/jp_html_parser_base.rb

Overview

Base class for JP HTML parsers

Provides common functionality for parsing Japan Pharmacopoeia HTML files. Subclasses should implement:

  • #parse method
  • Section pattern constants

Direct Known Subclasses

JpJaHtmlParser

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(html_content) ⇒ JpHtmlParserBase

Initialize parser with HTML content

Parameters:

  • html_content (String)

    The HTML content to parse



19
20
21
# File 'lib/openphar/parsers/jp_html_parser_base.rb', line 19

def initialize(html_content)
  @document = Nokogiri::HTML(html_content, nil, "UTF-8")
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



14
15
16
# File 'lib/openphar/parsers/jp_html_parser_base.rb', line 14

def document
  @document
end

Class Method Details

.parse_file(file_path) ⇒ Array<Hash>

Parse monographs from a file

Parameters:

  • file_path (String)

    Path to HTML file

Returns:

  • (Array<Hash>)

    Array of parsed monograph data



35
36
37
38
# File 'lib/openphar/parsers/jp_html_parser_base.rb', line 35

def self.parse_file(file_path)
  html_content = File.read(file_path, encoding: "UTF-8")
  new(html_content).parse
end

.parse_files(file_paths) ⇒ Array<Hash>

Parse monographs from multiple files

Parameters:

  • file_paths (Array<String>)

    Paths to HTML files

Returns:

  • (Array<Hash>)

    Combined array of parsed monograph data



44
45
46
# File 'lib/openphar/parsers/jp_html_parser_base.rb', line 44

def self.parse_files(file_paths)
  file_paths.flat_map { |path| parse_file(path) }
end

Instance Method Details

#parseArray<Hash>

Parse monographs from the document Subclasses should override this method

Returns:

  • (Array<Hash>)

    Array of parsed monograph data

Raises:

  • (NotImplementedError)


27
28
29
# File 'lib/openphar/parsers/jp_html_parser_base.rb', line 27

def parse
  raise NotImplementedError, "Subclasses must implement #parse"
end