From 3e1d0253ca887a0b2d07a99a0f1a1a977d25f83c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B1=E5=8D=9A=E4=BA=9E?= Date: Thu, 18 Jul 2024 10:20:58 +0800 Subject: [PATCH] Update weather api url to https://opendata.cwa.gov.tw --- app/models/ruling_weather_setting.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/models/ruling_weather_setting.rb b/app/models/ruling_weather_setting.rb index a545457..3975bb0 100644 --- a/app/models/ruling_weather_setting.rb +++ b/app/models/ruling_weather_setting.rb @@ -3,6 +3,9 @@ class RulingWeatherSetting require 'json' include Mongoid::Document include Mongoid::Timestamps + + WEATHER_API_URL = "https://opendata.cwa.gov.tw".freeze + field :time_offset, type: String, default: "+8" field :location, type: String, default: "" field :dataid, type: String, default: "" @@ -37,9 +40,8 @@ class RulingWeatherSetting def get_now_info(custom_location=nil,custom_dataid=nil,custom_observatory_name=nil) time_now = DateTime.now.new_offset(self.time_offset) today = time_now.strftime("%Y-%m-%d") - host = "https://opendata.cwb.gov.tw" custom_dataid = custom_dataid || self.dataid - url = "#{host}/api/v1/rest/datastore/#{custom_dataid}.json" + url = "#{WEATHER_API_URL}/api/v1/rest/datastore/#{custom_dataid}.json" startt = "#{today}T00:00:00" endt = "#{today}T23:59:59" custom_location = custom_location || self.location @@ -111,7 +113,7 @@ class RulingWeatherSetting if timeout_flag rain = cache["rain"].to_f rescue 0.0 else - url2 = "https://opendata.cwb.gov.tw/api/v1/rest/datastore/O-A0002-001.json" + url2 = "#{WEATHER_API_URL}/api/v1/rest/datastore/O-A0002-001.json" begin res2 = net_http_get_response(URI.parse("#{url2}?#{data2.to_query}")) content2 = JSON.parse(res2.body) @@ -147,9 +149,8 @@ class RulingWeatherSetting def test time_now = DateTime.now.utc.new_offset(self.time_offset) today = time_now.strftime("%Y-%m-%d") - host = "https://opendata.cwb.gov.tw" custom_dataid = custom_dataid || self.dataid - url = "#{host}/api/v1/rest/datastore/#{custom_dataid}.json" + url = "#{WEATHER_API_URL}/api/v1/rest/datastore/#{custom_dataid}.json" startt = "#{today}T00:00:00" endt = "#{today}T23:59:59" custom_location = custom_location || self.location