zammad.rb 1.0 KB

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