stripe 5.6.0 → 5.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b717ef70627c0f7467317a2e2f08e8d553b04411b6324df41f4cc5eac6bd6800
4
- data.tar.gz: 26a05a4ceeb4412c95c2ad12776785c72bd291841c5843eba2ec03a6dc21a722
3
+ metadata.gz: 8572565264d4271941fd03cf4cf4f9e02a8f98bfd918193b9b1ce18f3c9ba8ee
4
+ data.tar.gz: e2c2f14fcc9429ab22f9ca25bcdab3a80425cab368f7638e2a26ea9698a2a9ec
5
5
  SHA512:
6
- metadata.gz: ef1b962195d8a8510a1d360d8fbd18a246c65363877b43a451bccd1340c1ea18008581c04c4a0e62eb6eed9f2f8afebfccf9c4f5d059b08a2ccd921cae5b5cd0
7
- data.tar.gz: 98fb62f907a16aadb4cab5564053ce85e96931193a8e1eeccb8ef10ebcfa726ddf086e51aef09db0825c685129867d5a91716addcc2252ae771869ca4d83bc8a
6
+ metadata.gz: 4abc40405003b613b3361999bbc6a8be9be66a821826535f29ee6f2843a265b9148ef1ebb22b7aaa48539aabde2358598bccb684460cc9c07f19a0ec47b2bd3e
7
+ data.tar.gz: c052b3c37a92cafc3801c1c242bf55fddc3e8ed335783da746b7214bba63061478697d3d95bc540cacdd8d0d2f049d0836d4e202fd75923840284408d5de006f
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ## 5.7.0 - 2019-10-10
4
+ * [#865](https://github.com/stripe/stripe-ruby/pull/865) Support backwards pagination with list's `#auto_paging_each`
5
+
3
6
  ## 5.6.0 - 2019-10-04
4
7
  * [#861](https://github.com/stripe/stripe-ruby/pull/861) Nicer error when specifying non-nil non-string opt value
5
8
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 5.6.0
1
+ 5.7.0
@@ -11,12 +11,7 @@ module Stripe
11
11
 
12
12
  # set filters so that we can fetch the same limit, expansions, and
13
13
  # predicates when accessing the next and previous pages
14
- #
15
- # just for general cleanliness, remove any paging options
16
14
  obj.filters = filters.dup
17
- obj.filters.delete(:ending_before)
18
- obj.filters.delete(:starting_after)
19
-
20
15
  obj
21
16
  end
22
17
  end
@@ -59,8 +59,18 @@ module Stripe
59
59
 
60
60
  page = self
61
61
  loop do
62
- page.each(&blk)
63
- page = page.next_page
62
+ # Backward iterating activates if we have an `ending_before` constraint
63
+ # and _just_ an `ending_before` constraint. If `starting_after` was
64
+ # also used, we iterate forwards normally.
65
+ if filters.include?(:ending_before) &&
66
+ !filters.include?(:starting_after)
67
+ page.reverse_each(&blk)
68
+ page = page.previous_page
69
+ else
70
+ page.each(&blk)
71
+ page = page.next_page
72
+ end
73
+
64
74
  break if page.empty?
65
75
  end
66
76
  end
@@ -96,6 +106,8 @@ module Stripe
96
106
  # This method will try to respect the limit of the current page. If none
97
107
  # was given, the default limit will be fetched again.
98
108
  def previous_page(params = {}, opts = {})
109
+ return self.class.empty_list(opts) unless has_more
110
+
99
111
  first_id = data.first.id
100
112
 
101
113
  params = filters.merge(ending_before: first_id).merge(params)
@@ -107,5 +119,11 @@ module Stripe
107
119
  url ||
108
120
  raise(ArgumentError, "List object does not contain a 'url' field.")
109
121
  end
122
+
123
+ # Iterates through each resource in the page represented by the current
124
+ # `ListObject` in reverse.
125
+ def reverse_each(&blk)
126
+ data.reverse_each(&blk)
127
+ end
110
128
  end
111
129
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Stripe
4
- VERSION = "5.6.0"
4
+ VERSION = "5.7.0"
5
5
  end
@@ -25,20 +25,80 @@ module Stripe
25
25
  assert_equal expected, list.each.to_a
26
26
  end
27
27
 
28
- should "provide #auto_paging_each" do
28
+ should "provide #reverse_each" do
29
29
  arr = [
30
30
  { id: 1 },
31
31
  { id: 2 },
32
32
  { id: 3 },
33
33
  ]
34
+ expected = Util.convert_to_stripe_object(arr.reverse, {})
35
+ list = Stripe::ListObject.construct_from(data: arr)
36
+ assert_equal expected, list.reverse_each.to_a
37
+ end
38
+
39
+ should "provide #auto_paging_each that supports forward pagination" do
40
+ arr = [
41
+ { id: 1 },
42
+ { id: 2 },
43
+ { id: 3 },
44
+ { id: 4 },
45
+ { id: 5 },
46
+ { id: 6 },
47
+ ]
34
48
  expected = Util.convert_to_stripe_object(arr, {})
35
49
 
50
+ # Initial list object to page on. Notably, its last data element will be
51
+ # used as a cursor to fetch the next page.
36
52
  list = TestListObject.construct_from(data: [{ id: 1 }],
37
53
  has_more: true,
38
54
  url: "/things")
55
+ list.filters = { limit: 3 }
56
+
57
+ # The test will start with the synthetic list object above, and use it as
58
+ # a starting point to fetch two more pages. The second page indicates
59
+ # that there are no more elements by setting `has_more` to `false`, and
60
+ # iteration stops.
39
61
  stub_request(:get, "#{Stripe.api_base}/things")
40
- .with(query: { starting_after: "1" })
41
- .to_return(body: JSON.generate(data: [{ id: 2 }, { id: 3 }], has_more: false))
62
+ .with(query: { starting_after: "1", limit: "3" })
63
+ .to_return(body: JSON.generate(data: [{ id: 2 }, { id: 3 }, { id: 4 }], has_more: true, url: "/things"))
64
+ stub_request(:get, "#{Stripe.api_base}/things")
65
+ .with(query: { starting_after: "4", limit: "3" })
66
+ .to_return(body: JSON.generate(data: [{ id: 5 }, { id: 6 }], has_more: false, url: "/things"))
67
+
68
+ assert_equal expected, list.auto_paging_each.to_a
69
+ end
70
+
71
+ should "provide #auto_paging_each that supports backward pagination with `ending_before`" do
72
+ arr = [
73
+ { id: 6 },
74
+ { id: 5 },
75
+ { id: 4 },
76
+ { id: 3 },
77
+ { id: 2 },
78
+ { id: 1 },
79
+ ]
80
+ expected = Util.convert_to_stripe_object(arr, {})
81
+
82
+ # Initial list object to page on. Notably, its first data element will be
83
+ # used as a cursor to fetch the next page.
84
+ list = TestListObject.construct_from(data: [{ id: 6 }],
85
+ has_more: true,
86
+ url: "/things")
87
+
88
+ # We also add an `ending_before` filter on the list to simulate backwards
89
+ # pagination.
90
+ list.filters = { ending_before: 7, limit: 3 }
91
+
92
+ # The test will start with the synthetic list object above, and use it as
93
+ # a starting point to fetch two more pages. The second page indicates
94
+ # that there are no more elements by setting `has_more` to `false`, and
95
+ # iteration stops.
96
+ stub_request(:get, "#{Stripe.api_base}/things")
97
+ .with(query: { ending_before: "6", limit: "3" })
98
+ .to_return(body: JSON.generate(data: [{ id: 3 }, { id: 4 }, { id: 5 }], has_more: true, url: "/things"))
99
+ stub_request(:get, "#{Stripe.api_base}/things")
100
+ .with(query: { ending_before: "3", limit: "3" })
101
+ .to_return(body: JSON.generate(data: [{ id: 1 }, { id: 2 }], has_more: false, url: "/things"))
42
102
 
43
103
  assert_equal expected, list.auto_paging_each.to_a
44
104
  end
@@ -97,7 +157,7 @@ module Stripe
97
157
  .with(query: { "expand[]" => "data.source", "limit" => "3", "starting_after" => "1" })
98
158
  .to_return(body: JSON.generate(data: [{ id: 2 }], has_more: false))
99
159
  next_list = list.next_page
100
- assert_equal({ expand: ["data.source"], limit: 3 }, next_list.filters)
160
+ assert_equal({ expand: ["data.source"], limit: 3, starting_after: 1 }, next_list.filters)
101
161
  end
102
162
 
103
163
  should "fetch an empty page through #next_page" do
@@ -114,23 +174,25 @@ module Stripe
114
174
 
115
175
  should "fetch a next page through #previous_page" do
116
176
  list = TestListObject.construct_from(data: [{ id: 2 }],
177
+ has_more: true,
117
178
  url: "/things")
118
179
  stub_request(:get, "#{Stripe.api_base}/things")
119
180
  .with(query: { ending_before: "2" })
120
- .to_return(body: JSON.generate(data: [{ id: 1 }]))
181
+ .to_return(body: JSON.generate(data: [{ id: 1 }], has_more: false))
121
182
  next_list = list.previous_page
122
183
  refute next_list.empty?
123
184
  end
124
185
 
125
186
  should "fetch a next page through #previous_page and respect limit" do
126
187
  list = TestListObject.construct_from(data: [{ id: 2 }],
188
+ has_more: true,
127
189
  url: "/things")
128
190
  list.filters = { expand: ["data.source"], limit: 3 }
129
191
  stub_request(:get, "#{Stripe.api_base}/things")
130
192
  .with(query: { "expand[]" => "data.source", "limit" => "3", "ending_before" => "2" })
131
- .to_return(body: JSON.generate(data: [{ id: 1 }]))
193
+ .to_return(body: JSON.generate(data: [{ id: 1 }], has_more: false))
132
194
  next_list = list.previous_page
133
- assert_equal({ expand: ["data.source"], limit: 3 }, next_list.filters)
195
+ assert_equal({ ending_before: 2, expand: ["data.source"], limit: 3 }, next_list.filters)
134
196
  end
135
197
  end
136
198
  end
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: 5.6.0
4
+ version: 5.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stripe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-04 00:00:00.000000000 Z
11
+ date: 2019-10-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Stripe is the easiest way to accept payments online. See https://stripe.com
14
14
  for details.