mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-11-27 00:03:06 -05:00
Support Tempfiles in file_upload creation requests
This commit is contained in:
parent
ffb6ea4a31
commit
949efb017d
@ -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
|
||||
|
||||
|
||||
@ -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: {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user