Class: RateCenter::DataSource::LocalCallingGuide::ResponseParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rate_center/data_source/local_calling_guide.rb

Defined Under Namespace

Classes: ParseError, Response

Instance Attribute Summary collapse

Instance Method Summary collapse

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(**options)
  @parser = options.fetch(:parser) { MultiXml }
end

Instance Attribute Details

#parserObject (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.message)
end