From e0f5865cd8b0a751f5a691b86735e40e32273a04 Mon Sep 17 00:00:00 2001 From: Aart van Halteren Date: Thu, 15 Sep 2022 21:55:49 +0200 Subject: [PATCH] Catch date error --- app/models/entsoe.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/app/models/entsoe.rb b/app/models/entsoe.rb index 337803d..f180f20 100644 --- a/app/models/entsoe.rb +++ b/app/models/entsoe.rb @@ -105,11 +105,15 @@ class Entsoe doc = Nokogiri::XML(URI.open(url)) prices = doc.xpath('.//xmlns:Point').map{|p| parse_point(p)} - # get start_time (in UTC) from XML docment - start_time = DateTime.parse(doc.xpath('.//xmlns:period.timeInterval//xmlns:start').text) - - #returns a hash with keys formatted "yyyy-mm-dd hr" and values price (per kwh) - # tag runs from 1-24. We need hours from 00-23, therefore substracting 1 - prices.map{|p| [start_time.advance(hours: (p[0]-1)), p[1]]}.to_h + begin + # get start_time (in UTC) from XML docment + start_time = DateTime.parse(doc.xpath('.//xmlns:period.timeInterval//xmlns:start').text) + + #returns a hash with keys formatted "yyyy-mm-dd hr" and values price (per kwh) + # tag runs from 1-24. We need hours from 00-23, therefore substracting 1 + prices.map{|p| [start_time.advance(hours: (p[0]-1)), p[1]]}.to_h + rescue Date::Error => e + p e.message + end end end