faraday-gzip 0.1.0.rc1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '051489969db6aaa4791f080d6128042153c109f0e12ff479aa183a84f97410aa'
4
+ data.tar.gz: e077af7059120447e3e108743b7b39a9be75e2e75674e4d0a52471956678857c
5
+ SHA512:
6
+ metadata.gz: 31cfe83b44cd5ec13f2eb86ab4769ac4c18d11676104528cbac006f5e88ab6c71056ff91c8be1e978693b15957cb41a8420aa0bcc99170354ff7a66f9769987a
7
+ data.tar.gz: f617a27db54e84099aeed3db1fd671a4c80c3b9c83883b904cee142eb186d90a1eb9325aaae2c807e83022027af74dc1fc9b8405c32a81128151ea29b5fa49bb
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0.rc1
4
+
5
+ * Initial release.
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+ <% require 'date' %>
3
+ Copyright (c) 2022 Ilya Bodrov-Krukowski
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,57 @@
1
+ # Faraday Gzip
2
+
3
+ [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/lostisland/faraday-gzip/ci)](https://github.com/lostisland/faraday-gzip/actions?query=branch%3Amain)
4
+ [![Gem](https://img.shields.io/gem/v/faraday-gzip.svg?style=flat-square)](https://rubygems.org/gems/faraday-gzip)
5
+ [![License](https://img.shields.io/github/license/lostisland/faraday-gzip.svg?style=flat-square)](LICENSE.md)
6
+
7
+ The `Gzip` middleware 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 is added to the Gemfile, it will also add "br" to the header.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'faraday-gzip'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ ```shell
20
+ bundle install
21
+ ```
22
+
23
+ Or install it yourself as:
24
+
25
+ ```shell
26
+ gem install faraday-gzip
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ ```ruby
32
+ require 'faraday/gzip'
33
+
34
+ conn = Faraday.new(...) do |f|
35
+ f.request :gzip
36
+ #...
37
+ end
38
+ ```
39
+
40
+ ## Development
41
+
42
+ After checking out the repo, run `bin/setup` to install dependencies.
43
+
44
+ Then, run `bin/test` to run the tests.
45
+
46
+ To install this gem onto your local machine, run `rake build`.
47
+
48
+ To release a new version, make a commit with a message such as "Bumped to 0.0.2" and then run `rake release`.
49
+ See how it works [here](https://bundler.io/guides/creating_gem.html#releasing-the-gem).
50
+
51
+ ## Contributing
52
+
53
+ Bug reports and pull requests are welcome on GitHub.
54
+
55
+ ## License
56
+
57
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'zlib'
4
+
5
+ module Faraday
6
+ 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
+
12
+ class Middleware < Faraday::Middleware
13
+ def self.optional_dependency(lib = nil)
14
+ lib ? require(lib) : yield
15
+ true
16
+ rescue LoadError, NameError
17
+ false
18
+ end
19
+
20
+ BROTLI_SUPPORTED = optional_dependency 'brotli'
21
+
22
+ def self.supported_encodings
23
+ encodings = %w[gzip deflate]
24
+ encodings << 'br' if BROTLI_SUPPORTED
25
+ encodings
26
+ end
27
+
28
+ ACCEPT_ENCODING = 'Accept-Encoding'
29
+ CONTENT_ENCODING = 'Content-Encoding'
30
+ CONTENT_LENGTH = 'Content-Length'
31
+ SUPPORTED_ENCODINGS = supported_encodings.join(',').freeze
32
+
33
+ def call(env)
34
+ env[:request_headers][ACCEPT_ENCODING] ||= SUPPORTED_ENCODINGS
35
+ @app.call(env).on_complete do |response_env|
36
+ if response_env[:body].empty?
37
+ reset_body(response_env) { |body| raw_body(body) }
38
+ else
39
+ case response_env[:response_headers][CONTENT_ENCODING]
40
+ when 'gzip'
41
+ reset_body(response_env) { |body| uncompress_gzip(body) }
42
+ when 'deflate'
43
+ reset_body(response_env) { |body| inflate(body) }
44
+ when 'br'
45
+ reset_body(response_env) { |body| brotli_inflate(body) }
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ def reset_body(env)
52
+ env[:body] = yield(env[:body])
53
+ env[:response_headers].delete(CONTENT_ENCODING)
54
+ env[:response_headers][CONTENT_LENGTH] = env[:body].length
55
+ end
56
+
57
+ def uncompress_gzip(body)
58
+ io = StringIO.new(body)
59
+ gzip_reader = Zlib::GzipReader.new(io, encoding: 'ASCII-8BIT')
60
+ gzip_reader.read
61
+ end
62
+
63
+ def inflate(body)
64
+ # Inflate as a DEFLATE (RFC 1950+RFC 1951) stream
65
+ Zlib::Inflate.inflate(body)
66
+ rescue Zlib::DataError
67
+ # Fall back to inflating as a "raw" deflate stream which
68
+ # Microsoft servers return
69
+ inflate = Zlib::Inflate.new(-Zlib::MAX_WBITS)
70
+ begin
71
+ inflate.inflate(body)
72
+ ensure
73
+ inflate.close
74
+ end
75
+ end
76
+
77
+ def brotli_inflate(body)
78
+ Brotli.inflate(body)
79
+ end
80
+
81
+ def raw_body(body)
82
+ body
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faraday
4
+ module Gzip
5
+ VERSION = '0.1.0.rc1'
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require_relative 'gzip/middleware'
5
+ require_relative 'gzip/version'
6
+
7
+ module Faraday
8
+ # Middleware main module.
9
+ module Gzip
10
+ Faraday::Request.register_middleware(gzip: Faraday::Gzip::Middleware)
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,202 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday-gzip
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.rc1
5
+ platform: ruby
6
+ authors:
7
+ - Ilya Bodrov-Krukowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-02-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: zlib
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.21.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.21.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.25.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.25.0
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
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-performance
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop-rspec
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '2.0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '2.0'
153
+ description: 'Faraday plugin to automatically set compression headers (GZip, Deflate,
154
+ Brotli) and decompress the response.
155
+
156
+ '
157
+ email:
158
+ - golosizpru@gmail.com
159
+ executables: []
160
+ extensions: []
161
+ extra_rdoc_files: []
162
+ files:
163
+ - CHANGELOG.md
164
+ - LICENSE.md
165
+ - README.md
166
+ - lib/faraday/gzip.rb
167
+ - lib/faraday/gzip/middleware.rb
168
+ - lib/faraday/gzip/version.rb
169
+ homepage: https://github.com/lostisland/faraday-gzip
170
+ licenses:
171
+ - MIT
172
+ metadata:
173
+ bug_tracker_uri: https://github.com/lostisland/faraday-gzip/issues
174
+ changelog_uri: https://github.com/lostisland/faraday-gzip/blob/v0.1.0.rc1/CHANGELOG.md
175
+ documentation_uri: http://www.rubydoc.info/gems/faraday-gzip/0.1.0.rc1
176
+ homepage_uri: https://github.com/lostisland/faraday-gzip
177
+ source_code_uri: https://github.com/lostisland/faraday-gzip
178
+ wiki_uri: https://github.com/lostisland/faraday-gzip/wiki
179
+ rubygems_mfa_required: 'true'
180
+ post_install_message:
181
+ rdoc_options: []
182
+ require_paths:
183
+ - lib
184
+ required_ruby_version: !ruby/object:Gem::Requirement
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ version: '2.6'
189
+ - - "<"
190
+ - !ruby/object:Gem::Version
191
+ version: '4'
192
+ required_rubygems_version: !ruby/object:Gem::Requirement
193
+ requirements:
194
+ - - ">"
195
+ - !ruby/object:Gem::Version
196
+ version: 1.3.1
197
+ requirements: []
198
+ rubygems_version: 3.3.5
199
+ signing_key:
200
+ specification_version: 4
201
+ summary: Automatically sets compression headers and decompresses the response
202
+ test_files: []