faraday-multipart 1.0.0 → 1.1.0

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: 7bd6e0b4dcb4d5905af5f8ee93c61a77858f13d1bfbbc00c3b9ea24091ef6af9
4
- data.tar.gz: fd8d9f6e6cad9ad03e98e2b756be32c6862b23798552149d93568a6902bc6f19
3
+ metadata.gz: 5bd506d533ad9203184a3dac476941919f349e4565e81fa1521b8bc022f60f64
4
+ data.tar.gz: b34ae2db7c7d54de96ef743d2568c31066f2160ccc120d1b719e3fe6eb7141b0
5
5
  SHA512:
6
- metadata.gz: b5644f53bad5e159723fbb174164270dc4de8668eac04d278004c9b11608337771f73295e73160125c954dd6a36b0af702bada137c4f9894a935517c3afc06e7
7
- data.tar.gz: 37613553511b02097c04d4524e13fe1c3fc946b2549a07dc2b76e9519a570fa88f6f7717964c0cdbcf9425b09b46565cd8b8b73cd3cb8707ee597480d0c67f4b
6
+ metadata.gz: 3cb034f5f9fc6dbbb71225a967dcb81fdffdde7b7cd7790fe019e4f4c1b7e242f9df1726c6f2f8f0f33555d88613a6e6d75ebb50a638e939e9618d6dc7a65161
7
+ data.tar.gz: 37e5c0998d0ad5d40cd03cf3cdaf98e1f9473451035553bf61d1a8ba107ee53f62311080035520044b0b3053e1cb33bd2cd781d3480d11206197e96adc579099
data/CHANGELOG.md CHANGED
@@ -1,5 +1,54 @@
1
1
  # Changelog
2
2
 
3
- ## Unreleased
3
+ ## [1.0.4](https://github.com/lostisland/faraday-multipart/releases/tag/v1.0.4) (2022-06-07)
4
4
 
