class OfacSdn::Parser
OfacSdn::Parser
parses the XML document and returns a formatted hash
Attributes
document[R]
result[R]
xsd[R]
Public Class Methods
new(xsd:, document:)
click to toggle source
# File lib/ofac_sdn/parser.rb, line 13 def initialize(xsd:, document:) @xsd = xsd @document = document end
run(**kwargs)
click to toggle source
# File lib/ofac_sdn/parser.rb, line 7 def self.run(**kwargs) new(**kwargs).call end
Public Instance Methods
call()
click to toggle source
# File lib/ofac_sdn/parser.rb, line 18 def call initialize_result_hash build_hash_with('sdnList > sdnEntry') result end
Private Instance Methods
build_hash(sdn_entry)
click to toggle source
# File lib/ofac_sdn/parser.rb, line 26 def build_hash(sdn_entry) result[:sdnEntry] << sdn_entry_hash(sdn_entry) sdn_id = result[:sdnEntry].last[:id] build_sdn_lists(sdn_entry, sdn_id) end
build_hash_with(search_css)
click to toggle source
# File lib/ofac_sdn/parser.rb, line 32 def build_hash_with(search_css) document.css(search_css).map do |sdn_entry| build_hash(sdn_entry) end end
build_sdn_list(table, sdn_entry, sdn_id)
click to toggle source
# File lib/ofac_sdn/parser.rb, line 38 def build_sdn_list(table, sdn_entry, sdn_id) element = set_element(table, sdn_entry) return if element.empty? element.each_with_object({}) do |k, h| col_type = xsd[table][:element][k.name.to_sym] next unless col_type set_hash(h, k, col_type) unless k.nil? h[:sdn_id] = sdn_id end end
build_sdn_lists(sdn_entry, sdn_id)
click to toggle source
# File lib/ofac_sdn/parser.rb, line 49 def build_sdn_lists(sdn_entry, sdn_id) xsd.each_key do |table| next if table == :sdnEntry table_hash = build_sdn_list(table, sdn_entry, sdn_id) result[table] << table_hash unless table_hash.nil? end end
initialize_result_hash()
click to toggle source
# File lib/ofac_sdn/parser.rb, line 57 def initialize_result_hash @result ||= xsd.keys.each_with_object({}) { |t, h| h[t] = [] } end
sdn_entry_hash(sdn_entry)
click to toggle source
# File lib/ofac_sdn/parser.rb, line 61 def sdn_entry_hash(sdn_entry) xsd[:sdnEntry][:element].each_with_object({}) do |(e, e_type), h| element = sdn_entry.children.css(e).first set_hash(h, element, e_type) unless element.nil? end end
set_element(table, sdn_entry)
click to toggle source
# File lib/ofac_sdn/parser.rb, line 68 def set_element(table, sdn_entry) element = sdn_entry.css(table).children element = element[1].children if xsd[table][:key] && !element.empty? element end
set_hash(hash, element, column_type)
click to toggle source
# File lib/ofac_sdn/parser.rb, line 74 def set_hash(hash, element, column_type) key = :id if element.name == 'uid' key ||= element.name.to_sym hash[key] = to_type(element.text, column_type) end
to_type(value, type)
click to toggle source
# File lib/ofac_sdn/parser.rb, line 80 def to_type(value, type) case type when 'int' value.to_i when 'bool' return false if value == 'false' true else value.to_s end end