Skip to content

Commit

Permalink
Handle file objects like file_upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Sep 17, 2018
1 parent 0b4802c commit f3dd3ab
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 10 additions & 2 deletions lib/stripe/file_upload.rb → lib/stripe/file.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
3 changes: 2 additions & 1 deletion lib/stripe/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
56 changes: 56 additions & 0 deletions test/stripe/file_test.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions test/stripe/file_upload_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit f3dd3ab

Please sign in to comment.