Class: OxTenderAbstract::XmlParser

Inherits:
Object
  • Object
show all
Includes:
ContextualLogger
Defined in:
lib/oxtenderabstract/xml_parser.rb

Overview

XML parser for tender documents

Instance Method Summary collapse

Methods included from ContextualLogger

included, #log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger

Constructor Details

#initializeXmlParser

Returns a new instance of XmlParser.



10
11
12
# File 'lib/oxtenderabstract/xml_parser.rb', line 10

def initialize
  # XML parser initialization
end

Instance Method Details

#extract_attachments(xml_content) ⇒ Object

Extract attachments information from XML



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/oxtenderabstract/xml_parser.rb', line 51

def extract_attachments(xml_content)
  return Result.failure('Empty XML content') if xml_content.nil? || xml_content.empty?

  begin
    doc = Nokogiri::XML(xml_content)
    namespaces = extract_namespaces(doc)

    # Find various attachment node patterns
    attachment_nodes = []

    # Common attachment paths
    attachment_paths = [
      '//ns4:attachmentInfo',
      '//attachmentInfo',
      '//ns5:attachmentsInfo//ns4:attachmentInfo',
      '//attachmentsInfo//attachmentInfo'
    ]

    attachment_paths.each do |path|
      nodes = doc.xpath(path, namespaces)
      attachment_nodes.concat(nodes) if nodes.any?
    end

    attachments = attachment_nodes.map { |node| extract_attachment_info(node) }.compact

    Result.success({
                     attachments: attachments,
                     total_count: attachments.size
                   })
  rescue StandardError => e
    Result.failure("Attachment extraction error: #{e.message}")
  end
end

#parse(xml_content) ⇒ Object

Parse XML document and return structured data



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/oxtenderabstract/xml_parser.rb', line 15

def parse(xml_content)
  return Result.failure('Empty XML content') if xml_content.nil? || xml_content.empty?

  begin
    doc = Nokogiri::XML(xml_content)

    # Check XML validity
    return Result.failure('Invalid XML') if doc.errors.any?

    # Detect document type
    document_type = detect_document_type(doc)

    # Extract data based on type
    parsed_data = case document_type
                  when :tender
                    parse_tender_document(doc)
                  when :contract
                    parse_contract_document(doc)
                  when :organization
                    parse_organization_document(doc)
                  else
                    parse_generic_document(doc)
                  end

    Result.success({
                     document_type: document_type,
                     root_element: doc.root.name,
                     namespace: doc.root.namespace&.href,
                     content: parsed_data
                   })
  rescue StandardError => e
    Result.failure("XML parsing error: #{e.message}")
  end
end