Class: RateCenter::DataSource::LocalCallingGuide::ResponseParser
- Inherits:
-
Object
- Object
- RateCenter::DataSource::LocalCallingGuide::ResponseParser
- Defined in:
- lib/rate_center/data_source/local_calling_guide.rb
Defined Under Namespace
Classes: ParseError, Response
Instance Attribute Summary collapse
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Instance Method Summary collapse
-
#initialize(**options) ⇒ ResponseParser
constructor
A new instance of ResponseParser.
- #parse(xml, keys:) ⇒ Object
Constructor Details
#initialize(**options) ⇒ ResponseParser
Returns a new instance of ResponseParser.
29 30 31 |
# File 'lib/rate_center/data_source/local_calling_guide.rb', line 29 def initialize(**) @parser = .fetch(:parser) { MultiXml } end |
Instance Attribute Details
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
27 28 29 |
# File 'lib/rate_center/data_source/local_calling_guide.rb', line 27 def parser @parser end |
Instance Method Details
#parse(xml, keys:) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rate_center/data_source/local_calling_guide.rb', line 33 def parse(xml, keys:) response_data = parser.parse(xml) data = response_data.dig("root", *Array(keys)) return Response.new(data: []) if data.nil? data = data.is_a?(Array) ? data : Array([ data ]) parsed_data = data.map do |d| OpenStruct.new(d.transform_values { |v| v.strip.empty? ? nil : v }) end Response.new(data: parsed_data) rescue MultiXml::ParseError => e raise ParseError.new(e.) end |