Class: Stockboy::Readers::XML
- Inherits:
-
Stockboy::Reader
- Object
- Stockboy::Reader
- Stockboy::Readers::XML
- 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
-
#advanced_typecasting ⇒ Boolean
Detects input tag types and tries to extract dates, times, etc.
-
#convert_tags_to ⇒ Proc
Change tag formatting, e.g.
-
#elements ⇒ Array
Element nesting to traverse, the last one should represent the record instances that contain tags for each attribute.
-
#encoding ⇒ String
Override source encoding.
-
#options ⇒ Hash
readonly
XML options passed to the underlying Nori instance.
-
#parser ⇒ Symbol
Defaults to Nokogiri.
-
#strip_namespaces ⇒ Boolean
Removes namespace prefixes from tag names, default true.
Instance Method Summary collapse
-
#initialize(opts = {}, &block) ⇒ XML
constructor
Initialize a new XML reader.
- #parse(data) ⇒ Object
Constructor Details
Instance Attribute Details
#advanced_typecasting ⇒ Boolean
Detects input tag types and tries to extract dates, times, etc. from the data. Normally this is handled by the attribute map.
58 |
# File 'lib/stockboy/readers/xml.rb', line 58 dsl_attr :advanced_typecasting, attr_accessor: false |
#convert_tags_to ⇒ Proc
Change tag formatting, e.g. underscore if it happens to match your actual record attributes
50 |
# File 'lib/stockboy/readers/xml.rb', line 50 dsl_attr :convert_tags_to, attr_accessor: false |
#elements ⇒ Array
Element nesting to traverse, the last one should represent the record instances that contain tags for each attribute.
33 |
# File 'lib/stockboy/readers/xml.rb', line 33 dsl_attr :elements, attr_accessor: false |
#encoding ⇒ String
Override source encoding
23 |
# File 'lib/stockboy/readers/xml.rb', line 23 dsl_attr :encoding |
#options ⇒ Hash (readonly)
XML options passed to the underlying Nori instance
98 99 100 |
# File 'lib/stockboy/readers/xml.rb', line 98 def @xml_options end |
#parser ⇒ Symbol
Defaults to Nokogiri. Why would you change it?
65 |
# File 'lib/stockboy/readers/xml.rb', line 65 dsl_attr :parser, attr_accessor: false |
#strip_namespaces ⇒ Boolean
Removes namespace prefixes from tag names, default true.
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 |