Class: OpenSRS::XmlProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/opensrs/xml_processor.rb,
lib/opensrs/xml_processor/libxml.rb,
lib/opensrs/xml_processor/nokogiri.rb

Overview

XmlProcessor

Direct Known Subclasses

Libxml, Nokogiri

Defined Under Namespace

Classes: Libxml, Nokogiri

Class Method Summary collapse

Class Method Details

.build_element(type, data, container) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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) ⇒ Object

Recursively decodes individual data elements from OpenSRS server response.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object

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



15
16
17
18
19
20
21
22
23
24
25
26
# 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) ⇒ Object



28
29
30
# 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) ⇒ Object



32
33
34
# File 'lib/opensrs/xml_processor.rb', line 32

def self.encode_dt_assoc(data, container)
  build_element(:dt_assoc, data, container)
end

.parse(response) ⇒ Object

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

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
# 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