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

Create simple HTTP-logging middleware #28

Merged
merged 1 commit into from
Aug 11, 2020
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
2 changes: 2 additions & 0 deletions packages/ruby/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ gemspec
gem "rake", "~> 12.0"
gem "rspec", "~> 3.0"
gem "standard"
gem "rack-test"
gem "webmock"
24 changes: 24 additions & 0 deletions packages/ruby/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,32 @@ PATH
remote: .
specs:
readme-metrics (0.1.0)
httparty

GEM
remote: https://rubygems.org/
specs:
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
ast (2.4.1)
crack (0.4.3)
safe_yaml (~> 1.0.0)
diff-lcs (1.4.4)
hashdiff (1.0.1)
httparty (0.18.1)
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2020.0512)
multi_xml (0.6.0)
parallel (1.19.2)
parser (2.7.1.4)
ast (~> 2.4.1)
public_suffix (4.0.5)
rack (2.2.3)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rainbow (3.0.0)
rake (12.3.3)
regexp_parser (1.7.1)
Expand Down Expand Up @@ -42,19 +59,26 @@ GEM
rubocop-performance (1.6.1)
rubocop (>= 0.71.0)
ruby-progressbar (1.10.1)
safe_yaml (1.0.5)
standard (0.4.7)
rubocop (~> 0.85.0)
rubocop-performance (~> 1.6.0)
unicode-display_width (1.7.0)
webmock (3.8.3)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
ruby

DEPENDENCIES
rack-test
rake (~> 12.0)
readme-metrics!
rspec (~> 3.0)
standard
webmock

BUNDLED WITH
2.1.4
21 changes: 21 additions & 0 deletions packages/ruby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,24 @@ Track your API metrics within ReadMe.
## Installation

## Usage

`Readme::Metrics` is a Rack middleware and is compatible with all Rack-based
apps, including Rails.

### Rails

```ruby
# application.rb
require "readme/metrics"

config.middleware.use Readme::Metrics, "http://example.com/your/logging/api"
```

### Rack::Builder

```ruby
Rack::Builder.new do |builder|
builder.use Readme::Metrics, "http://example.com/your/logging/api"
builder.run your_app
end
```
15 changes: 12 additions & 3 deletions packages/ruby/lib/readme/metrics.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
require "readme/metrics/version"
require "httparty"

module Readme
module Metrics
class Error < StandardError; end
# Your code goes here...
class Metrics
def initialize(app, endpoint)
@app = app
@endpoint = endpoint
end

def call(env)
path = "#{env["REQUEST_METHOD"]} #{env["PATH_INFO"]}"
HTTParty.post(@endpoint, body: {path: path}.to_json)
@app.call(env)
end
end
end
2 changes: 1 addition & 1 deletion packages/ruby/lib/readme/metrics/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Readme
module Metrics
class Metrics
VERSION = "0.1.0"
end
end
2 changes: 2 additions & 0 deletions packages/ruby/readme-metrics.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ Gem::Specification.new do |spec|
# spec.bindir = "exe"
# spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_runtime_dependency "httparty"
end
47 changes: 47 additions & 0 deletions packages/ruby/spec/readme/metrics_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,52 @@
require "rack/test"
require "webmock/rspec"

RSpec.describe Readme::Metrics do
include Rack::Test::Methods

before do
stub_request(:post, readme_endpoint)
end

it "has a version number" do
expect(Readme::Metrics::VERSION).not_to be nil
end

it "doesn't modify the response" do
get "/"

response_without_middleware = noop_app.call(double)
response_with_middleware = mock_response_to_raw(last_response)

expect(response_with_middleware).to eq response_without_middleware
end

it "posts request urls to Readme API" do
get "/api/foo"
post "/api/bar"

expect(WebMock).to have_requested(:post, readme_endpoint)
.with(body: {path: "GET /api/foo"}.to_json)

expect(WebMock).to have_requested(:post, readme_endpoint)
.with(body: {path: "POST /api/bar"}.to_json)
end

def readme_endpoint
"http://example.com/"
end

def app
Readme::Metrics.new(noop_app, readme_endpoint)
end

def noop_app
lambda do |env|
[200, {"Content-Type" => "text/plain"}, ["OK"]]
end
end

def mock_response_to_raw(mock_response)
[mock_response.status, mock_response.headers, [mock_response.body]]
end
end