detailed_log_subscriber.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. def request(event)
  7. log_request_response_details(event)
  8. warn_on_deprecated_header_or_version_eol_header(event)
  9. end
  10. def logger
  11. ActiveResource::Base.logger
  12. end
  13. private
  14. def log_request_response_details(event)
  15. data = event.payload[:data]
  16. headers = data.extract_options!
  17. request_body = data.first
  18. info("Request:\n#{request_body}") if request_body
  19. info("Headers: #{headers.inspect}")
  20. info("Response:\n#{event.payload[:response].body}")
  21. end
  22. def warn_on_deprecated_header_or_version_eol_header(event)
  23. payload = event.payload
  24. payload[:response].each do |header_name, header_value|
  25. case header_name.downcase
  26. when VERSION_DEPRECATION_HEADER
  27. warning_message = <<-MSG
  28. [DEPRECATED] ShopifyAPI made a call to #{payload[:method].upcase} #{payload[:path]}, and this call made
  29. use of a deprecated endpoint, behaviour, or parameter. See #{header_value} for more details.
  30. MSG
  31. warn(warning_message)
  32. when VERSION_EOL_WARNING_HEADER
  33. warning_message = <<-MSG
  34. [API Version Warning] ShopifyAPI made a call to #{payload[:method].upcase} #{payload[:path]}, and this call used
  35. an API version that is unsupported or will become unsupported within 30 days. See #{header_value} for more details.
  36. MSG
  37. warn(warning_message)
  38. end
  39. end
  40. end
  41. end
  42. end
  43. ActiveResource::DetailedLogSubscriber.attach_to(:active_resource_detailed)