Skip to content

Commit 2127a0c

Browse files
authored
Remove references to deprecated ::Parts and ::UploadIO (#6)
* remove references to deprecated ::Parts and ::UploadIO * add backwards compatibility by detecting version of multipart-post gem * find a way to work with multipart_post 2.1 and 2.2 * add notes to CHANGELOG.md * move version.rb around so it loads first, to allow others to pick this up * simplify multipart-port dependency in faraday-multipart.gemspec
1 parent 598775f commit 2127a0c

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

33
## Unreleased
4+
* Drop support for 'multipart-post' < 2.0.0. This is not a breaking change as the code didn't work with 1.x.
5+
* Change references to UploadIO and Parts according to class reorganization in 'multipart-post' 2.2.0 (PR [#89](https://github.com/socketry/multipart-post/pull/89))
6+
* Introduce a backwards compatible safeguard so the gem still works with previous 'multipart-post' 2.x releases.
7+
48

59
* Initial release.

faraday-multipart.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ Gem::Specification.new do |spec|
3131

3232
spec.required_ruby_version = '>= 2.4', '< 4'
3333

34-
spec.add_dependency 'multipart-post', '>= 1.2', '< 3'
34+
spec.add_dependency 'multipart-post', '~> 2'
3535
end

lib/faraday/multipart.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# frozen_string_literal: true
22

3+
require_relative 'multipart/version'
34
require_relative 'multipart/file_part'
45
require_relative 'multipart/param_part'
56
require_relative 'multipart/middleware'
6-
require_relative 'multipart/version'
77

88
module Faraday
99
# Main Faraday::Multipart module.
@@ -16,5 +16,11 @@ module Multipart
1616
ParamPart = Multipart::ParamPart
1717
Parts = Multipart::Parts
1818
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
2026
end

lib/faraday/multipart/file_part.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
require 'stringio'
44

5-
# multipart-post gem
6-
require 'composite_io'
7-
require 'parts'
8-
95
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:
109
module Multipart
1110
# Multipart value used to POST a binary data from a file or
1211
#
@@ -51,9 +50,16 @@ module Multipart
5150
# The open IO object for the uploaded file.
5251
#
5352
# @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
5763

5864
# Similar to, but not compatible with CompositeReadIO provided by the
5965
# multipart-post gem.

lib/faraday/multipart/version.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
# frozen_string_literal: true
22

33
module Faraday
4+
# #:nodoc:
45
module Multipart
56
VERSION = '1.0.3'
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
615
end
716
end

0 commit comments

Comments
 (0)