detailed_log_subscriber.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # frozen_string_literal: true
  2. module ActiveResource
  3. class DetailedLogSubscriber < ActiveSupport::LogSubscriber
  4. VERSION_EOL_WARNING_HEADER = 'x-shopify-api-version-warning'
  5. VERSION_DEPRECATION_HEADER = 'x-shopify-api-deprecated-reason'
  6. SHOPIFY_ACCESS_TOKEN = 'X-Shopify-Access-Token'
  7. FILTERED = '[FILTERED]'
  8. def request(event)
  9. log_request_response_details(event)
  10. warn_on_deprecated_header_or_version_eol_header(event)
  11. end
  12. def logger
  13. ActiveResource::Base.logger
  14. end
  15. private
  16. def log_request_response_details(event)
  17. data = event.payload[:data]
  18. headers = data.extract_options!
  19. headers[SHOPIFY_ACCESS_TOKEN] = FILTERED
  20. request_body = data.first
  21. info("Request:\n#{request_body}") if request_body
  22. info("Headers: #{headers.inspect}")
  23. info("Response:\n#{event.payload[:response].body}")
  24. end
  25. def warn_on_deprecated_header_or_version_eol_header(event)
  26. payload = event.payload
  27. payload[:response].each do |header_name, header_value|
  28. case header_name.downcase
  29. when VERSION_DEPRECATION_HEADER
  30. warning_message = <<-MSG
  31. [DEPRECATED] ShopifyAPI made a call to #{payload[:method].upcase} #{payload[:path]}, and this call made
  32. use of a deprecated endpoint, behaviour, or parameter. See #{header_value} for more details.
  33. MSG
  34. warn(warning_message)
  35. when VERSION_EOL_WARNING_HEADER
  36. warning_message = <<-MSG
  37. [API Version Warning] ShopifyAPI made a call to #{payload[:method].upcase} #{payload[:path]}, and this call used
  38. an API version that is unsupported or will become unsupported within 30 days. See #{header_value} for more details.
  39. MSG
  40. warn(warning_message)
  41. end
  42. end
  43. end
  44. end
  45. end