Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relax constraints on objects that we'll accept as a file #766

Merged
merged 1 commit into from
Apr 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/stripe/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ def self.create(params = {}, opts = {})
# rest-client would accept a vanilla `File` for upload, but Faraday does
# not. Support the old API by wrapping a `File`-like object with an
# `UploadIO` object if we're given one.
if params[:file] && params[:file].respond_to?(:path) && params[:file].respond_to?(:read)
if params[:file] && !params[:file].is_a?(String)
unless params[:file].respond_to?(:read)
raise ArgumentError, "file must respond to `#read`"
end

params[:file] = Faraday::UploadIO.new(params[:file], nil)
end

Expand Down
21 changes: 21 additions & 0 deletions test/stripe/file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ class FileTest < Test::Unit::TestCase
assert_requested :post, "#{Stripe.uploads_base}/v1/files"
assert file.is_a?(Stripe::File)
end

should "be creatable with a string" do
file = Stripe::File.create(
purpose: "dispute_evidence",
file: "my-file-contents",
file_link_data: { create: true }
)
assert_requested :post, "#{Stripe.uploads_base}/v1/files"
assert file.is_a?(Stripe::File)
end

should "raise given a file object that doesn't respond to #read" do
e = assert_raises(ArgumentError) do
Stripe::File.create(
purpose: "dispute_evidence",
file: Object.new,
file_link_data: { create: true }
)
end
assert_equal "file must respond to `#read`", e.message
end
end

should "be deserializable when `object=file`" do
Expand Down