Skip to content

Commit

Permalink
Use duck typing to detect File-like objects
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Dec 14, 2017
1 parent be8e4ff commit d946d7a
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/stripe/file_upload.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "tempfile"

module Stripe
class FileUpload < APIResource
extend Stripe::APIOperations::Create
Expand All @@ -20,9 +18,9 @@ def self.request(method, url, params = {}, opts = {})

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] && [File, Tempfile].any? { |klass| params[:file].is_a?(klass) }
# 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)
params[:file] = Faraday::UploadIO.new(params[:file], nil)
end

Expand Down

0 comments on commit d946d7a

Please sign in to comment.