5
- * Initial release.
5
+ ### What's Changed
6
+
7
+ * Drop support for 'multipart-post' < 2.0.0. This is not a breaking change as this gem's code didn't work with 1.x.
8
+ * Change references to `UploadIO` and `Parts` according to class reorganization in the 'multipart-post' gem 2.2.0 (see [multipart-post gem PR #89](https://github.com/socketry/multipart-post/pull/89))
9
+ * Introduce a backwards compatible safeguard so the gem still works with previous 'multipart-post' 2.x releases.
10
+
11
+ **Full Changelog**: https://github.com/lostisland/faraday-multipart/compare/v1.0.3...v1.0.4
12
+
13
+ ## [1.0.3](https://github.com/lostisland/faraday-multipart/releases/tag/v1.0.3) (2022-01-08)
14
+
15
+ ### What's Changed
16
+
17
+ * Add `Faraday::ParamPart` alias back by @iMacTia in https://github.com/lostisland/faraday-multipart/pull/2
18
+
19
+ **Full Changelog**: https://github.com/lostisland/faraday-multipart/compare/v1.0.2...v1.0.3
20
+
21
+ ## [1.0.2](https://github.com/lostisland/faraday-multipart/releases/tag/v1.0.2) (2022-01-06)
22
+
23
+ ### Fixes
24
+
25
+ * Add missing UploadIO alias
26
+ * Re-add support for Ruby 2.4+
27
+
28
+ **Full Changelog**: https://github.com/lostisland/faraday-multipart/compare/v1.0.1...v1.0.2
29
+
30
+ ## [1.0.1](https://github.com/lostisland/faraday-multipart/releases/tag/v1.0.1) (2022-01-06)
31
+
32
+ ### What's Changed
33
+ * Add support for Faraday v1 by @iMacTia in https://github.com/lostisland/faraday-multipart/pull/1
34
+
35
+ **Full Changelog**: https://github.com/lostisland/faraday-multipart/compare/v1.0.0...v1.0.1
36
+
37
+ ## [1.0.0](https://github.com/lostisland/faraday-multipart/releases/tag/v1.0.0) (2022-01-04)
38
+
39
+ ### Summary
40
+
41
+ The initial release of the `faraday-multipart` gem.
42
+
43
+ This middleware was previously bundled with Faraday but was removed as of v2.0.
44
+
45
+ ### MIGRATION NOTES
46
+
47
+ If you're upgrading from Faraday 1.0 and including this middleware as a gem, please be aware the namespacing for helper classes has changed:
48
+
49
+ * `Faraday::FilePart` is now `Faraday::Multipart::FilePart`
50
+ * `Faraday::Parts` is now `Faraday::Multipart::Parts`
51
+ * `Faraday::CompositeReadIO` is now `Faraday::Multipart::CompositeReadIO`
52
+ * `Faraday::ParamPart` is now `Faraday::Multipart::ParamPart`
53
+
54
+ Moreover, in case you're adding the middleware to your faraday connection with the full qualified name rather than the `:multipart` alias, please be aware the middleware class is now `Faraday::Multipart::Middleware`.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Faraday Multipart
2
2
 
3
- [![GitHub Workflow Status](https://img.shields.io/github/workflow/status/lostisland/faraday-multipart/ci)](https://github.com/lostisland/faraday-multipart/actions?query=branch%3Amain)
3
+ [![ci](https://github.com/lostisland/faraday-multipart/actions/workflows/ci.yaml/badge.svg)](https://github.com/lostisland/faraday-multipart/actions/workflows/ci.yaml)
4
4
  [![Gem](https://img.shields.io/gem/v/faraday-multipart.svg?style=flat-square)](https://rubygems.org/gems/faraday-multipart)
5
5
  [![License](https://img.shields.io/github/license/lostisland/faraday-multipart.svg?style=flat-square)](LICENSE.md)
6
6
 
@@ -42,10 +42,24 @@ gem install faraday-multipart
42
42
  First of all, you'll need to add the multipart middleware to your Faraday connection:
43
43
 
44
44
  ```ruby
45
+ require 'faraday'
45
46
  require 'faraday/multipart'
46
47
 
47
48
  conn = Faraday.new(...) do |f|
48
- f.request :multipart
49
+ f.request :multipart, **options
50
+ # ...
51
+ end
52
+ ```
53
+
54
+ If you need to [specify a different content type for the multipart
55
+ request](https://www.iana.org/assignments/media-types/media-types.xhtml#multipart),
56
+ you can do so by providing the `content_type` option but it must start with
57
+ `multipart/`
58
+ otherwise it will default to `multipart/form-data`:
59
+
60
+ ```ruby
61
+ conn = Faraday.new(...) do |f|
62
+ f.request :multipart, content_type: 'multipart/mixed'
49
63
  # ...
50
64
  end
51
65
  ```
@@ -91,6 +105,50 @@ payload[:raw_with_id] = Faraday::Multipart::ParamPart.new(
91
105
  conn.post('/', payload)
92
106
  ```
93
107
 
108
+ ### Sending an array of documents
109
+
110
+ Sometimes, the server you're calling will expect an array of documents or other values for the same key.
111
+ The `multipart` middleware will automatically handle this scenario for you:
112
+
113
+ ```ruby
114
+ payload = {
115
+ files: [
116
+ Faraday::Multipart::FilePart.new(__FILE__, 'text/x-ruby'),
117
+ Faraday::Multipart::FilePart.new(__FILE__, 'text/x-pdf')
118
+ ],
119
+ url: [
120
+ 'http://mydomain.com/callback1',
121
+ 'http://mydomain.com/callback2'
122
+ ]
123
+ }
124
+
125
+ conn.post(url, payload)
126
+ #=> POST url[]=http://mydomain.com/callback1&url[]=http://mydomain.com/callback2
127
+ #=> and includes both files in the request under the `files[]` name
128
+ ```
129
+
130
+ However, by default these will be sent with `files[]` key and the URLs with `url[]`, similarly to arrays in URL parameters.
131
+ Some servers (e.g. Mailgun) expect each document to have the same parameter key instead.
132
+ You can instruct the `multipart` middleware to do so by providing the `flat_encode` option:
133
+
134
+ ```ruby
135
+ require 'faraday'
136
+ require 'faraday/multipart'
137
+
138
+ conn = Faraday.new(...) do |f|
139
+ f.request :multipart, flat_encode: true
140
+ # ...
141
+ end
142
+
143
+ payload = ... # see example above
144
+
145
+ conn.post(url, payload)
146
+ #=> POST url=http://mydomain.com/callback1&url=http://mydomain.com/callback2
147
+ #=> and includes both files in the request under the `files` name
148
+ ```
149
+
150
+ This works for both `UploadIO` and normal parameters alike.
151
+
94
152
  ## Development
95
153
 
96
154
  After checking out the repo, run `bin/setup` to install dependencies.
@@ -99,8 +157,9 @@ Then, run `bin/test` to run the tests.
99
157
 
100
158
  To install this gem onto your local machine, run `rake build`.
101
159
 
102
- To release a new version, make a commit with a message such as "Bumped to 0.0.2" and then run `rake release`. See how it
103
- works [here](https://bundler.io/guides/creating_gem.html#releasing-the-gem).
160
+ ### Releasing a new version
161
+
162
+ To release a new version, make a commit with a message such as "Bumped to 0.0.2", and change the _Unreleased_ heading in `CHANGELOG.md` to a heading like "0.0.2 (2022-01-01)", and then use GitHub Releases to author a release. A GitHub Actions workflow then publishes a new gem to [RubyGems.org](https://rubygems.org/gems/faraday-multipart).
104
163
 
105
164
  ## Contributing
106
165
 
@@ -2,11 +2,10 @@
2
2
 
3
3
  require 'stringio'
4
4
 
5
- # multipart-post gem
6
- require 'composite_io'
7
- require 'parts'
8
-
9
5
  module Faraday
6
+ # Rubocop doesn't seem to understand that this is an extension to the
7
+ # Multipart module, so let's add a nodoc
8
+ # #:nodoc:
10
9
  module Multipart
11
10
  # Multipart value used to POST a binary data from a file or
12
11
  #
@@ -51,9 +50,16 @@ module Faraday
51
50
  # The open IO object for the uploaded file.
52
51
  #
53
52
  # @return [IO]
54
- FilePart = ::UploadIO
55
-
56
- Parts = ::Parts
53
+ if ::Gem::Requirement.new('>= 2.2.0').satisfied_by?(multipart_post_version)
54
+ require 'multipart/post'
55
+ FilePart = ::Multipart::Post::UploadIO
56
+ Parts = ::Multipart::Post::Parts
57
+ else
58
+ require 'composite_io'
59
+ require 'parts'
60
+ FilePart = ::UploadIO
61
+ Parts = ::Parts
62
+ end
57
63
 
58
64
  # Similar to, but not compatible with CompositeReadIO provided by the
59
65
  # multipart-post gem.
@@ -5,11 +5,10 @@ require 'securerandom'
5
5
  module Faraday
6
6
  module Multipart
7
7
  # Middleware for supporting multi-part requests.
8
- class Middleware < Faraday::Request::UrlEncoded
8
+ class Middleware < Faraday::Middleware
9
+ CONTENT_TYPE = 'Content-Type'
9
10
  DEFAULT_BOUNDARY_PREFIX = '-----------RubyMultipartPost'
10
11
 
11
- self.mime_type = 'multipart/form-data'
12
-
13
12
  def initialize(app = nil, options = {})
14
13
  super(app)
15
14
  @options = options
@@ -28,15 +27,37 @@ module Faraday
28
27
  @app.call env
29
28
  end
30
29
 
30
+ private
31
+
32
+ # @param env [Faraday::Env]
33
+ # @yield [request_body] Body of the request
34
+ def match_content_type(env)
35
+ return unless process_request?(env)
36
+
37
+ env.request_headers[CONTENT_TYPE] ||= mime_type
38
+ return if env.body.respond_to?(:to_str) || env.body.respond_to?(:read)
39
+
40
+ yield(env.body)
41
+ end
42
+
31
43
  # @param env [Faraday::Env]
32
44
  def process_request?(env)
33
45
  type = request_type(env)
34
46
  env.body.respond_to?(:each_key) && !env.body.empty? && (
35
47
  (type.empty? && has_multipart?(env.body)) ||
36
- (type == self.class.mime_type)
48
+ (type == mime_type)
37
49
  )
38
50
  end
39
51
 
52
+ # @param env [Faraday::Env]
53
+ #
54
+ # @return [String]
55
+ def request_type(env)
56
+ type = env.request_headers[CONTENT_TYPE].to_s
57
+ type = type.split(';', 2).first if type.index(';')
58
+ type
59
+ end
60
+
40
61
  # Returns true if obj is an enumerable with values that are multipart.
41
62
  #
42
63
  # @param obj [Object]
@@ -97,6 +118,17 @@ module Faraday
97
118
  end
98
119
  end
99
120
  end
121
+
122
+ # Determines and provides the multipart mime type for the request.
123
+ #
124
+ # @return [String] the multipart mime type
125
+ def mime_type
126
+ @mime_type ||= if @options[:content_type].to_s.match?(%r{\Amultipart/.+})
127
+ @options[:content_type].to_s
128
+ else
129
+ 'multipart/form-data'
130
+ end
131
+ end
100
132
  end
101
133
  end
102
134
  end
@@ -1,7 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Faraday
4
+ # #:nodoc:
4
5
  module Multipart
5
- VERSION = '1.0.0'
6
+ VERSION = '1.1.0'
7
+
8
+ def self.multipart_post_version
9
+ require 'multipart/post/version'
10
+ ::Gem::Version.new(::Multipart::Post::VERSION)
11
+ rescue LoadError
12
+ require 'multipart_post'
13
+ ::Gem::Version.new(::MultipartPost::VERSION)
14
+ end
6
15
  end
7
16
  end
@@ -1,14 +1,26 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'faraday'
3
+ require_relative 'multipart/version'
4
4
  require_relative 'multipart/file_part'
5
5
  require_relative 'multipart/param_part'
6
6
  require_relative 'multipart/middleware'
7
- require_relative 'multipart/version'
8
7
 
9
8
  module Faraday
10
9
  # Main Faraday::Multipart module.
11
10
  module Multipart
12
11
  Faraday::Request.register_middleware(multipart: Faraday::Multipart::Middleware)
13
12
  end
13
+
14
+ # Aliases for Faraday v1, these are all deprecated and will be removed in v2 of this middleware
15
+ FilePart = Multipart::FilePart
16
+ ParamPart = Multipart::ParamPart
17
+ Parts = Multipart::Parts
18
+ CompositeReadIO = Multipart::CompositeReadIO
19
+ # multipart-post v2.2.0 introduces a new class hierarchy for classes like Parts and UploadIO
20
+ # For backwards compatibility, detect the gem version and use the right class
21
+ UploadIO = if ::Gem::Requirement.new('>= 2.2.0').satisfied_by?(Multipart.multipart_post_version)
22
+ ::Multipart::Post::UploadIO
23
+ else
24
+ ::UploadIO
25
+ end
14
26
  end
metadata CHANGED
@@ -1,155 +1,23 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-multipart
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mattia Giuffrida
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-04 00:00:00.000000000 Z
11
+ date: 2024-12-17 00:00:00.000000000 Z
12
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: '2.0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '2.0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: multipart-post
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '1.2'
34
- - - "<"
35
- - !ruby/object:Gem::Version
36
- version: '3'
37
- type: :runtime
38
- prerelease: false
39
- version_requirements: !ruby/object:Gem::Requirement
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- version: '1.2'
44
- - - "<"
45
- - !ruby/object:Gem::Version
46
- version: '3'
47
- - !ruby/object:Gem::Dependency
48
- name: bundler
49
15
  requirement: !ruby/object:Gem::Requirement
50
16
  requirements:
51
17
  - - "~>"
52
18
  - !ruby/object:Gem::Version
53
19
  version: '2.0'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - "~>"
59
- - !ruby/object:Gem::Version
60
- version: '2.0'
61
- - !ruby/object:Gem::Dependency
62
- name: rake
63
- requirement: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - "~>"
66
- - !ruby/object:Gem::Version
67
- version: '13.0'
68
- type: :development
69
- prerelease: false
70
- version_requirements: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - "~>"
73
- - !ruby/object:Gem::Version
74
- version: '13.0'
75
- - !ruby/object:Gem::Dependency
76
- name: rspec
77
- requirement: !ruby/object:Gem::Requirement
78
- requirements:
79
- - - "~>"
80
- - !ruby/object:Gem::Version
81
- version: '3.0'
82
- type: :development
83
- prerelease: false
84
- version_requirements: !ruby/object:Gem::Requirement
85
- requirements:
86
- - - "~>"
87
- - !ruby/object:Gem::Version
88
- version: '3.0'
89
- - !ruby/object:Gem::Dependency
90
- name: simplecov
91
- requirement: !ruby/object:Gem::Requirement
92
- requirements:
93
- - - "~>"
94
- - !ruby/object:Gem::Version
95
- version: 0.21.0
96
- type: :development
97
- prerelease: false
98
- version_requirements: !ruby/object:Gem::Requirement
99
- requirements:
100
- - - "~>"
101
- - !ruby/object:Gem::Version
102
- version: 0.21.0
103
- - !ruby/object:Gem::Dependency
104
- name: rubocop
105
- requirement: !ruby/object:Gem::Requirement
106
- requirements:
107
- - - "~>"
108
- - !ruby/object:Gem::Version
109
- version: 1.21.0
110
- type: :development
111
- prerelease: false
112
- version_requirements: !ruby/object:Gem::Requirement
113
- requirements:
114
- - - "~>"
115
- - !ruby/object:Gem::Version
116
- version: 1.21.0
117
- - !ruby/object:Gem::Dependency
118
- name: rubocop-packaging
119
- requirement: !ruby/object:Gem::Requirement
120
- requirements:
121
- - - "~>"
122
- - !ruby/object:Gem::Version
123
- version: 0.5.0
124
- type: :development
125
- prerelease: false
126
- version_requirements: !ruby/object:Gem::Requirement
127
- requirements:
128
- - - "~>"
129
- - !ruby/object:Gem::Version
130
- version: 0.5.0
131
- - !ruby/object:Gem::Dependency
132
- name: rubocop-performance
133
- requirement: !ruby/object:Gem::Requirement
134
- requirements:
135
- - - "~>"
136
- - !ruby/object:Gem::Version
137
- version: '1.0'
138
- type: :development
139
- prerelease: false
140
- version_requirements: !ruby/object:Gem::Requirement
141
- requirements:
142
- - - "~>"
143
- - !ruby/object:Gem::Version
144
- version: '1.0'
145
- - !ruby/object:Gem::Dependency
146
- name: rubocop-rspec
147
- requirement: !ruby/object:Gem::Requirement
148
- requirements:
149
- - - "~>"
150
- - !ruby/object:Gem::Version
151
- version: '2.0'
152
- type: :development
20
+ type: :runtime
153
21
  prerelease: false
154
22
  version_requirements: !ruby/object:Gem::Requirement
155
23
  requirements:
@@ -178,12 +46,12 @@ licenses:
178
46
  - MIT
179
47
  metadata:
180
48
  bug_tracker_uri: https://github.com/lostisland/faraday-multipart/issues
181
- changelog_uri: https://github.com/lostisland/faraday-multipart/blob/v1.0.0/CHANGELOG.md
182
- documentation_uri: http://www.rubydoc.info/gems/faraday-multipart/1.0.0
49
+ changelog_uri: https://github.com/lostisland/faraday-multipart/blob/v1.1.0/CHANGELOG.md
50
+ documentation_uri: http://www.rubydoc.info/gems/faraday-multipart/1.1.0
183
51
  homepage_uri: https://github.com/lostisland/faraday-multipart
184
52
  source_code_uri: https://github.com/lostisland/faraday-multipart
185
53
  wiki_uri: https://github.com/lostisland/faraday-multipart/wiki
186
- post_install_message:
54
+ post_install_message:
187
55
  rdoc_options: []
188
56
  require_paths:
189
57
  - lib
@@ -191,7 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
191
59
  requirements:
192
60
  - - ">="
193
61
  - !ruby/object:Gem::Version
194
- version: '2.6'
62
+ version: '2.4'
195
63
  - - "<"
196
64
  - !ruby/object:Gem::Version
197
65
  version: '4'
@@ -202,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
202
70
  version: '0'
203
71
  requirements: []
204
72
  rubygems_version: 3.1.6
205
- signing_key:
73
+ signing_key:
206
74
  specification_version: 4
207
75
  summary: Perform multipart-post requests using Faraday.
208
76
  test_files: []