Class: DS::Extractor::XmlRecordLocator

Inherits:
BaseRecordLocator show all
Defined in:
lib/ds/extractor/xml_record_locator.rb

Instance Attribute Summary collapse

Attributes inherited from BaseRecordLocator

#errors

Instance Method Summary collapse

Methods inherited from BaseRecordLocator

#add_error

Constructor Details

#initialize(namespaces: DS::Constants::XML_NAMESPACES) ⇒ XmlRecordLocator

Returns a new instance of XmlRecordLocator.



9
10
11
12
# File 'lib/ds/extractor/xml_record_locator.rb', line 9

def initialize namespaces: DS::Constants::XML_NAMESPACES
  @namespaces = namespaces
  super()
end

Instance Attribute Details

#namespaces ⇒ Object

Returns the value of attribute namespaces.



7
8
9
# File 'lib/ds/extractor/xml_record_locator.rb', line 7

def namespaces
  @namespaces
end

Instance Method Details

#locate_record(xml, id, id_location) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ds/extractor/xml_record_locator.rb', line 14

def locate_record xml, id, id_location
  xpath = id_location.gsub(/ID_PLACEHOLDER/, id)
  # try with namespaces
  record = try_locate_record xml, xpath, namespaces: namespaces
  return record if record.present?

  # try without providing namespaces
  record = try_locate_record xml, xpath
  return record if record.present?

  # strip namespaces and try one last time
  xml.remove_namespaces!
  try_locate_record xml, xpath
end

#try_locate_record(xml, xpath, namespaces: nil) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/ds/extractor/xml_record_locator.rb', line 29

def try_locate_record xml, xpath, namespaces: nil
  xml.xpath xpath, namespaces
rescue Nokogiri::XML::XPath::SyntaxError => e
  add_error e.message
  raise unless e.message =~ /undefined namespace prefix/i
  []
end