zammad.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. require 'cache'
  3. class Service::GeoCalendar::Zammad
  4. def self.location(address)
  5. # check cache
  6. cache_key = "zammadgeocalendar::#{address}"
  7. cache = Cache.get(cache_key)
  8. return cache if cache
  9. # do lookup
  10. host = 'https://geo.zammad.com'
  11. url = if address
  12. "/calendar?ip=#{CGI.escape address}"
  13. else
  14. '/calendar'
  15. end
  16. data = {}
  17. begin
  18. response = UserAgent.get(
  19. "#{host}#{url}",
  20. {},
  21. {
  22. json: true,
  23. open_timeout: 2,
  24. read_timeout: 4,
  25. total_timeout: 12,
  26. },
  27. )
  28. if !response.success? && response.code.to_s !~ /^40.$/
  29. raise "ERROR: #{response.code}/#{response.body}"
  30. end
  31. data = response.data
  32. Cache.write(cache_key, data, { expires_in: 30.minutes })
  33. rescue => e
  34. Rails.logger.error "#{host}#{url}: #{e.inspect}"
  35. Cache.write(cache_key, data, { expires_in: 1.minute })
  36. end
  37. data
  38. end
  39. end