Skip to content

Commit

Permalink
Support Tempfiles in file_upload creation requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Dec 6, 2017
1 parent ffb6ea4 commit 6665841
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/stripe/file_upload.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'tempfile'

module Stripe
class FileUpload < APIResource
extend Stripe::APIOperations::Create
Expand All @@ -20,7 +22,7 @@ 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` 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

Expand Down
21 changes: 20 additions & 1 deletion test/stripe/file_upload_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class FileUploadTest < Test::Unit::TestCase
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}/,
Expand All @@ -46,6 +46,25 @@ class FileUploadTest < Test::Unit::TestCase
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: {
Expand Down

0 comments on commit 6665841

Please sign in to comment.