Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP adapter. #7

Merged
merged 2 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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