Module: NoaaWeatherClient::Responses::ValidatableXmlResponse

Included in:
CurrentObservation, Forecast, LatLonList
Defined in:
lib/noaa_weather_client/responses/validatable_xml_response.rb

Constant Summary collapse

SCHEMA_PATH =
File.expand_path("../../../../data/xml", __FILE__)

Instance Method Summary collapse

Instance Method Details

#validate!(doc, schema_name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/noaa_weather_client/responses/validatable_xml_response.rb', line 8

def validate!(doc, schema_name)
  # chdir to help Nokogiri load included schemas
  Dir.chdir(SCHEMA_PATH) do
    schema_file = File.join(SCHEMA_PATH, "#{schema_name}.xsd")
    schema = Nokogiri::XML::Schema(File.read(schema_file))
    errors = schema.validate(doc)
    unless errors.empty?
      raise Errors::InvalidXmlError, "Invalid xml: #{errors.map(&:message).join("\n")}"
    end
  end
end