zammad.rb 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Service::GeoCalendar::Zammad
  3. def self.location(address)
  4. # check cache
  5. cache_key = "zammadgeocalendar::#{address}"
  6. cache = ::Rails.cache.read(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. verify_ssl: true,
  26. },
  27. )
  28. if !response.success? && response.code.to_s !~ %r{^40.$}
  29. raise "#{response.code}/#{response.body}"
  30. end
  31. data = response.data
  32. ::Rails.cache.write(cache_key, data, { expires_in: 30.minutes })
  33. rescue => e
  34. Rails.logger.error "#{host}#{url}: #{e.inspect}"
  35. ::Rails.cache.write(cache_key, data, { expires_in: 1.minute })
  36. end
  37. data
  38. end
  39. end