Sha256: 7875c89984749e3aef9f05ae4fe7cb7c42cf85563eeea9d6f835b3fbc3e14caf

Contents?: true

Size: 1.79 KB

Versions: 12

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module MyApiClient
  module Params
    # Description of Params
    class Params
      attr_reader :request, :response

      # Description of #initialize
      #
      # @param request [MyApiClient::Params::Request] describe_request_here
      # @param response [Sawyer::Response, nil] describe_response_here
      def initialize(request, response)
        @request = request
        @response = response
      end

      # Generate metadata for bugsnag.
      # It will integrate request and response params.
      # Blank parameter will be omitted.
      #
      # @return [Hash] Metadata for bugsnag
      def metadata
        request_metadata.merge(response_metadata)
      end
      alias to_bugsnag metadata

      # Returns contents as string for to be readable for human
      #
      # @return [String] Contents as string
      def inspect
        { request: request, response: response }.inspect
      end

      private

      # Generate metadata from request params.
      # It will be added prefix "request_".
      #
      # @return [Hash] Metadata for bugsnag
      def request_metadata
        if request.present?
          request.metadata.transform_keys { |key| :"request_#{key}" }
        else
          {}
        end
      end

      # Generate metadata from response params.
      # It will be added prefix "response_".
      #
      # @return [Hash] Metadata for bugsnag
      def response_metadata
        if response.present?
          data = response.data
          body = data.respond_to?(:to_h) ? data.to_h : data
          {
            response_status: response.status,
            response_headers: response.headers,
            response_body: body,
            duration: response.timing,
          }.compact
        else
          {}
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
my_api_client-1.1.0 lib/my_api_client/params/params.rb
my_api_client-1.0.0 lib/my_api_client/params/params.rb
my_api_client-0.27.0 lib/my_api_client/params/params.rb
my_api_client-0.26.0 lib/my_api_client/params/params.rb
my_api_client-0.25.0 lib/my_api_client/params/params.rb
my_api_client-0.24.0 lib/my_api_client/params/params.rb
my_api_client-0.23.0 lib/my_api_client/params/params.rb
my_api_client-0.22.0 lib/my_api_client/params/params.rb
my_api_client-0.21.0 lib/my_api_client/params/params.rb
my_api_client-0.20.0 lib/my_api_client/params/params.rb
my_api_client-0.19.0 lib/my_api_client/params/params.rb
my_api_client-0.18.0 lib/my_api_client/params/params.rb