net_http_wrapper 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +4 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +65 -0
- data/README.md +75 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/net_http_wrapper.rb +61 -0
- data/lib/net_http_wrapper/version.rb +3 -0
- data/net_http_wrapper.gemspec +29 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1ba9d676fb9ac9e647a7dfe07c7af79e9259d20db79a3de283cedcb71b773786
|
4
|
+
data.tar.gz: 5c20e8bb7508fa43fa5fcd86cc052e08aab666da25a3f74d07965ef14c481571
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aa524bd45bb786f56c581423e82d30a9553242bbe71a5e1e0a7536ca0b20a2fbf1ecac7322e86905f0cb7d6378b9728fa6c71694b3b5cee0b5c9009aecb92353
|
7
|
+
data.tar.gz: 7ffcfe898f9e9c29b7077e560eef0e3b08425e3d5ddda261916eb9993e8c27a7bea9f8519caeb6f0c418aba8d07cebd4a5146b598cbfa28bbbd2ee5e9279ad24
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
net_http_wrapper (0.0.2)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
addressable (2.6.0)
|
10
|
+
public_suffix (>= 2.0.2, < 4.0)
|
11
|
+
ast (2.4.0)
|
12
|
+
crack (0.4.3)
|
13
|
+
safe_yaml (~> 1.0.0)
|
14
|
+
diff-lcs (1.3)
|
15
|
+
hashdiff (0.3.8)
|
16
|
+
jaro_winkler (1.5.2)
|
17
|
+
parallel (1.13.0)
|
18
|
+
parser (2.6.0.0)
|
19
|
+
ast (~> 2.4.0)
|
20
|
+
powerpack (0.1.2)
|
21
|
+
public_suffix (3.0.3)
|
22
|
+
rainbow (3.0.0)
|
23
|
+
rake (10.5.0)
|
24
|
+
rspec (3.8.0)
|
25
|
+
rspec-core (~> 3.8.0)
|
26
|
+
rspec-expectations (~> 3.8.0)
|
27
|
+
rspec-mocks (~> 3.8.0)
|
28
|
+
rspec-core (3.8.0)
|
29
|
+
rspec-support (~> 3.8.0)
|
30
|
+
rspec-expectations (3.8.2)
|
31
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
32
|
+
rspec-support (~> 3.8.0)
|
33
|
+
rspec-mocks (3.8.0)
|
34
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
35
|
+
rspec-support (~> 3.8.0)
|
36
|
+
rspec-support (3.8.0)
|
37
|
+
rubocop (0.64.0)
|
38
|
+
jaro_winkler (~> 1.5.1)
|
39
|
+
parallel (~> 1.10)
|
40
|
+
parser (>= 2.5, != 2.5.1.1)
|
41
|
+
powerpack (~> 0.1)
|
42
|
+
rainbow (>= 2.2.2, < 4.0)
|
43
|
+
ruby-progressbar (~> 1.7)
|
44
|
+
unicode-display_width (~> 1.4.0)
|
45
|
+
ruby-progressbar (1.10.0)
|
46
|
+
safe_yaml (1.0.4)
|
47
|
+
unicode-display_width (1.4.1)
|
48
|
+
webmock (3.5.1)
|
49
|
+
addressable (>= 2.3.6)
|
50
|
+
crack (>= 0.3.2)
|
51
|
+
hashdiff
|
52
|
+
|
53
|
+
PLATFORMS
|
54
|
+
ruby
|
55
|
+
|
56
|
+
DEPENDENCIES
|
57
|
+
bundler (~> 2.0)
|
58
|
+
net_http_wrapper!
|
59
|
+
rake (~> 10.0)
|
60
|
+
rspec (~> 3.0)
|
61
|
+
rubocop
|
62
|
+
webmock (~> 3.5.1)
|
63
|
+
|
64
|
+
BUNDLED WITH
|
65
|
+
2.0.1
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# NetHttpWrapper
|
2
|
+
[![Build Status](https://travis-ci.org/DimaSamodurov/net_http_wrapper.svg?branch=master)](https://travis-ci.org/DimaSamodurov/net_http_wrapper)
|
3
|
+
[![Code Climate](https://api.codeclimate.com/v1/badges/83512d69c7a20acd6582/maintainability)](https://codeclimate.com/github/DimaSamodurov/net_http_wrapper/maintainability)
|
4
|
+
|
5
|
+
NetHttpWrapper adds callbacks to Net::HTTP requests.
|
6
|
+
|
7
|
+
It can be used e.g. for logging purposes,
|
8
|
+
but the library does not make any assumptions on how to do this.
|
9
|
+
It does not add neither additional dependency nor convention to your project.
|
10
|
+
|
11
|
+
You can use Rails logger, external service or just print to STDOUT,
|
12
|
+
using format you need.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'net_http_wrapper'
|
20
|
+
```
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Register the `after_request` callback as follows:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
NetHttpWrapper.after_request do |http:, request:, response:, start_time:|
|
28
|
+
# log request/duration, store metrics, analyze response body etc.
|
29
|
+
end
|
30
|
+
```
|
31
|
+
You can add multiple callbacks.
|
32
|
+
Callbacks will be called in the same order as registered.
|
33
|
+
|
34
|
+
Note: you are responsible for error handling within the block!
|
35
|
+
|
36
|
+
Enable the callbacks invocation (initially disabled):
|
37
|
+
```ruby
|
38
|
+
NetHttpWrapper.enable
|
39
|
+
```
|
40
|
+
|
41
|
+
### Example
|
42
|
+
|
43
|
+
Snippet below can be added as an initializer to Rails application.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
NetHttpWrapper.enable
|
47
|
+
|
48
|
+
NetHttpWrapper.after_request do |http:, request:, response:, start_time:|
|
49
|
+
request_duration = (Time.now - start_time).round(3)
|
50
|
+
request_url =
|
51
|
+
URI.decode("http#{"s" if http.use_ssl?}://#{http.address}:#{http.port}#{request.path}")
|
52
|
+
|
53
|
+
Rails.logger.info(name: 'http_request',
|
54
|
+
method: request.method,
|
55
|
+
url: request_url,
|
56
|
+
status: response.code,
|
57
|
+
duration: request_duration)
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
## Development
|
62
|
+
|
63
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
64
|
+
Then, run `rake spec` to run the tests, and run `rubocop` to check code style.
|
65
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
66
|
+
|
67
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
68
|
+
To release a new version, update the version number in `version.rb`,
|
69
|
+
and then run `bundle exec rake release`, which will create a git tag for the version,
|
70
|
+
push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
Bug reports and pull requests are welcome on GitHub
|
75
|
+
at https://github.com/dimasamodurov/net_http_wrapper.
|
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_wrapper'
|
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,61 @@
|
|
1
|
+
require 'net_http_wrapper/version'
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
# Allows registering callbacks on `Net::HTTP#request` method.
|
5
|
+
module NetHttpWrapper
|
6
|
+
class Error < StandardError
|
7
|
+
end
|
8
|
+
|
9
|
+
# Defines methods to be available on NetHttpWrapper class.
|
10
|
+
module DSL
|
11
|
+
def enable
|
12
|
+
Net::HTTP.prepend NetHttpWrapper
|
13
|
+
@enabled = true
|
14
|
+
end
|
15
|
+
|
16
|
+
def disable
|
17
|
+
@enabled = false
|
18
|
+
end
|
19
|
+
|
20
|
+
def enabled?
|
21
|
+
@enabled
|
22
|
+
end
|
23
|
+
|
24
|
+
# Registers a callback that will be called upon request completion.
|
25
|
+
#
|
26
|
+
# Callback will receive following parameters:
|
27
|
+
# - http: HTTP instance,
|
28
|
+
# - request: HTTP Request,
|
29
|
+
# - response: HTTP Response,
|
30
|
+
# - start_time: Request Start Time
|
31
|
+
#
|
32
|
+
# Example:
|
33
|
+
# NetHttpWrapper.after_request do |http:, request:, response:, start_time:|
|
34
|
+
# # e.g. log request duration
|
35
|
+
# end
|
36
|
+
def after_request(&block)
|
37
|
+
after_request_callbacks << block
|
38
|
+
end
|
39
|
+
|
40
|
+
# List of registered callbacks to be executed upon the request completion.
|
41
|
+
def after_request_callbacks
|
42
|
+
@after_request_callbacks ||= []
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
extend DSL
|
47
|
+
|
48
|
+
def request(req, body = nil, &block)
|
49
|
+
start_time = Time.now
|
50
|
+
super.tap do |resp|
|
51
|
+
next unless NetHttpWrapper.enabled?
|
52
|
+
|
53
|
+
NetHttpWrapper.after_request_callbacks.each do |callback|
|
54
|
+
callback.call(http: self,
|
55
|
+
request: req,
|
56
|
+
response: resp,
|
57
|
+
start_time: start_time)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'net_http_wrapper/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'net_http_wrapper'
|
7
|
+
spec.version = NetHttpWrapper::VERSION
|
8
|
+
spec.authors = ['Dmytro Samodurov']
|
9
|
+
spec.email = ['dimasamodurov@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Net::HTTP request wrapper.'
|
12
|
+
spec.description = 'Wraps Net::HTTP requests e.g. for logging purposes.'
|
13
|
+
spec.homepage = 'https://github.com/dimasamodurov/net_http_wrapper'
|
14
|
+
|
15
|
+
# Specify which files should be added to the gem when it is released.
|
16
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
17
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
end
|
20
|
+
spec.bindir = 'bin'
|
21
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
27
|
+
spec.add_development_dependency 'webmock', '~> 3.5.1'
|
28
|
+
spec.add_development_dependency 'rubocop'
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: net_http_wrapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dmytro Samodurov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-02-13 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: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: webmock
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.5.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.5.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Wraps Net::HTTP requests e.g. for logging purposes.
|
84
|
+
email:
|
85
|
+
- dimasamodurov@gmail.com
|
86
|
+
executables:
|
87
|
+
- console
|
88
|
+
- setup
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- ".gitignore"
|
93
|
+
- ".rspec"
|
94
|
+
- ".rubocop.yml"
|
95
|
+
- ".travis.yml"
|
96
|
+
- Gemfile
|
97
|
+
- Gemfile.lock
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
100
|
+
- bin/console
|
101
|
+
- bin/setup
|
102
|
+
- lib/net_http_wrapper.rb
|
103
|
+
- lib/net_http_wrapper/version.rb
|
104
|
+
- net_http_wrapper.gemspec
|
105
|
+
homepage: https://github.com/dimasamodurov/net_http_wrapper
|
106
|
+
licenses: []
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubygems_version: 3.0.1
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: Net::HTTP request wrapper.
|
127
|
+
test_files: []
|