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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 44ad7c4e9c57927b182f8da9719aeb71455cd664607582af5be4440d660c9015
4
- data.tar.gz: cfd6528ad8d82ce76f1b62cddbd63e2af086dab192a62c1dc14e6603ae5f5a18
3
+ metadata.gz: 5183c6f188272ea7e213adf03bb2d637c348e9f273b1b8829255f135a95b8c92
4
+ data.tar.gz: ce73797d7aa191ef07c376af0193ea704a653237146affec2806031194d064c7
5
5
  SHA512:
6
- metadata.gz: 61a096ffcf59316c2c7a1e9be64f159e879887a4a56dbfe495268d4c70a7636944885307277dd62c1a705df063676a6526921a65d01a0436cf01331349a01828
7
- data.tar.gz: 174bd0148dfcdc7e33acddb9c086629b37f8179905081c45ea4c35fb2e916c6f07c994a8065fce6b900aca34de1624e162330b185766f8c18a9d3edb6098c6f3
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
  ![CI](https://github.com/bodrovis/faraday-gzip/actions/workflows/ci.yaml/badge.svg)
4
4
  [![Gem](https://img.shields.io/gem/v/faraday-gzip.svg?style=flat-square)](https://rubygems.org/gems/faraday-gzip)
5
+ ![Gem Total Downloads](https://img.shields.io/gem/dt/faraday-gzip)
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 wasn't set in the request, this sets it to "gzip,deflate" and appropriately handles the compressed response from the server. This resembles what Ruby does internally in Net::HTTP#get. If [Brotli](https://github.com/miyucy/brotli) is added to the Gemfile, it will also add "br" to the 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
- This gem is tested with Ruby 2.6+ and JRuby 9.3+. Faraday 1 and 2 is supported.
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
- ```shell
24
+ ```
23
25
  bundle install
24
26
  ```
25
27
 
26
28
  Or install it yourself as:
27
29
 
28
- ```shell
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' # <=== add this line
39
+ require 'faraday/gzip' # <=== Add this line
36
40
 
37
41
  conn = Faraday.new(...) do |f|
38
- f.request :gzip # <=== add this line
39
- #...
42
+ f.request :gzip # <=== Add this line
43
+ # Additional configuration...
40
44
  end
41
45
  ```
42
46
 
43
47
  ## Development
44
48
 
45
- After checking out the repo, run `bin/setup` to install dependencies.
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
- To release a new version, make a commit with a message such as "Bumped to 0.0.2" and then run `rake release`.
52
- See how it works [here](https://bundler.io/guides/creating_gem.html#releasing-the-gem).
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
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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
- # Middleware to automatically decompress response bodies. If the
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
- if empty_body?(response_env)
38
- reset_body(response_env) { |body| raw_body(body) }
39
- else
40
- case response_env[:response_headers][CONTENT_ENCODING]
41
- when 'gzip'
42
- reset_body(response_env) { |body| uncompress_gzip(body) }
43
- when 'deflate'
44
- reset_body(response_env) { |body| inflate(body) }
45
- when 'br'
46
- reset_body(response_env) { |body| brotli_inflate(body) }
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
- def reset_body(env)
53
- env[:body] = yield(env[:body])
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
- gzip_reader.read
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Faraday
4
4
  module Gzip
5
- VERSION = '2.0.1'
5
+ VERSION = '3.0.4'
6
6
  end
7
7
  end
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: 2.0.1
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: 2024-01-02 00:00:00.000000000 Z
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: '1.0'
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: '1.0'
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: '2.11'
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: '2.11'
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/2.0.1
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: '2.6'
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.5.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: []