Sha256: a845ef466ce2e8b11f4240a5f1ec4644c8ce92bb3f07a8879450e19ef913dcb3

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true
module ActiveResource
  class DetailedLogSubscriber < ActiveSupport::LogSubscriber
    VERSION_EOL_WARNING_HEADER = 'x-shopify-api-version-warning'
    VERSION_DEPRECATION_HEADER = 'x-shopify-api-deprecated-reason'
    def request(event)
      log_request_response_details(event)
      warn_on_deprecated_header_or_version_eol_header(event)
    end

    def logger
      ActiveResource::Base.logger
    end

    private

    def log_request_response_details(event)
      data = event.payload[:data]
      headers = data.extract_options!
      request_body = data.first

      info("Request:\n#{request_body}") if request_body
      info("Headers: #{headers.inspect}")
      info("Response:\n#{event.payload[:response].body}")
    end

    def warn_on_deprecated_header_or_version_eol_header(event)
      payload = event.payload

      payload[:response].each do |header_name, header_value|
        case header_name.downcase
        when VERSION_DEPRECATION_HEADER
          warning_message = <<-MSG
          [DEPRECATED] ShopifyAPI made a call to #{payload[:method].upcase} #{payload[:path]}, and this call made
          use of a deprecated endpoint, behaviour, or parameter. See #{header_value} for more details.
          MSG

          warn(warning_message)

        when VERSION_EOL_WARNING_HEADER
          warning_message = <<-MSG
          [API Version Warning] ShopifyAPI made a call to #{payload[:method].upcase} #{payload[:path]}, and this call used
          an API version that is unsupported or will become unsupported within 30 days. See #{header_value} for more details.
          MSG

          warn(warning_message)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
shopify_api-9.4.0 lib/active_resource/detailed_log_subscriber.rb
shopify_api-9.3.0 lib/active_resource/detailed_log_subscriber.rb