Fix timeout bug.

This commit is contained in:
BoHung Chiu 2022-09-02 10:16:22 +08:00
parent d324d141d0
commit 1ef665007a
1 changed files with 12 additions and 2 deletions

View File

@ -24,6 +24,8 @@ module RulingBusesHelper
include HTTParty
base_uri 'https://ptx.transportdata.tw/MOTC/v2'
open_timeout 3
read_timeout 1
format :json
headers(
@ -125,7 +127,11 @@ module RulingBusesHelper
'(' + v.map{|r| "RouteName/#{bus_info.locale} eq '#{r}'"}.join(' or ') + '))')
end.join(' or ')
url = "/Bus/EstimatedTimeOfArrival/City/#{city}?$select=Direction,RouteName,NextBusTime&$filter=#{filters}&$format=JSON&$orderby=RouteName/#{locale}"
begin
bus_data = JSON.parse(self.get(url).body)
rescue Errno::EHOSTUNREACH, Net::ReadTimeout, Net::OpenTimeout
return nil
end
if bus_data.class==Hash
return nil
end
@ -135,7 +141,11 @@ module RulingBusesHelper
'(' + v.map{|r| "RouteName/#{bus_info.locale} eq '#{r}'"}.join(' or ') + '))')
end.join(' or ')
url2 = "/Bus/EstimatedTimeOfArrival/InterCity?$select=Direction,RouteName,NextBusTime&$filter=#{filters}&$format=JSON&$orderby=RouteName/#{locale}"
begin
pbus_data = JSON.parse(self.get(url2).body)
rescue Errno::EHOSTUNREACH, Net::ReadTimeout, Net::OpenTimeout
pbus_data = []
end
else
pbus_data = []
end