diff --git a/lib/service/geo_calendar.rb b/lib/service/geo_calendar.rb new file mode 100644 index 000000000..5fe8744f1 --- /dev/null +++ b/lib/service/geo_calendar.rb @@ -0,0 +1,67 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +module Service + class GeoCalendar + include ApplicationLib + +=begin + +lookup calendar based on ip or hostname + + result = Service::GeoCalendar.location( '99.99.99.99' ) + +lookup calendar based on own system ip + + result = Service::GeoCalendar.location + +returns + + result = { + "name" => 'Country Name', + "timezone" => 'time zone of ip', + "business_hours" => { + "mon" => { + "active" => true, + "timeframes" => [["09:00","17:00"]] + }, + "tue" => { + "active" => true, + "timeframes" => [["09:00","17:00"]] + }, + "wed":{ + "active" => true, + "timeframes" => [["09:00","17:00"]] + }, + "thu":{ + "active" => true, + "timeframes" => [["09:00","17:00"]] + }, + "fri":{ + "active" => true, + "timeframes" => [["09:00","17:00"]] + }, + "sat":{ + "active" => false, + "timeframes" => [["09:00","17:00"]] + }, + "sun":{ + "active" => false, + "timeframes" => [["09:00","17:00"]] + } + }, + "ical_url" => "", + } + +=end + + def self.location(address = nil) + + # load backend + backend = load_adapter_by_setting( 'geo_calendar_backend' ) + return if !backend + + # db lookup + backend.location(address) + end + end +end diff --git a/lib/service/geo_calendar/zammad.rb b/lib/service/geo_calendar/zammad.rb new file mode 100644 index 000000000..d4ce1d4ef --- /dev/null +++ b/lib/service/geo_calendar/zammad.rb @@ -0,0 +1,44 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +require 'cache' + +class Service::GeoCalendar::Zammad + def self.location(address) + + # check cache + cache_key = "zammadgeocalendar::#{address}" + cache = Cache.get( cache_key ) + return cache if cache + + # do lookup + host = 'https://geo.zammad.com' + if address + url = "/calendar?ip=#{CGI.escape address}" + else + url = '/calendar' + end + data = {} + begin + response = UserAgent.get( + "#{host}#{url}", + {}, + { + json: true, + open_timeout: 2, + read_timeout: 4, + }, + ) + if !response.success? && response.code.to_s !~ /^40.$/ + fail "ERROR: #{response.code}/#{response.body}" + end + + data = response.data + + Cache.write( cache_key, data, { expires_in: 1.day } ) + rescue => e + Rails.logger.error "#{host}#{url}: #{e.inspect}" + Cache.write( cache_key, data, { expires_in: 1.minutes } ) + end + data + end +end