This commit is contained in:
邱博亞 2024-07-18 10:20:58 +08:00
parent cc95469a1f
commit 3e1d0253ca
1 changed files with 6 additions and 5 deletions

View File

@ -3,6 +3,9 @@ class RulingWeatherSetting
require 'json' require 'json'
include Mongoid::Document include Mongoid::Document
include Mongoid::Timestamps include Mongoid::Timestamps
WEATHER_API_URL = "https://opendata.cwa.gov.tw".freeze
field :time_offset, type: String, default: "+8" field :time_offset, type: String, default: "+8"
field :location, type: String, default: "" field :location, type: String, default: ""
field :dataid, 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) def get_now_info(custom_location=nil,custom_dataid=nil,custom_observatory_name=nil)
time_now = DateTime.now.new_offset(self.time_offset) time_now = DateTime.now.new_offset(self.time_offset)
today = time_now.strftime("%Y-%m-%d") today = time_now.strftime("%Y-%m-%d")
host = "https://opendata.cwb.gov.tw"
custom_dataid = custom_dataid || self.dataid 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" startt = "#{today}T00:00:00"
endt = "#{today}T23:59:59" endt = "#{today}T23:59:59"
custom_location = custom_location || self.location custom_location = custom_location || self.location
@ -111,7 +113,7 @@ class RulingWeatherSetting
if timeout_flag if timeout_flag
rain = cache["rain"].to_f rescue 0.0 rain = cache["rain"].to_f rescue 0.0
else 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 begin
res2 = net_http_get_response(URI.parse("#{url2}?#{data2.to_query}")) res2 = net_http_get_response(URI.parse("#{url2}?#{data2.to_query}"))
content2 = JSON.parse(res2.body) content2 = JSON.parse(res2.body)
@ -147,9 +149,8 @@ class RulingWeatherSetting
def test def test
time_now = DateTime.now.utc.new_offset(self.time_offset) time_now = DateTime.now.utc.new_offset(self.time_offset)
today = time_now.strftime("%Y-%m-%d") today = time_now.strftime("%Y-%m-%d")
host = "https://opendata.cwb.gov.tw"
custom_dataid = custom_dataid || self.dataid 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" startt = "#{today}T00:00:00"
endt = "#{today}T23:59:59" endt = "#{today}T23:59:59"
custom_location = custom_location || self.location custom_location = custom_location || self.location