Skip to content

Commit

Permalink
change implantation from Excon request to request_call
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlewap committed Dec 13, 2017
1 parent 67d6077 commit 3557dc0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
30 changes: 16 additions & 14 deletions lib/sniffer/adapters/excon_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,37 @@ module Adapters
module ExconAdapter
def self.included(base)
base.class_eval do
alias_method :request_without_sniffer, :request
alias_method :request, :request_with_sniffer
alias_method :request_without_sniffer, :request_call
alias_method :request_call, :request_with_sniffer
end
end

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def request_with_sniffer(params = {}, &block)
def request_with_sniffer(datum)
if Sniffer.enabled?
@data_item = Sniffer::DataItem.new
@data_item.response = Sniffer::DataItem::Response.new
query = data[:path]
query += "/?#{hash_to_query data[:query]}" if data[:query]
@data_item.request = Sniffer::DataItem::Request.new(host: data[:host],
method: params[:method]&.to_sym,
query = datum[:path]
query += "?#{hash_to_query datum[:query]}" if datum[:query]
@data_item.request = Sniffer::DataItem::Request.new(host: datum[:host],
method: datum[:method]&.to_sym,
query: query,
headers: data[:headers],
body: data[:body].to_s,
port: data[:port])
headers: datum[:headers],
body: datum[:body].to_s,
port: datum[:port])
Sniffer.store(@data_item)
end

bm = Benchmark.realtime do
@res = request_without_sniffer(params, &block)
@res = request_without_sniffer(datum)
end

if Sniffer.enabled?
@data_item.response = Sniffer::DataItem::Response.new(status: @res[:status],
headers: @res[:headers].collect.to_h,
body: @res[:body].to_s,
response_for_sniffer = Excon::Response.new(response(@res)[:response])
@data_item.response = Sniffer::DataItem::Response.new(status: response_for_sniffer.status,
headers: response_for_sniffer.headers.collect.to_h,
body: response_for_sniffer
.body.force_encoding(Encoding::UTF_8),
timing: bm)

@data_item.log
Expand Down
4 changes: 3 additions & 1 deletion spec/yaml/excon/post_response.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
---
:request:
:host: "localhost"
:query: /data/?lang=ruby
:query: /data?lang=ruby
:port: 4567
:headers:
user-agent: "excon/0.59.0"
host: "localhost:4567"
content-length: 11
:body: author=Matz
:method: :post
:port: 4567
Expand Down

0 comments on commit 3557dc0

Please sign in to comment.