net-http-middleware 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/.rubocop.yml +63 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +71 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/net/http/middleware.rb +23 -0
- data/lib/net/http/middleware/chain.rb +40 -0
- data/lib/net/http/middleware/version.rb +7 -0
- data/net-http-middleware.gemspec +28 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 48321c6f01487502d42ecc2281b509f1df98e316
|
4
|
+
data.tar.gz: 8472889c77958206005227a3664aa140cb162594
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 93bb6d00b605dab5c7b14492ea3a6a71f4624b0b6df2675e1fb36fffeb7e59dabd6178c231a1b5a5944df06e6af17969b010450095882a8e8ea8406510dd9b8d
|
7
|
+
data.tar.gz: 051de91fe772ef6c5495110194cd309dac827daa5bf549b5b5d1fd718f3fd6aa672d9b9dda201859a20f78a0e7582a5351b5d228d8bd54b49445d2ce16797155
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.3
|
3
|
+
DisplayCopNames: true
|
4
|
+
Exclude:
|
5
|
+
- bin/*
|
6
|
+
- spec/spec_helper.rb
|
7
|
+
|
8
|
+
Metrics/LineLength:
|
9
|
+
Max: 120
|
10
|
+
|
11
|
+
Metrics/ParameterLists:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Metrics/BlockLength:
|
15
|
+
Exclude:
|
16
|
+
- spec/**/*
|
17
|
+
- net-http-middleware.gemspec
|
18
|
+
|
19
|
+
Style/Alias:
|
20
|
+
EnforcedStyle: prefer_alias_method
|
21
|
+
|
22
|
+
Layout/AlignParameters:
|
23
|
+
EnforcedStyle: with_fixed_indentation
|
24
|
+
|
25
|
+
Style/BlockDelimiters:
|
26
|
+
EnforcedStyle: braces_for_chaining
|
27
|
+
|
28
|
+
Style/Documentation:
|
29
|
+
Enabled: false
|
30
|
+
|
31
|
+
Style/DoubleNegation:
|
32
|
+
Enabled: false
|
33
|
+
|
34
|
+
Layout/FirstParameterIndentation:
|
35
|
+
EnforcedStyle: consistent
|
36
|
+
|
37
|
+
Style/FrozenStringLiteralComment:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
Style/GuardClause:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
Style/EmptyMethod:
|
44
|
+
EnforcedStyle: expanded
|
45
|
+
|
46
|
+
Style/IfUnlessModifier:
|
47
|
+
Enabled: false
|
48
|
+
|
49
|
+
Layout/IndentArray:
|
50
|
+
EnforcedStyle: consistent
|
51
|
+
|
52
|
+
Layout/IndentHash:
|
53
|
+
EnforcedStyle: consistent
|
54
|
+
|
55
|
+
Style/ModuleFunction:
|
56
|
+
Enabled: false
|
57
|
+
|
58
|
+
Style/RescueModifier:
|
59
|
+
Exclude:
|
60
|
+
- spec/**/*
|
61
|
+
|
62
|
+
Style/TrailingUnderscoreVariable:
|
63
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Ivan Shamatov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# Net::Http::Middleware
|
2
|
+
|
3
|
+
Middlewares for Net::HTTP request method.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
# Example of tracer, which will add header to each outgoing request
|
9
|
+
class OpenTraceId
|
10
|
+
def initialize(chain)
|
11
|
+
@chain = chain
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(req, body = nil, &block)
|
15
|
+
request.add_field('X-Custom-CrossService-RequestId', SecureRandom.uuid)
|
16
|
+
@chain.call(req, body, block)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Example of Loging for
|
21
|
+
class LogRequest
|
22
|
+
def initialize(chain, logger)
|
23
|
+
@chain = chain
|
24
|
+
@logger = logger
|
25
|
+
end
|
26
|
+
|
27
|
+
def call(req, body = nil, &block)
|
28
|
+
@logger.info('We log before')
|
29
|
+
response = @chain.call(req, body, block)
|
30
|
+
@logger.info('We log after')
|
31
|
+
response
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Adding middlewares to configurator
|
36
|
+
Net::HTTP.configure_middleware do |chain|
|
37
|
+
chain.use LogRequest, Logger.new(STDOUT)
|
38
|
+
chain.use OpenTraceId
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
|
43
|
+
## Installation
|
44
|
+
|
45
|
+
Add this line to your application's Gemfile:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
gem 'net-http-middleware'
|
49
|
+
```
|
50
|
+
|
51
|
+
And then execute:
|
52
|
+
|
53
|
+
$ bundle
|
54
|
+
|
55
|
+
Or install it yourself as:
|
56
|
+
|
57
|
+
$ gem install net-http-middleware
|
58
|
+
|
59
|
+
## Development
|
60
|
+
|
61
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
62
|
+
|
63
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/net-http-middleware.
|
68
|
+
|
69
|
+
## License
|
70
|
+
|
71
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'net/http/middleware'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'net/http/middleware/version'
|
3
|
+
require 'net/http/middleware/chain'
|
4
|
+
|
5
|
+
module Net
|
6
|
+
class HTTP
|
7
|
+
class << self
|
8
|
+
attr_accessor :middleware_chain
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.configure_middleware(&block)
|
12
|
+
@middleware_chain ||= Net::HTTP::Middleware::Chain.new(&block)
|
13
|
+
end
|
14
|
+
|
15
|
+
alias_method(:orig_request, :request) unless method_defined?(:orig_request)
|
16
|
+
|
17
|
+
def request(req, body = nil, &block)
|
18
|
+
Net::HTTP.middleware_chain.call(req, body = nil) do
|
19
|
+
orig_request(req, body, &block)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Net
|
2
|
+
class HTTP
|
3
|
+
module Middleware
|
4
|
+
class Chain
|
5
|
+
attr_accessor :stack
|
6
|
+
|
7
|
+
def initialize(&block)
|
8
|
+
@stack = []
|
9
|
+
instance_eval(&block) if block_given?
|
10
|
+
end
|
11
|
+
|
12
|
+
def use(middleware, *args)
|
13
|
+
stack << [middleware, args]
|
14
|
+
end
|
15
|
+
|
16
|
+
def call(request, body = nil, &on_finish)
|
17
|
+
build_call_chain(stack.dup, on_finish).call(request, body)
|
18
|
+
end
|
19
|
+
|
20
|
+
private def build_call_chain(stack, on_finish)
|
21
|
+
stack.reverse.reduce(FinalizeMiddleware.new(on_finish)) do |chain, current_middleware|
|
22
|
+
klass, args = current_middleware
|
23
|
+
klass.new(chain, *args)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Final middleware which will execute orig_request
|
28
|
+
class FinalizeMiddleware
|
29
|
+
def initialize(on_finish)
|
30
|
+
@on_finish = on_finish
|
31
|
+
end
|
32
|
+
|
33
|
+
def call(_req, _body = nil)
|
34
|
+
@on_finish.call
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'net/http/middleware/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'net-http-middleware'
|
9
|
+
spec.version = Net::Http::Middleware::VERSION
|
10
|
+
spec.authors = ['Ivan Shamatov']
|
11
|
+
spec.email = ['status.enable@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'Middleware-available Net::HTTP monkey patch'
|
14
|
+
spec.description = 'Implementation of `chain of responsibility`-pattern for Net::HTTP request method.'
|
15
|
+
spec.homepage = 'https://github.com/IvanShamatov/net-http-middleware'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: net-http-middleware
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ivan Shamatov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.15'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.15'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: Implementation of `chain of responsibility`-pattern for Net::HTTP request
|
56
|
+
method.
|
57
|
+
email:
|
58
|
+
- status.enable@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".rubocop.yml"
|
66
|
+
- ".travis.yml"
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/net/http/middleware.rb
|
74
|
+
- lib/net/http/middleware/chain.rb
|
75
|
+
- lib/net/http/middleware/version.rb
|
76
|
+
- net-http-middleware.gemspec
|
77
|
+
homepage: https://github.com/IvanShamatov/net-http-middleware
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.6.11
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Middleware-available Net::HTTP monkey patch
|
101
|
+
test_files: []
|