stripe 4.22.0 → 4.22.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9af080098c5b87476257bab5cef1ddfc6ba190e710bfb2aa380f50bd318260a1
4
- data.tar.gz: d4686caef64a334b68a61218392f06515df3b987d2bf883b3e87b062b094d2aa
3
+ metadata.gz: 476e36b3d00026b4ecee9ec154dfaed3063cb9faab0ec44d7384e2670715a64c
4
+ data.tar.gz: 67483a0122ac08406cbd2c0b0ee33b332dafae86100ab5439f15f674662d071e
5
5
  SHA512:
6
- metadata.gz: bf08c3b1573899316f8451ad382d2c5a7f3a7b2445e0334156d288b64309f28c6df96a6b0ce4e7255768f9086778a8e55f82666e22c4b97e471f8e83040fce62
7
- data.tar.gz: 2b8dc5dc6f47fe3651dfb404e046fbab8efa3b0c5d0e7aad7d2105e4fe58f393734063a58f3c2d166cbf69df152ec28facb515f40c338cf36376f70402d67279
6
+ metadata.gz: 4632faee7ec59e8eb024bf27c08e98bd97c1b41b5872db0b1d13cb5611770e6ab16154e73ab1989d2a50e6ad7b4e94902395f5af5aa718f99122eef29ac83908
7
+ data.tar.gz: 0bd55c06591145a77defb666ff50ab292c75366d6cd46b315edbad7fd25a12931bce725c4d0d81c1faf67d3294594482e5c9b6f481ed344458d69cade3ec1697
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ## 4.22.1 - 2019-08-09
4
+ * [#808](https://github.com/stripe/stripe-ruby/pull/808) Unify request/response handling
5
+
3
6
  ## 4.22.0 - 2019-07-30
4
7
  * [#821](https://github.com/stripe/stripe-ruby/pull/821) Listing `BalanceTransaction` objects now uses `/v1/balance_transactions` instead of `/v1/balance/history`
5
8
 
@@ -85,7 +88,7 @@
85
88
  * [#718](https://github.com/stripe/stripe-ruby/pull/718) Fix an error message typo
86
89
 
87
90
  ## 4.4.0 - 2018-12-21
88
- * [#716](https://github.com/stripe/stripe-ruby/pull/716) Add support for the `CheckoutSession` resource
91
+ * [#716](https://github.com/stripe/stripe-ruby/pull/716) Add support for the `CheckoutSession` resource
89
92
 
90
93
  ## 4.3.0 - 2018-12-10
91
94
  * [#711](https://github.com/stripe/stripe-ruby/pull/711) Add support for account links
data/VERSION CHANGED
@@ -1 +1 @@
1
- 4.22.0
1
+ 4.22.1
@@ -103,5 +103,16 @@ module Stripe
103
103
  instance.refresh
104
104
  instance
105
105
  end
106
+
107
+ protected def request_stripe_object(method:, path:, params:, opts: {})
108
+ resp, opts = request(method, path, params, opts)
109
+
110
+ # If we're getting back this thing, update; otherwise, instantiate.
111
+ if Util.object_name_matches_class?(resp.data[:object], self.class)
112
+ initialize_from(resp.data, opts)
113
+ else
114
+ Util.convert_to_stripe_object(resp.data, opts)
115
+ end
116
+ end
106
117
  end
107
118
  end
@@ -20,8 +20,12 @@ module Stripe
20
20
  operations: %i[create retrieve update delete list]
21
21
 
22
22
  def reject(params = {}, opts = {})
23
- resp, opts = request(:post, resource_url + "/reject", params, opts)
24
- initialize_from(resp.data, opts)
23
+ request_stripe_object(
24
+ method: :post,
25
+ path: resource_url + "/reject",
26
+ params: params,
27
+ opts: opts
28
+ )
25
29
  end
26
30
 
27
31
  save_nested_resource :external_account
@@ -11,8 +11,12 @@ module Stripe
11
11
  custom_method :void_credit_note, http_verb: :post, http_path: "void"
12
12
 
13
13
  def void_credit_note(params = {}, opts = {})
14
- resp, opts = request(:post, resource_url + "/void", params, opts)
15
- initialize_from(resp.data, opts)
14
+ request_stripe_object(
15
+ method: :post,
16
+ path: resource_url + "/void",
17
+ params: params,
18
+ opts: opts
19
+ )
16
20
  end
17
21
  end
18
22
  end
@@ -10,8 +10,12 @@ module Stripe
10
10
  custom_method :close, http_verb: :post
11
11
 
12
12
  def close(params = {}, opts = {})
13
- resp, opts = request(:post, resource_url + "/close", params, opts)
14
- initialize_from(resp.data, opts)
13
+ request_stripe_object(
14
+ method: :post,
15
+ path: resource_url + "/close",
16
+ params: params,
17
+ opts: opts
18
+ )
15
19
  end
16
20
 
17
21
  def close_url
@@ -16,28 +16,48 @@ module Stripe
16
16
  custom_method :void_invoice, http_verb: :post, http_path: "void"
17
17
 
18
18
  def finalize_invoice(params = {}, opts = {})
19
- resp, opts = request(:post, resource_url + "/finalize", params, opts)
20
- initialize_from(resp.data, opts)
19
+ request_stripe_object(
20
+ method: :post,
21
+ path: resource_url + "/finalize",
22
+ params: params,
23
+ opts: opts
24
+ )
21
25
  end
22
26
 
23
27
  def mark_uncollectible(params = {}, opts = {})
24
- resp, opts = request(:post, resource_url + "/mark_uncollectible", params, opts)
25
- initialize_from(resp.data, opts)
28
+ request_stripe_object(
29
+ method: :post,
30
+ path: resource_url + "/mark_uncollectible",
31
+ params: params,
32
+ opts: opts
33
+ )
26
34
  end
27
35
 
28
36
  def pay(params = {}, opts = {})
29
- resp, opts = request(:post, resource_url + "/pay", params, opts)
30
- initialize_from(resp.data, opts)
37
+ request_stripe_object(
38
+ method: :post,
39
+ path: resource_url + "/pay",
40
+ params: params,
41
+ opts: opts
42
+ )
31
43
  end
32
44
 
33
45
  def send_invoice(params = {}, opts = {})
34
- resp, opts = request(:post, resource_url + "/send", params, opts)
35
- initialize_from(resp.data, opts)
46
+ request_stripe_object(
47
+ method: :post,
48
+ path: resource_url + "/send",
49
+ params: params,
50
+ opts: opts
51
+ )
36
52
  end
37
53
 
38
54
  def void_invoice(params = {}, opts = {})
39
- resp, opts = request(:post, resource_url + "/void", params, opts)
40
- initialize_from(resp.data, opts)
55
+ request_stripe_object(
56
+ method: :post,
57
+ path: resource_url + "/void",
58
+ params: params,
59
+ opts: opts
60
+ )
41
61
  end
42
62
 
43
63
  def self.upcoming(params, opts = {})
@@ -12,13 +12,21 @@ module Stripe
12
12
  custom_method :decline, http_verb: :post
13
13
 
14
14
  def approve(params = {}, opts = {})
15
- resp, opts = request(:post, resource_url + "/approve", params, opts)
16
- initialize_from(resp.data, opts)
15
+ request_stripe_object(
16
+ method: :post,
17
+ path: resource_url + "/approve",
18
+ params: params,
19
+ opts: opts
20
+ )
17
21
  end
18
22
 
19
23
  def decline(params = {}, opts = {})
20
- resp, opts = request(:post, resource_url + "/decline", params, opts)
21
- initialize_from(resp.data, opts)
24
+ request_stripe_object(
25
+ method: :post,
26
+ path: resource_url + "/decline",
27
+ params: params,
28
+ opts: opts
29
+ )
22
30
  end
23
31
  end
24
32
  end
@@ -12,8 +12,12 @@ module Stripe
12
12
  custom_method :details, http_verb: :get
13
13
 
14
14
  def details(params = {}, opts = {})
15
- resp, opts = request(:get, resource_url + "/details", params, opts)
16
- Util.convert_to_stripe_object(resp.data, opts)
15
+ request_stripe_object(
16
+ method: :get,
17
+ path: resource_url + "/details",
18
+ params: params,
19
+ opts: opts
20
+ )
17
21
  end
18
22
  end
19
23
  end
@@ -12,13 +12,21 @@ module Stripe
12
12
  custom_method :return_order, http_verb: :post, http_path: "returns"
13
13
 
14
14
  def pay(params = {}, opts = {})
15
- resp, opts = request(:post, resource_url + "/pay", params, opts)
16
- Util.convert_to_stripe_object(resp.data, opts)
15
+ request_stripe_object(
16
+ method: :post,
17
+ path: resource_url + "/pay",
18
+ params: params,
19
+ opts: opts
20
+ )
17
21
  end
18
22
 
19
23
  def return_order(params = {}, opts = {})
20
- resp, opts = request(:post, resource_url + "/returns", params, opts)
21
- Util.convert_to_stripe_object(resp.data, opts)
24
+ request_stripe_object(
25
+ method: :post,
26
+ path: resource_url + "/returns",
27
+ params: params,
28
+ opts: opts
29
+ )
22
30
  end
23
31
 
24
32
  private def pay_url
@@ -13,18 +13,30 @@ module Stripe
13
13
  custom_method :confirm, http_verb: :post
14
14
 
15
15
  def cancel(params = {}, opts = {})
16
- resp, opts = request(:post, resource_url + "/cancel", params, opts)
17
- initialize_from(resp.data, opts)
16
+ request_stripe_object(
17
+ method: :post,
18
+ path: resource_url + "/cancel",
19
+ params: params,
20
+ opts: opts
21
+ )
18
22
  end
19
23
 
20
24
  def capture(params = {}, opts = {})
21
- resp, opts = request(:post, resource_url + "/capture", params, opts)
22
- initialize_from(resp.data, opts)
25
+ request_stripe_object(
26
+ method: :post,
27
+ path: resource_url + "/capture",
28
+ params: params,
29
+ opts: opts
30
+ )
23
31
  end
24
32
 
25
33
  def confirm(params = {}, opts = {})
26
- resp, opts = request(:post, resource_url + "/confirm", params, opts)
27
- initialize_from(resp.data, opts)
34
+ request_stripe_object(
35
+ method: :post,
36
+ path: resource_url + "/confirm",
37
+ params: params,
38
+ opts: opts
39
+ )
28
40
  end
29
41
  end
30
42
  end
@@ -12,13 +12,21 @@ module Stripe
12
12
  custom_method :detach, http_verb: :post
13
13
 
14
14
  def attach(params = {}, opts = {})
15
- resp, opts = request(:post, resource_url + "/attach", params, opts)
16
- initialize_from(resp.data, opts)
15
+ request_stripe_object(
16
+ method: :post,
17
+ path: resource_url + "/attach",
18
+ params: params,
19
+ opts: opts
20
+ )
17
21
  end
18
22
 
19
23
  def detach(params = {}, opts = {})
20
- resp, opts = request(:post, resource_url + "/detach", params, opts)
21
- initialize_from(resp.data, opts)
24
+ request_stripe_object(
25
+ method: :post,
26
+ path: resource_url + "/detach",
27
+ params: params,
28
+ opts: opts
29
+ )
22
30
  end
23
31
  end
24
32
  end
@@ -11,8 +11,12 @@ module Stripe
11
11
  custom_method :cancel, http_verb: :post
12
12
 
13
13
  def cancel(params = {}, opts = {})
14
- resp, opts = request(:post, resource_url + "/cancel", params, opts)
15
- initialize_from(resp.data, opts)
14
+ request_stripe_object(
15
+ method: :post,
16
+ path: resource_url + "/cancel",
17
+ params: params,
18
+ opts: opts
19
+ )
16
20
  end
17
21
 
18
22
  def cancel_url
@@ -9,8 +9,12 @@ module Stripe
9
9
  custom_method :approve, http_verb: :post
10
10
 
11
11
  def approve(params = {}, opts = {})
12
- resp, opts = request(:post, resource_url + "/approve", params, opts)
13
- initialize_from(resp.data, opts)
12
+ request_stripe_object(
13
+ method: :post,
14
+ path: resource_url + "/approve",
15
+ params: params,
16
+ opts: opts
17
+ )
14
18
  end
15
19
  end
16
20
  end
@@ -12,13 +12,21 @@ module Stripe
12
12
  custom_method :confirm, http_verb: :post
13
13
 
14
14
  def cancel(params = {}, opts = {})
15
- resp, opts = request(:post, resource_url + "/cancel", params, opts)
16
- initialize_from(resp.data, opts)
15
+ request_stripe_object(
16
+ method: :post,
17
+ path: resource_url + "/cancel",
18
+ params: params,
19
+ opts: opts
20
+ )
17
21
  end
18
22
 
19
23
  def confirm(params = {}, opts = {})
20
- resp, opts = request(:post, resource_url + "/confirm", params, opts)
21
- initialize_from(resp.data, opts)
24
+ request_stripe_object(
25
+ method: :post,
26
+ path: resource_url + "/confirm",
27
+ params: params,
28
+ opts: opts
29
+ )
22
30
  end
23
31
  end
24
32
  end
@@ -10,8 +10,12 @@ module Stripe
10
10
  custom_method :verify, http_verb: :post
11
11
 
12
12
  def verify(params = {}, opts = {})
13
- resp, opts = request(:post, resource_url + "/verify", params, opts)
14
- initialize_from(resp.data, opts)
13
+ request_stripe_object(
14
+ method: :post,
15
+ path: resource_url + "/verify",
16
+ params: params,
17
+ opts: opts
18
+ )
15
19
  end
16
20
 
17
21
  def detach(params = {}, opts = {})
@@ -15,13 +15,21 @@ module Stripe
15
15
  nested_resource_class_methods :revision, operations: %i[retrieve list]
16
16
 
17
17
  def cancel(params = {}, opts = {})
18
- resp, opts = request(:post, resource_url + "/cancel", params, opts)
19
- initialize_from(resp.data, opts)
18
+ request_stripe_object(
19
+ method: :post,
20
+ path: resource_url + "/cancel",
21
+ params: params,
22
+ opts: opts
23
+ )
20
24
  end
21
25
 
22
26
  def release(params = {}, opts = {})
23
- resp, opts = request(:post, resource_url + "/release", params, opts)
24
- initialize_from(resp.data, opts)
27
+ request_stripe_object(
28
+ method: :post,
29
+ path: resource_url + "/release",
30
+ params: params,
31
+ opts: opts
32
+ )
25
33
  end
26
34
 
27
35
  def revisions(params = {}, opts = {})
@@ -11,8 +11,12 @@ module Stripe
11
11
  custom_method :cancel, http_verb: :post
12
12
 
13
13
  def cancel(params = {}, opts = {})
14
- resp, opts = request(:post, resource_url + "/cancel", params, opts)
15
- initialize_from(resp.data, opts)
14
+ request_stripe_object(
15
+ method: :post,
16
+ path: resource_url + "/cancel",
17
+ params: params,
18
+ opts: opts
19
+ )
16
20
  end
17
21
  end
18
22
  end
@@ -15,8 +15,12 @@ module Stripe
15
15
  operations: %i[create retrieve update list]
16
16
 
17
17
  def cancel(params = {}, opts = {})
18
- resp, opts = request(:post, resource_url + "/cancel", params, opts)
19
- initialize_from(resp.data, opts)
18
+ request_stripe_object(
19
+ method: :post,
20
+ path: resource_url + "/cancel",
21
+ params: params,
22
+ opts: opts
23
+ )
20
24
  end
21
25
 
22
26
  def cancel_url
@@ -43,6 +43,10 @@ module Stripe
43
43
  @object_classes ||= Stripe::ObjectTypes.object_names_to_classes
44
44
  end
45
45
 
46
+ def self.object_name_matches_class?(object_name, klass)
47
+ Util.object_classes[object_name] == klass
48
+ end
49
+
46
50
  # Converts a hash of fields or an array of hashes into a +StripeObject+ or
47
51
  # array of +StripeObject+s. These new objects will be created as a concrete
48
52
  # type as dictated by their `object` field (e.g. an `object` value of
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Stripe
4
- VERSION = "4.22.0".freeze
4
+ VERSION = "4.22.1".freeze
5
5
  end
@@ -511,6 +511,83 @@ module Stripe
511
511
  end
512
512
  end
513
513
 
514
+ context "#request_stripe_object" do
515
+ class HelloTestAPIResource < APIResource
516
+ OBJECT_NAME = "hello".freeze
517
+ def say_hello(params = {}, opts = {})
518
+ request_stripe_object(
519
+ method: :post,
520
+ path: resource_url + "/say",
521
+ params: params,
522
+ opts: opts
523
+ )
524
+ end
525
+ end
526
+
527
+ setup do
528
+ Util.instance_variable_set(
529
+ :@object_classes,
530
+ Stripe::ObjectTypes.object_names_to_classes.merge(
531
+ "hello" => HelloTestAPIResource
532
+ )
533
+ )
534
+ end
535
+ teardown do
536
+ Util.class.instance_variable_set(:@object_classes, Stripe::ObjectTypes.object_names_to_classes)
537
+ end
538
+
539
+ should "make requests appropriately" do
540
+ stub_request(:post, "#{Stripe.api_base}/v1/hellos/hi_123/say")
541
+ .with(body: { foo: "bar" }, headers: { "Stripe-Account" => "acct_hi" })
542
+ .to_return(body: JSON.generate("object" => "hello"))
543
+
544
+ hello = HelloTestAPIResource.new(id: "hi_123")
545
+ hello.say_hello({ foo: "bar" }, stripe_account: "acct_hi")
546
+ end
547
+
548
+ should "update attributes in-place when it returns the same thing" do
549
+ stub_request(:post, "#{Stripe.api_base}/v1/hellos/hi_123/say")
550
+ .to_return(body: JSON.generate("object" => "hello", "additional" => "attribute"))
551
+
552
+ hello = HelloTestAPIResource.new(id: "hi_123")
553
+ hello.unsaved = "a value"
554
+ new_hello = hello.say_hello
555
+
556
+ # Doesn't matter if you use the return variable or the instance.
557
+ assert_equal(hello, new_hello)
558
+
559
+ # It updates new attributes in-place.
560
+ assert_equal("attribute", hello.additional)
561
+
562
+ # It removes unsaved attributes, but at least lets you know about them.
563
+ e = assert_raises(NoMethodError) { hello.unsaved }
564
+ assert_match("The 'unsaved' attribute was set in the past", e.message)
565
+ end
566
+
567
+ should "instantiate a new object of the appropriate class when it is different than the host class" do
568
+ stub_request(:post, "#{Stripe.api_base}/v1/hellos/hi_123/say")
569
+ .to_return(body: JSON.generate("object" => "goodbye", "additional" => "attribute"))
570
+
571
+ hello = HelloTestAPIResource.new(id: "hi_123")
572
+ hello.unsaved = "a value"
573
+ new_goodbye = hello.say_hello
574
+
575
+ # The returned value and the instance are different objects.
576
+ refute_equal(new_goodbye, hello)
577
+
578
+ # The returned value has stuff from the server.
579
+ assert_equal("attribute", new_goodbye.additional)
580
+ assert_equal("goodbye", new_goodbye.object)
581
+
582
+ # You instance doesn't have stuff from the server.
583
+ e = assert_raises(NoMethodError) { hello.additional }
584
+ refute_match(/was set in the past/, e.message)
585
+
586
+ # The instance preserves unset attributes on the original instance (not sure this is good behavior?)
587
+ assert_equal("a value", hello.unsaved)
588
+ end
589
+ end
590
+
514
591
  @@fixtures = {} # rubocop:disable Style/ClassVars
515
592
  setup do
516
593
  if @@fixtures.empty?
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stripe
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.22.0
4
+ version: 4.22.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stripe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-07-30 00:00:00.000000000 Z
11
+ date: 2019-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday