class OpenSRS::XmlProcessor

XmlProcessor

Public Class Methods

build_element(type, data, container) click to toggle source
# File lib/opensrs/xml_processor.rb, line 36
def self.build_element(type, data, container)
  element = new_element(type, container)

  # if array, item = the item
  # if hash, item will be array of the key & value
  data.each_with_index do |item, index|
    item_node = new_element(:item, container)
    item_node['key'] = item.is_a?(Array) ? item[0].to_s : index.to_s

    value = item.is_a?(Array) ? item[1] : item

    encoded_data = encode_data(value, item_node)
    if encoded_data.is_a?(String)
      item_node.content = encoded_data
    else
      item_node << encoded_data
    end
    element << item_node
  end

  element
end
decode_data(data) click to toggle source

Recursively decodes individual data elements from OpenSRS server response.

# File lib/opensrs/xml_processor.rb, line 61
def self.decode_data(data)
  data.each do |element|
    case element.name
    when 'dt_array'
      return decode_dt_array_data(element)
    when 'dt_assoc'
      return decode_dt_assoc_data(element)
    when 'text', 'item', 'dt_scalar'
      next if element.content.strip.empty?

      return element.content.strip
    end
  end
end
encode_data(data, container = nil) click to toggle source

Encodes individual elements, and their child elements, for the root XML document.

# File lib/opensrs/xml_processor.rb, line 15
def self.encode_data(data, container = nil)
  case data
  when Array
    encode_dt_array(data, container)
  when Hash
    encode_dt_assoc(data, container)
  when String, Numeric, Date, Time, Symbol, NilClass
    data.to_s
  else
    data.inspect
  end
end
encode_dt_array(data, container) click to toggle source
# File lib/opensrs/xml_processor.rb, line 28
def self.encode_dt_array(data, container)
  build_element(:dt_array, data, container)
end
encode_dt_assoc(data, container) click to toggle source
# File lib/opensrs/xml_processor.rb, line 32
def self.encode_dt_assoc(data, container)
  build_element(:dt_assoc, data, container)
end
parse(response) click to toggle source

Parses the main data block from OpenSRS and discards the rest of the response.

# File lib/opensrs/xml_processor.rb, line 6
def self.parse(response)
  data_block = data_block_element(response)

  raise ArgumentError, 'No data found in document' unless data_block

  decode_data(data_block)
end