Class: Stockboy::Readers::XML

Inherits:
Stockboy::Reader show all
Defined in:
lib/stockboy/readers/xml.rb

Overview

Extract data from XML

This works great with SOAP, probably not fully-featured yet for various XML formats. The SOAP provider returns a hash, because it takes care of extracting the envelope body already, so this reader supports options for reading elements from a nested hash too.

Backed by the Nori gem from Savon, see nori for full options.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ XML

Initialize a new XML reader



86
87
88
89
90
91
# File 'lib/stockboy/readers/xml.rb', line 86

def initialize(opts={}, &block)
  super
  self.elements = opts.delete(:elements)
  @xml_options = opts
  DSL.new(self).instance_eval(&block) if block_given?
end

Instance Attribute Details

#advanced_typecastingBoolean

Detects input tag types and tries to extract dates, times, etc. from the data. Normally this is handled by the attribute map.

Returns:

  • (Boolean)


58
# File 'lib/stockboy/readers/xml.rb', line 58

dsl_attr :advanced_typecasting, attr_accessor: false

#convert_tags_toProc

Change tag formatting, e.g. underscore if it happens to match your actual record attributes

Examples:

convert_tags_to ->(tag) { tag.underscore }

Returns:

  • (Proc)


50
# File 'lib/stockboy/readers/xml.rb', line 50

dsl_attr :convert_tags_to, attr_accessor: false

#elementsArray

Element nesting to traverse, the last one should represent the record instances that contain tags for each attribute.

Examples:

elements ["allItemsResponse", "itemList", "recordItem"]

Returns:

  • (Array)

Raises:

  • (ArgumentError)


33
# File 'lib/stockboy/readers/xml.rb', line 33

dsl_attr :elements, attr_accessor: false

#encodingString

Override source encoding

Returns:

  • (String)


23
# File 'lib/stockboy/readers/xml.rb', line 23

dsl_attr :encoding

#optionsHash (readonly)

XML options passed to the underlying Nori instance

Returns:

  • (Hash)


98
99
100
# File 'lib/stockboy/readers/xml.rb', line 98

def options
  @xml_options
end

#parserSymbol

Defaults to Nokogiri. Why would you change it?

Returns:

  • (Symbol)


65
# File 'lib/stockboy/readers/xml.rb', line 65

dsl_attr :parser, attr_accessor: false

#strip_namespacesBoolean

Removes namespace prefixes from tag names, default true.

Returns:

  • (Boolean)


40
# File 'lib/stockboy/readers/xml.rb', line 40

dsl_attr :strip_namespaces, attr_accessor: false

Instance Method Details

#parse(data) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/stockboy/readers/xml.rb', line 102

def parse(data)
  hash = if data.is_a? Hash
    data
  else
    if data.respond_to? :to_xml
      data.to_xml("UTF-8")
      nori.parse(data)
    elsif data.respond_to? :to_hash
      data.to_hash
    else
      data.encode!("UTF-8", encoding) if encoding
      nori.parse(data)
    end
  end

  with_string_pool do
    remap_keys hash
    extract hash
  end
end