Sha256: db9c6d772d6501a676f006e662aeae4eeeba6ab0d2d3cf964dab4835f6715c0b

Contents?: true

Size: 1.74 KB

Versions: 5

Compression:

Stored size: 1.74 KB

Contents

# frozen_string_literal: true

module Sniffer
  module Adapters
    # HTTP adapter
    module PatronAdapter
      def self.included(base)
        base.class_eval do
          alias_method :request_without_sniffer, :request
          alias_method :request, :request_with_sniffer
        end
      end

      # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
      def request_with_sniffer(action_name, url, headers, options = {})
        if Sniffer.enabled?
          data_item = Sniffer::DataItem.new
          uri = URI(base_url)
          data_item.request = Sniffer::DataItem::Request.new(host: uri.host,
                                                             method: action_name,
                                                             query: url,
                                                             headers: headers.dup,
                                                             body: options[:data].to_s,
                                                             port: uri.port)

          Sniffer.store(data_item)
        end

        bm = Benchmark.realtime do
          @res = request_without_sniffer(action_name, url, headers, options)
        end

        if Sniffer.enabled?
          data_item.response = Sniffer::DataItem::Response.new(status: @res.status,
                                                               headers: @res.headers,
                                                               body: @res.body.to_s,
                                                               timing: bm)

          data_item.log
        end

        @res
      end
      # rubocop:enable Metrics/AbcSize,Metrics/MethodLength
    end
  end
end

::Patron::Session.send(:include, Sniffer::Adapters::PatronAdapter) if defined?(::Patron::Session)

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sniffer-0.3.2 lib/sniffer/adapters/patron_adapter.rb
sniffer-0.3.1 lib/sniffer/adapters/patron_adapter.rb
sniffer-0.3.0 lib/sniffer/adapters/patron_adapter.rb
sniffer-0.2.0 lib/sniffer/adapters/patron_adapter.rb
sniffer-0.1.0 lib/sniffer/adapters/patron_adapter.rb