Skip to content

Commit

Permalink
Add HTTP adapter. (#7)
Browse files Browse the repository at this point in the history
* Add HTTP adapter.

* Change list of arguments for overridden Net::HTTP#request.
  • Loading branch information
maxshend authored Apr 25, 2022
1 parent ccc3786 commit 33b8bfc
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ gem "database_cleaner-active_record", "~> 2.0"
gem "standard", "~> 1.3"
gem "simplecov", "~> 0.21.0"
gem "pry"
gem "webmock", "~> 3.14"

# Dummy app dependencies
gem "sqlite3", "~> 1.4"
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Change configuration in an initializer if you need:
IoToResponsePayloadRatio.configure do |config|
config.publish = :notifications # defaults to :logs
config.warn_threshold = 0.8 # defaults to 0
config.adapters = [:active_record, :net_http] # defaults to [:active_record]
end
```

Expand Down
3 changes: 2 additions & 1 deletion lib/io_to_response_payload_ratio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

require "io_to_response_payload_ratio/adapters/base_adapter"
require "io_to_response_payload_ratio/adapters/active_record_adapter"
require "io_to_response_payload_ratio/adapters/net_http_adapter"

require "io_to_response_payload_ratio/publishers/base_publisher"
require "io_to_response_payload_ratio/publishers/logs_publisher"
Expand All @@ -16,7 +17,7 @@

module IoToResponsePayloadRatio
NAMESPACE = :io_to_response_payload_ratio
ADAPTERS = [ActiveRecordAdapter].freeze
ADAPTERS = [ActiveRecordAdapter, NetHttpAdapter].freeze
PUBLISHERS = [LogsPublisher, NotificationsPublisher].freeze

class << self
Expand Down
24 changes: 24 additions & 0 deletions lib/io_to_response_payload_ratio/adapters/net_http_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require "net/http"
require "io_to_response_payload_ratio/patches/net_http_adapter_patch"

module IoToResponsePayloadRatio
class NetHttpAdapter < BaseAdapter
def self.kind
:net_http
end

def initialize!
ActiveSupport.on_load(:after_initialize) do
Net::HTTP.prepend(NetHttpAdapterPatch)

if defined?(::WebMock)
WebMock::HttpLibAdapters::NetHttpAdapter
.instance_variable_get(:@webMockNetHTTP)
.prepend(NetHttpAdapterPatch)
end
end
end
end
end
13 changes: 13 additions & 0 deletions lib/io_to_response_payload_ratio/patches/net_http_adapter_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module IoToResponsePayloadRatio
module NetHttpAdapterPatch
def request(*args, &block)
super do |response|
if response.body && IoToResponsePayloadRatio.aggregator.active?
IoToResponsePayloadRatio.aggregator.increment(NetHttpAdapter.kind, response.body.bytesize)
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

RSpec.describe IoToResponsePayloadRatio::NetHttpAdapter do
let(:aggregator) { IoToResponsePayloadRatio.aggregator }
let(:body) { "Hello, World!" }
let(:url) { "www.example.com" }

before do
stub_request(:get, url).to_return body: body
end

before do
aggregator.start!
end

after do
aggregator.stop!
end

context "when aggregator is inactive" do
before do
aggregator.stop!
end

it "does nothing" do
expect(aggregator).not_to receive(:increment)

Net::HTTP.get url, "/"
end
end

it "increments aggregator by request's body bytesize" do
allow(aggregator).to receive(:increment)

Net::HTTP.get url, "/"

expect(aggregator).to have_received(:increment).with(described_class.kind, body.bytesize)
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "webmock/rspec"
require "simplecov"
SimpleCov.start if ENV["COVERAGE"]

Expand Down

0 comments on commit 33b8bfc

Please sign in to comment.