faraday-multipart 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d85a6f383a3e6d727617d552cf1eeb34519b0fb83a810f80897f803a67f23ddc
4
- data.tar.gz: c6b1b47ef169ee943c4b318b7efa1a5363eb70d918f48f2f1924d6ea62a438fb
3
+ metadata.gz: 5bd506d533ad9203184a3dac476941919f349e4565e81fa1521b8bc022f60f64
4
+ data.tar.gz: b34ae2db7c7d54de96ef743d2568c31066f2160ccc120d1b719e3fe6eb7141b0
5
5
  SHA512:
6
- metadata.gz: 4c29ca86e5f9f815d7b56a5ee87774f9f04d1eeedf882c7572edf9f848ef8e2d0353667dd954dbf1b10033435deab39e75382073c2cf35fddb4db5fcfeb28c6d
7
- data.tar.gz: 9a79cbe41c36b5183a0507b58cd10f80301f55c78e3c770f95731568ddffba3f237f4859205a53ddc43f2e39852314135d65443d4549ac7a4a1522942da5e8e8
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.3'
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,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'multipart/version'
3
4
  require_relative 'multipart/file_part'
4
5
  require_relative 'multipart/param_part'
5
6
  require_relative 'multipart/middleware'
6
- require_relative 'multipart/version'
7
7
 
8
8
  module Faraday
9
9
  # Main Faraday::Multipart module.
@@ -16,5 +16,11 @@ module Faraday
16
16
  ParamPart = Multipart::ParamPart
17
17
  Parts = Multipart::Parts
18
18
  CompositeReadIO = Multipart::CompositeReadIO
19
- UploadIO = ::UploadIO
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
20
26
  end
metadata CHANGED
@@ -1,35 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-multipart
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
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-08 00:00:00.000000000 Z
11
+ date: 2024-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multipart-post
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.2'
20
- - - "<"
21
- - !ruby/object:Gem::Version
22
- version: '3'
19
+ version: '2.0'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- version: '1.2'
30
- - - "<"
24
+ - - "~>"
31
25
  - !ruby/object:Gem::Version
32
- version: '3'
26
+ version: '2.0'
33
27
  description: 'Perform multipart-post requests using Faraday.
34
28
 
35
29
  '
@@ -52,12 +46,12 @@ licenses:
52
46
  - MIT
53
47
  metadata:
54
48
  bug_tracker_uri: https://github.com/lostisland/faraday-multipart/issues
55
- changelog_uri: https://github.com/lostisland/faraday-multipart/blob/v1.0.3/CHANGELOG.md
56
- documentation_uri: http://www.rubydoc.info/gems/faraday-multipart/1.0.3
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
57
51
  homepage_uri: https://github.com/lostisland/faraday-multipart
58
52
  source_code_uri: https://github.com/lostisland/faraday-multipart
59
53
  wiki_uri: https://github.com/lostisland/faraday-multipart/wiki
60
- post_install_message:
54
+ post_install_message:
61
55
  rdoc_options: []
62
56
  require_paths:
63
57
  - lib
@@ -76,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
70
  version: '0'
77
71
  requirements: []
78
72
  rubygems_version: 3.1.6
79
- signing_key:
73
+ signing_key:
80
74
  specification_version: 4
81
75
  summary: Perform multipart-post requests using Faraday.
82
76
  test_files: []