trabajo-afectivo/lib/service/geo_calendar/zammad.rb

46 lines
1 KiB
Ruby
Raw Normal View History

2016-10-19 03:11:36 +00:00
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
2015-09-22 14:49:57 +00:00
require 'cache'
class Service::GeoCalendar::Zammad
def self.location(address)
# check cache
cache_key = "zammadgeocalendar::#{address}"
cache = Cache.get(cache_key)
2015-09-22 14:49:57 +00:00
return cache if cache
# do lookup
host = 'https://geo.zammad.com'
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}",
{},
{
json: true,
open_timeout: 2,
read_timeout: 4,
total_timeout: 12,
2015-09-22 14:49:57 +00:00
},
)
if !response.success? && response.code.to_s !~ /^40.$/
2016-03-01 14:26:46 +00:00
raise "ERROR: #{response.code}/#{response.body}"
2015-09-22 14:49:57 +00:00
end
data = response.data
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}"
Cache.write(cache_key, data, { expires_in: 1.minute })
2015-09-22 14:49:57 +00:00
end
data
end
end