2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
2015-09-22 14:49:57 +00:00
|
|
|
|
|
|
|
class Service::GeoCalendar::Zammad
|
|
|
|
def self.location(address)
|
|
|
|
|
|
|
|
# check cache
|
|
|
|
cache_key = "zammadgeocalendar::#{address}"
|
2021-05-31 13:05:54 +00:00
|
|
|
cache = ::Cache.read(cache_key)
|
2015-09-22 14:49:57 +00:00
|
|
|
return cache if cache
|
|
|
|
|
|
|
|
# do lookup
|
|
|
|
host = 'https://geo.zammad.com'
|
2016-01-15 17:22:57 +00:00
|
|
|
url = if address
|
|
|
|
"/calendar?ip=#{CGI.escape address}"
|
|
|
|
else
|
|
|
|
'/calendar'
|
|
|
|
end
|
2015-09-22 14:49:57 +00:00
|
|
|
data = {}
|
|
|
|
begin
|
|
|
|
response = UserAgent.get(
|
|
|
|
"#{host}#{url}",
|
|
|
|
{},
|
|
|
|
{
|
2018-12-19 17:31:51 +00:00
|
|
|
json: true,
|
|
|
|
open_timeout: 2,
|
|
|
|
read_timeout: 4,
|
2016-01-21 01:07:50 +00:00
|
|
|
total_timeout: 12,
|
2015-09-22 14:49:57 +00:00
|
|
|
},
|
|
|
|
)
|
2021-05-12 11:37:44 +00:00
|
|
|
if !response.success? && response.code.to_s !~ %r{^40.$}
|
2020-03-12 08:23:19 +00:00
|
|
|
raise "#{response.code}/#{response.body}"
|
2015-09-22 14:49:57 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
data = response.data
|
|
|
|
|
2018-04-26 08:55:53 +00:00
|
|
|
::Cache.write(cache_key, data, { expires_in: 30.minutes })
|
2015-09-22 14:49:57 +00:00
|
|
|
rescue => e
|
|
|
|
Rails.logger.error "#{host}#{url}: #{e.inspect}"
|
2018-04-26 08:55:53 +00:00
|
|
|
::Cache.write(cache_key, data, { expires_in: 1.minute })
|
2015-09-22 14:49:57 +00:00
|
|
|
end
|
|
|
|
data
|
|
|
|
end
|
|
|
|
end
|