faraday-gzip 2.0.1 → 3.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +19 -0
- data/README.md +20 -16
- data/lib/faraday/gzip/middleware.rb +46 -21
- data/lib/faraday/gzip/version.rb +1 -1
- metadata +15 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5183c6f188272ea7e213adf03bb2d637c348e9f273b1b8829255f135a95b8c92
|
4
|
+
data.tar.gz: ce73797d7aa191ef07c376af0193ea704a653237146affec2806031194d064c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f9add78a8ff2c274ef021ae4e8ebfc1f525847ba4d05ca6ccf52cba1bc9f9d891e4627a354fdbf724393cb420e1225c33521d55f15f0c292490c8466e6ba202
|
7
|
+
data.tar.gz: 2edf0cd82a52d2bf33d9024ed3363b4735dc7a7645f847bf1aca250947d14663d696d2bc64bb10fc83019a677a2b701d933766ed1afcc7f76d554a30b4e6c5f9
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,24 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 3.0.4 (06-Apr-2025)
|
4
|
+
|
5
|
+
* Require StringIO that might not always be readily available
|
6
|
+
|
7
|
+
## 3.0.3 (25-Feb-2025)
|
8
|
+
|
9
|
+
* Minor code fixes, make some methods more solid
|
10
|
+
|
11
|
+
## 3.0.1 / 3.0.2 (01-Nov-2024)
|
12
|
+
|
13
|
+
* Minor fixes in gemspec
|
14
|
+
|
15
|
+
## 3.0.0 (29-Oct-2024)
|
16
|
+
|
17
|
+
* **Breaking change**: Drop support for Ruby 2, require 3.0+
|
18
|
+
* **Breaking change**: Drop support for Faraday v1. If you need to support Faraday v1, stay on [faraday-gzip version 2](https://github.com/bodrovis/faraday-gzip/tree/v2).
|
19
|
+
* Various code tweaks
|
20
|
+
* Remove JRuby 9.3 from CI matrix
|
21
|
+
|
3
22
|
## 2.0.1 (02-Jan-2024)
|
4
23
|
|
5
24
|
* Handle cases when body is `nil` (thanks, @bendangelo)
|
data/README.md
CHANGED
@@ -2,54 +2,58 @@
|
|
2
2
|
|
3
3
|

|
4
4
|
[](https://rubygems.org/gems/faraday-gzip)
|
5
|
+

|
5
6
|
|
6
|
-
The `Gzip` middleware for Faraday 1 and 2 adds the necessary `Accept-Encoding` headers and automatically decompresses the response. If the "Accept-Encoding" header
|
7
|
+
The `Gzip` middleware for Faraday 1 and 2 adds the necessary `Accept-Encoding` headers and automatically decompresses the response. If the "Accept-Encoding" header isn't set in the request, it defaults to `gzip,deflate` and appropriately handles the server's compressed response. This functionality resembles what Ruby does internally in `Net::HTTP#get`. If [Brotli](https://github.com/miyucy/brotli) is included in your Gemfile, the middleware also adds `br` to the header for Brotli support.
|
7
8
|
|
8
9
|
## Prerequisites
|
9
10
|
|
10
|
-
|
11
|
+
* faraday-gzip v3 supports only Faraday v2 and is tested with Ruby 3.0+ and JRuby 9.4
|
12
|
+
* [faraday-gzip v2](https://github.com/bodrovis/faraday-gzip/tree/v2) supports Faraday v1 and v2 and is tested with Ruby 2.7+ and JRuby 9.4.
|
11
13
|
|
12
14
|
## Installation
|
13
15
|
|
14
16
|
Add this line to your application's Gemfile:
|
15
17
|
|
16
18
|
```ruby
|
17
|
-
gem 'faraday-gzip'
|
19
|
+
gem 'faraday-gzip', '~> 3'
|
18
20
|
```
|
19
21
|
|
20
22
|
And then execute:
|
21
23
|
|
22
|
-
```
|
24
|
+
```
|
23
25
|
bundle install
|
24
26
|
```
|
25
27
|
|
26
28
|
Or install it yourself as:
|
27
29
|
|
28
|
-
```
|
30
|
+
```
|
29
31
|
gem install faraday-gzip
|
30
32
|
```
|
31
33
|
|
32
34
|
## Usage
|
33
35
|
|
36
|
+
To enable the middleware in your Faraday connection, add it as shown below:
|
37
|
+
|
34
38
|
```ruby
|
35
|
-
require 'faraday/gzip' # <===
|
39
|
+
require 'faraday/gzip' # <=== Add this line
|
36
40
|
|
37
41
|
conn = Faraday.new(...) do |f|
|
38
|
-
f.request :gzip # <===
|
39
|
-
|
42
|
+
f.request :gzip # <=== Add this line
|
43
|
+
# Additional configuration...
|
40
44
|
end
|
41
45
|
```
|
42
46
|
|
43
47
|
## Development
|
44
48
|
|
45
|
-
|
46
|
-
|
47
|
-
Then, run `bin/test` to run the tests.
|
48
|
-
|
49
|
-
To install this gem onto your local machine, run `rake build`.
|
49
|
+
To contribute or make changes:
|
50
50
|
|
51
|
-
|
52
|
-
|
51
|
+
* Clone the repo
|
52
|
+
* Run `bundle` to install dependencies
|
53
|
+
* Implement your feature
|
54
|
+
* Write and run tests using `rspec .`
|
55
|
+
* Use rake build to build the gem locally if needed
|
56
|
+
* Create a new PR with your changes
|
53
57
|
|
54
58
|
## Contributing
|
55
59
|
|
@@ -57,4 +61,4 @@ Bug reports and pull requests are welcome on GitHub.
|
|
57
61
|
|
58
62
|
## License
|
59
63
|
|
60
|
-
|
64
|
+
This gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -1,16 +1,19 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'zlib'
|
4
|
+
require 'stringio'
|
4
5
|
|
6
|
+
# Middleware to automatically decompress response bodies. If the
|
7
|
+
# "Accept-Encoding" header wasn't set in the request, this sets it to
|
8
|
+
# "gzip,deflate" and appropriately handles the compressed response from the
|
9
|
+
# server. This resembles what Ruby 1.9+ does internally in Net::HTTP#get.
|
10
|
+
# Based on https://github.com/lostisland/faraday_middleware/blob/main/lib/faraday_middleware/gzip.rb
|
5
11
|
module Faraday
|
12
|
+
# Main module
|
6
13
|
module Gzip
|
7
|
-
#
|
8
|
-
# "Accept-Encoding" header wasn't set in the request, this sets it to
|
9
|
-
# "gzip,deflate" and appropriately handles the compressed response from the
|
10
|
-
# server. This resembles what Ruby 1.9+ does internally in Net::HTTP#get.
|
11
|
-
# Based on https://github.com/lostisland/faraday_middleware/blob/main/lib/faraday_middleware/gzip.rb
|
12
|
-
|
14
|
+
# Faraday middleware for decompression
|
13
15
|
class Middleware < Faraday::Middleware
|
16
|
+
# System method required by Faraday
|
14
17
|
def self.optional_dependency(lib = nil)
|
15
18
|
lib ? require(lib) : yield
|
16
19
|
true
|
@@ -20,6 +23,8 @@ module Faraday
|
|
20
23
|
|
21
24
|
BROTLI_SUPPORTED = optional_dependency 'brotli'
|
22
25
|
|
26
|
+
# Returns supported encodings, adds brotli if the corresponding
|
27
|
+
# dependency is present
|
23
28
|
def self.supported_encodings
|
24
29
|
encodings = %w[gzip deflate]
|
25
30
|
encodings << 'br' if BROTLI_SUPPORTED
|
@@ -31,37 +36,46 @@ module Faraday
|
|
31
36
|
CONTENT_LENGTH = 'Content-Length'
|
32
37
|
SUPPORTED_ENCODINGS = supported_encodings.join(',').freeze
|
33
38
|
|
39
|
+
# Main method to process the response
|
34
40
|
def call(env)
|
35
41
|
env[:request_headers][ACCEPT_ENCODING] ||= SUPPORTED_ENCODINGS
|
42
|
+
|
36
43
|
@app.call(env).on_complete do |response_env|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
end
|
48
|
-
end
|
44
|
+
reset_body(response_env, find_processor(response_env))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Finds a proper processor
|
49
|
+
def find_processor(response_env)
|
50
|
+
if empty_body?(response_env)
|
51
|
+
->(body) { raw_body(body) }
|
52
|
+
else
|
53
|
+
processors[response_env[:response_headers][CONTENT_ENCODING]]
|
49
54
|
end
|
50
55
|
end
|
51
56
|
|
52
|
-
|
53
|
-
|
57
|
+
# Calls the proper processor to decompress body
|
58
|
+
def reset_body(env, processor)
|
59
|
+
return if processor.nil?
|
60
|
+
|
61
|
+
env[:body] = processor.call(env[:body])
|
54
62
|
env[:response_headers].delete(CONTENT_ENCODING)
|
55
63
|
|
56
64
|
env[:response_headers][CONTENT_LENGTH] = env[:body].nil? ? 0 : env[:body].length
|
57
65
|
end
|
58
66
|
|
67
|
+
# Process gzip
|
59
68
|
def uncompress_gzip(body)
|
60
69
|
io = StringIO.new(body)
|
61
70
|
gzip_reader = Zlib::GzipReader.new(io, encoding: 'ASCII-8BIT')
|
62
|
-
|
71
|
+
begin
|
72
|
+
gzip_reader.read
|
73
|
+
ensure
|
74
|
+
gzip_reader.close
|
75
|
+
end
|
63
76
|
end
|
64
77
|
|
78
|
+
# Process deflate
|
65
79
|
def inflate(body)
|
66
80
|
# Inflate as a DEFLATE (RFC 1950+RFC 1951) stream
|
67
81
|
Zlib::Inflate.inflate(body)
|
@@ -76,10 +90,12 @@ module Faraday
|
|
76
90
|
end
|
77
91
|
end
|
78
92
|
|
93
|
+
# Process brotli
|
79
94
|
def brotli_inflate(body)
|
80
95
|
Brotli.inflate(body)
|
81
96
|
end
|
82
97
|
|
98
|
+
# Do not process anything, leave body as is
|
83
99
|
def raw_body(body)
|
84
100
|
body
|
85
101
|
end
|
@@ -89,6 +105,15 @@ module Faraday
|
|
89
105
|
def empty_body?(response_env)
|
90
106
|
response_env[:body].nil? || response_env[:body].empty?
|
91
107
|
end
|
108
|
+
|
109
|
+
# Method providing the processors
|
110
|
+
def processors
|
111
|
+
@processors ||= {
|
112
|
+
'gzip' => ->(body) { uncompress_gzip(body) },
|
113
|
+
'deflate' => ->(body) { inflate(body) },
|
114
|
+
'br' => ->(body) { brotli_inflate(body) }
|
115
|
+
}
|
116
|
+
end
|
92
117
|
end
|
93
118
|
end
|
94
119
|
end
|
data/lib/faraday/gzip/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faraday-gzip
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ilya Krukowski
|
8
|
-
autorequire:
|
9
8
|
bindir: bin
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: faraday
|
@@ -16,14 +15,20 @@ dependencies:
|
|
16
15
|
requirements:
|
17
16
|
- - ">="
|
18
17
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
18
|
+
version: '2.0'
|
19
|
+
- - "<"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3'
|
20
22
|
type: :runtime
|
21
23
|
prerelease: false
|
22
24
|
version_requirements: !ruby/object:Gem::Requirement
|
23
25
|
requirements:
|
24
26
|
- - ">="
|
25
27
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
28
|
+
version: '2.0'
|
29
|
+
- - "<"
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: '3'
|
27
32
|
- !ruby/object:Gem::Dependency
|
28
33
|
name: zlib
|
29
34
|
requirement: !ruby/object:Gem::Requirement
|
@@ -108,20 +113,6 @@ dependencies:
|
|
108
113
|
- - "~>"
|
109
114
|
- !ruby/object:Gem::Version
|
110
115
|
version: '1.32'
|
111
|
-
- !ruby/object:Gem::Dependency
|
112
|
-
name: rubocop-packaging
|
113
|
-
requirement: !ruby/object:Gem::Requirement
|
114
|
-
requirements:
|
115
|
-
- - "~>"
|
116
|
-
- !ruby/object:Gem::Version
|
117
|
-
version: 0.5.0
|
118
|
-
type: :development
|
119
|
-
prerelease: false
|
120
|
-
version_requirements: !ruby/object:Gem::Requirement
|
121
|
-
requirements:
|
122
|
-
- - "~>"
|
123
|
-
- !ruby/object:Gem::Version
|
124
|
-
version: 0.5.0
|
125
116
|
- !ruby/object:Gem::Dependency
|
126
117
|
name: rubocop-performance
|
127
118
|
requirement: !ruby/object:Gem::Requirement
|
@@ -142,14 +133,14 @@ dependencies:
|
|
142
133
|
requirements:
|
143
134
|
- - "~>"
|
144
135
|
- !ruby/object:Gem::Version
|
145
|
-
version: '
|
136
|
+
version: '3.0'
|
146
137
|
type: :development
|
147
138
|
prerelease: false
|
148
139
|
version_requirements: !ruby/object:Gem::Requirement
|
149
140
|
requirements:
|
150
141
|
- - "~>"
|
151
142
|
- !ruby/object:Gem::Version
|
152
|
-
version: '
|
143
|
+
version: '3.0'
|
153
144
|
description: 'Faraday plugin to automatically set compression headers (GZip, Deflate,
|
154
145
|
Brotli) and decompress the response.
|
155
146
|
|
@@ -172,11 +163,9 @@ licenses:
|
|
172
163
|
metadata:
|
173
164
|
bug_tracker_uri: https://github.com/bodrovis/faraday-gzip/issues
|
174
165
|
changelog_uri: https://github.com/bodrovis/faraday-gzip/blob/master/CHANGELOG.md
|
175
|
-
documentation_uri: http://www.rubydoc.info/gems/faraday-gzip/
|
176
|
-
homepage_uri: https://github.com/bodrovis/faraday-gzip
|
166
|
+
documentation_uri: http://www.rubydoc.info/gems/faraday-gzip/3.0.4
|
177
167
|
source_code_uri: https://github.com/bodrovis/faraday-gzip
|
178
168
|
rubygems_mfa_required: 'true'
|
179
|
-
post_install_message:
|
180
169
|
rdoc_options: []
|
181
170
|
require_paths:
|
182
171
|
- lib
|
@@ -184,18 +173,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
184
173
|
requirements:
|
185
174
|
- - ">="
|
186
175
|
- !ruby/object:Gem::Version
|
187
|
-
version: '
|
188
|
-
- - "<"
|
189
|
-
- !ruby/object:Gem::Version
|
190
|
-
version: '4'
|
176
|
+
version: '3.0'
|
191
177
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
192
178
|
requirements:
|
193
179
|
- - ">="
|
194
180
|
- !ruby/object:Gem::Version
|
195
181
|
version: '0'
|
196
182
|
requirements: []
|
197
|
-
rubygems_version: 3.
|
198
|
-
signing_key:
|
183
|
+
rubygems_version: 3.6.7
|
199
184
|
specification_version: 4
|
200
185
|
summary: Automatically sets compression headers and decompresses the response
|
201
186
|
test_files: []
|