Support Tempfiles in file_upload creation requests

This commit is contained in:
Olivier Bellone 2017-12-05 16:42:42 -08:00
parent ffb6ea4a31
commit 949efb017d
No known key found for this signature in database
GPG Key ID: 11E77E3AA0C40303
2 changed files with 23 additions and 2 deletions

View File

@ -1,3 +1,5 @@
require "tempfile"
module Stripe
class FileUpload < APIResource
extend Stripe::APIOperations::Create
@ -20,7 +22,7 @@ module Stripe
# rest-client would accept a vanilla `File` for upload, but Faraday does
# not. Support the old API by wrapping a `File` with an `UploadIO` object
# if we're given one.
if params[:file] && params[:file].is_a?(File)
if params[:file] && [File, Tempfile].any? { |klass| params[:file].is_a?(klass) }
params[:file] = Faraday::UploadIO.new(params[:file], nil)
end

View File

@ -31,7 +31,7 @@ module Stripe
assert file.is_a?(Stripe::FileUpload)
end
should "be creatable" do
should "be creatable with a File" do
stub_request(:post, "#{Stripe.uploads_base}/v1/files")
.with(headers: {
"Content-Type" => /\A#{Faraday::Request::Multipart.mime_type}/,
@ -46,6 +46,25 @@ module Stripe
assert file.is_a?(Stripe::FileUpload)
end
should "be creatable with a Tempfile" do
stub_request(:post, "#{Stripe.uploads_base}/v1/files")
.with(headers: {
"Content-Type" => /\A#{Faraday::Request::Multipart.mime_type}/,
}) do |request|
request.body =~ /Hello world/
end.to_return(body: JSON.generate(FIXTURE))
tempfile = Tempfile.new("foo")
tempfile.write("Hello world")
tempfile.rewind
file = Stripe::FileUpload.create(
purpose: "dispute_evidence",
file: tempfile
)
assert file.is_a?(Stripe::FileUpload)
end
should "be creatable with Faraday::UploadIO" do
stub_request(:post, "#{Stripe.uploads_base}/v1/files")
.with(headers: {