From f3dd3ab1bf416e5a7d4cff2a002504760592df23 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Mon, 17 Sep 2018 14:37:41 +0200 Subject: [PATCH] Handle file objects like file_upload --- lib/stripe.rb | 2 +- lib/stripe/{file_upload.rb => file.rb} | 12 +++++- lib/stripe/util.rb | 3 +- test/stripe/file_test.rb | 56 ++++++++++++++++++++++++++ test/stripe/file_upload_test.rb | 10 +++++ 5 files changed, 79 insertions(+), 4 deletions(-) rename lib/stripe/{file_upload.rb => file.rb} (66%) create mode 100644 test/stripe/file_test.rb diff --git a/lib/stripe.rb b/lib/stripe.rb index 7fe803fa4..c6d5a2993 100644 --- a/lib/stripe.rb +++ b/lib/stripe.rb @@ -55,8 +55,8 @@ require "stripe/ephemeral_key" require "stripe/event" require "stripe/exchange_rate" +require "stripe/file" require "stripe/file_link" -require "stripe/file_upload" require "stripe/invoice" require "stripe/invoice_item" require "stripe/invoice_line_item" diff --git a/lib/stripe/file_upload.rb b/lib/stripe/file.rb similarity index 66% rename from lib/stripe/file_upload.rb rename to lib/stripe/file.rb index f598ab012..b4cd50350 100644 --- a/lib/stripe/file_upload.rb +++ b/lib/stripe/file.rb @@ -1,11 +1,16 @@ # frozen_string_literal: true module Stripe - class FileUpload < APIResource + class File < APIResource extend Stripe::APIOperations::Create extend Stripe::APIOperations::List - OBJECT_NAME = "file_upload".freeze + # This resource can have two different object names. In latter API + # versions, only `file` is used, but since stripe-ruby may be used with + # any API version, we need to support deserializing the older + # `file_upload` object into the same class. + OBJECT_NAME = "file".freeze + OBJECT_NAME_ALT = "file_upload".freeze def self.resource_url "/v1/files" @@ -32,4 +37,7 @@ def self.create(params = {}, opts = {}) super end end + + # For backwards compatibility, the `File` class is aliased to `FileUpload`. + FileUpload = File end diff --git a/lib/stripe/util.rb b/lib/stripe/util.rb index a40716c88..ccda642f4 100644 --- a/lib/stripe/util.rb +++ b/lib/stripe/util.rb @@ -64,8 +64,9 @@ def self.object_classes # rubocop:disable Metrics/MethodLength EphemeralKey::OBJECT_NAME => EphemeralKey, Event::OBJECT_NAME => Event, ExchangeRate::OBJECT_NAME => ExchangeRate, + File::OBJECT_NAME => File, + File::OBJECT_NAME_ALT => File, FileLink::OBJECT_NAME => FileLink, - FileUpload::OBJECT_NAME => FileUpload, Invoice::OBJECT_NAME => Invoice, InvoiceItem::OBJECT_NAME => InvoiceItem, InvoiceLineItem::OBJECT_NAME => InvoiceLineItem, diff --git a/test/stripe/file_test.rb b/test/stripe/file_test.rb new file mode 100644 index 000000000..1a5393028 --- /dev/null +++ b/test/stripe/file_test.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +require ::File.expand_path("../../test_helper", __FILE__) + +module Stripe + class FileTest < Test::Unit::TestCase + should "be listable" do + files = Stripe::File.list + assert files.data.is_a?(Array) + assert files.data[0].is_a?(Stripe::File) + end + + should "be retrievable" do + file = Stripe::File.retrieve("file_123") + assert file.is_a?(Stripe::File) + end + + should "be creatable with a File" do + file = Stripe::File.create( + purpose: "dispute_evidence", + file: ::File.new(__FILE__) + ) + assert file.is_a?(Stripe::File) + end + + should "be creatable with a Tempfile" do + tempfile = Tempfile.new("foo") + tempfile.write("Hello world") + tempfile.rewind + + file = Stripe::File.create( + purpose: "dispute_evidence", + file: tempfile + ) + assert file.is_a?(Stripe::File) + end + + should "be creatable with Faraday::UploadIO" do + file = Stripe::File.create( + purpose: "dispute_evidence", + file: Faraday::UploadIO.new(::File.new(__FILE__), nil) + ) + assert file.is_a?(Stripe::File) + end + + should "be deserializable when `object=file`" do + file = Stripe::Util.convert_to_stripe_object({ object: "file" }, {}) + assert file.is_a?(Stripe::File) + end + + should "be deserializable when `object=file_upload`" do + file = Stripe::Util.convert_to_stripe_object({ object: "file_upload" }, {}) + assert file.is_a?(Stripe::File) + end + end +end diff --git a/test/stripe/file_upload_test.rb b/test/stripe/file_upload_test.rb index 67de1055a..42556b18e 100644 --- a/test/stripe/file_upload_test.rb +++ b/test/stripe/file_upload_test.rb @@ -42,5 +42,15 @@ class FileUploadTest < Test::Unit::TestCase ) assert file.is_a?(Stripe::FileUpload) end + + should "be deserializable when `object=file`" do + file = Stripe::Util.convert_to_stripe_object({ object: "file" }, {}) + assert file.is_a?(Stripe::FileUpload) + end + + should "be deserializable when `object=file_upload`" do + file = Stripe::Util.convert_to_stripe_object({ object: "file_upload" }, {}) + assert file.is_a?(Stripe::FileUpload) + end end end