Skip to content

Commit 7c82f4a

Browse files
fern-supportclaude
andcommitted
chore: drop stale fernignore overrides for iterators and pagination clients
Remove item_iterator.rb, inventory/client.rb and transfer_orders/client.rb from .fernignore and take the generated 1.40.0 versions. The hand-patched item_iterator.rb predates track_http_responses, which the regenerated cursor_item_iterator.rb calls, and the generator now handles nil items and body-cursor pagination itself. The old inventory client was also missing 7 endpoints. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 336bf51 commit 7c82f4a

5 files changed

Lines changed: 1156 additions & 182 deletions

File tree

‎.fernignore‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@ lib/square/reporting_helper.rb
1414
Gemfile.custom
1515
custom.gemspec.rb
1616

17-
# Pagination is currently broken for endpoints where the page token is not submitted via a query param
18-
# Manually fixed pagination bugs in:
19-
lib/square/inventory/client.rb
20-
lib/square/transfer_orders/client.rb
21-
22-
# Iterator does not handle nil item fields (when API returns nil instead of empty array for no items)
23-
# Manually added nil checks to avoid calling .shift/.empty? on nil
24-
lib/square/internal/iterators/item_iterator.rb
2517
.fern/replay.lock
2618
.fern/replay.yml
2719
.gitattributes

‎Gemfile.lock‎

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
PATH
2+
remote: .
3+
specs:
4+
square.rb (48.0.0.20260916)
5+
apimatic_core (~> 0.3.11)
6+
apimatic_core_interfaces (~> 0.2.1)
7+
apimatic_faraday_client_adapter (~> 0.1.4)
8+
9+
GEM
10+
remote: https://rubygems.org/
11+
specs:
12+
addressable (2.9.0)
13+
public_suffix (>= 2.0.2, < 8.0)
14+
apimatic_core (0.3.21)
15+
apimatic_core_interfaces (~> 0.2.0)
16+
certifi (~> 2018.1, >= 2018.01.18)
17+
faraday-multipart (~> 1.0)
18+
nokogiri (~> 1.13, >= 1.13.10)
19+
apimatic_core_interfaces (0.2.3)
20+
apimatic_faraday_client_adapter (0.1.6)
21+
apimatic_core_interfaces (~> 0.2.0)
22+
certifi (~> 2018.1, >= 2018.01.18)
23+
faraday (~> 2.0, >= 2.0.1)
24+
faraday-follow_redirects (~> 0.2)
25+
faraday-gzip (>= 1, < 4)
26+
faraday-http-cache (~> 2.2)
27+
faraday-multipart (~> 1.0)
28+
faraday-net_http_persistent (~> 2.0)
29+
faraday-retry (~> 2.0)
30+
ast (2.4.3)
31+
base64 (0.3.0)
32+
bigdecimal (4.1.3)
33+
certifi (2018.01.18)
34+
coderay (1.1.3)
35+
connection_pool (3.0.2)
36+
crack (1.0.1)
37+
bigdecimal
38+
rexml
39+
faraday (2.15.1)
40+
faraday-net_http (>= 2.0, < 3.6)
41+
json
42+
logger
43+
faraday-follow_redirects (0.5.0)
44+
faraday (>= 1, < 3)
45+
faraday-gzip (3.2.0)
46+
faraday (>= 2.0, < 3)
47+
zlib (~> 3.0)
48+
faraday-http-cache (2.8.0)
49+
faraday (>= 0.8)
50+
faraday-multipart (1.2.0)
51+
multipart-post (~> 2.0)
52+
faraday-net_http (3.5.0)
53+
net-http (~> 0.5)
54+
faraday-net_http_persistent (2.3.1)
55+
faraday (~> 2.5)
56+
net-http-persistent (>= 4.0.4, < 5)
57+
faraday-retry (2.4.0)
58+
faraday (~> 2.0)
59+
hashdiff (1.2.1)
60+
io-console (0.9.4)
61+
json (3.1.0)
62+
language_server-protocol (3.17.0.6)
63+
lint_roller (1.1.0)
64+
logger (1.7.0)
65+
method_source (1.1.0)
66+
minitest (5.27.0)
67+
minitest-proveit (1.0.1)
68+
minitest (> 5, < 7)
69+
minitest-rg (5.4.0)
70+
minitest (>= 5.0, < 7)
71+
multipart-post (2.4.1)
72+
mutex_m (0.3.0)
73+
net-http (0.10.0)
74+
net-protocol (>= 0.3.0)
75+
uri (>= 0.11.1)
76+
net-http-persistent (4.0.8)
77+
connection_pool (>= 2.2.4, < 4)
78+
net-protocol (0.4.0)
79+
timeout
80+
nokogiri (1.19.4-aarch64-linux-gnu)
81+
racc (~> 1.4)
82+
nokogiri (1.19.4-aarch64-linux-musl)
83+
racc (~> 1.4)
84+
nokogiri (1.19.4-arm-linux-gnu)
85+
racc (~> 1.4)
86+
nokogiri (1.19.4-arm-linux-musl)
87+
racc (~> 1.4)
88+
nokogiri (1.19.4-arm64-darwin)
89+
racc (~> 1.4)
90+
nokogiri (1.19.4-x86_64-darwin)
91+
racc (~> 1.4)
92+
nokogiri (1.19.4-x86_64-linux-gnu)
93+
racc (~> 1.4)
94+
nokogiri (1.19.4-x86_64-linux-musl)
95+
racc (~> 1.4)
96+
parallel (2.3.0)
97+
parser (3.3.13.0)
98+
ast (~> 2.4.1)
99+
racc
100+
prism (1.9.0)
101+
pry (0.16.1)
102+
coderay (~> 1.1)
103+
method_source (~> 1.0)
104+
reline (>= 0.6.0)
105+
public_suffix (7.0.5)
106+
racc (1.8.1)
107+
rainbow (3.1.1)
108+
rake (13.4.2)
109+
regexp_parser (2.13.1)
110+
reline (0.7.0)
111+
io-console (~> 0.5)
112+
rexml (3.4.4)
113+
rubocop (1.92.1)
114+
json (>= 2.3)
115+
language_server-protocol (~> 3.17.0.2)
116+
lint_roller (~> 1.1.0)
117+
parallel (>= 1.10)
118+
parser (>= 3.3.0.2)
119+
rainbow (>= 2.2.2, < 4.0)
120+
regexp_parser (>= 2.11.3, < 3.0)
121+
rubocop-ast (>= 1.49.0, < 2.0)
122+
ruby-progressbar (~> 1.7)
123+
unicode-display_width (>= 2.4.0, < 4.0)
124+
rubocop-ast (1.50.0)
125+
parser (>= 3.3.7.2)
126+
prism (~> 1.7)
127+
rubocop-minitest (0.41.0)
128+
lint_roller (~> 1.1)
129+
rubocop (>= 1.89.0, < 2.0)
130+
rubocop-ast (>= 1.38.0, < 2.0)
131+
ruby-progressbar (1.13.0)
132+
timeout (0.6.1)
133+
unicode-display_width (3.3.0)
134+
unicode-emoji (~> 4.3)
135+
unicode-emoji (4.3.0)
136+
uri (1.1.2)
137+
webmock (3.34.0)
138+
addressable (>= 2.8.0)
139+
crack (>= 0.3.2)
140+
hashdiff (>= 0.4.0, < 2.0.0)
141+
zlib (3.2.4)
142+
143+
PLATFORMS
144+
aarch64-linux-gnu
145+
aarch64-linux-musl
146+
arm-linux-gnu
147+
arm-linux-musl
148+
arm64-darwin
149+
x86_64-darwin
150+
x86_64-linux-gnu
151+
x86_64-linux-musl
152+
153+
DEPENDENCIES
154+
base64
155+
minitest (~> 5.16)
156+
minitest-proveit (~> 1.0)
157+
minitest-rg
158+
mutex_m
159+
pry
160+
rake (~> 13.0)
161+
rubocop (~> 1.21)
162+
rubocop-minitest
163+
square.rb!
164+
webmock
165+
166+
BUNDLED WITH
167+
2.5.22

‎lib/square/internal/iterators/item_iterator.rb‎

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,35 @@ module Internal
55
class ItemIterator
66
include Enumerable
77

8-
# Iterates over each item returned by the API.
8+
# The raw HTTP response from the most recent page request made by this pager or its `pages`.
9+
# @return [Net::HTTPResponse, nil]
10+
attr_reader :http_response
11+
12+
# Iterates over each item returned by the API, starting again from the first page on every call.
13+
# Each loop is independent of other loops and of `next_element`.
914
#
1015
# @param block [Proc] The block which each retrieved item is yielded to.
11-
# @return [NilClass]
12-
def each(&block)
13-
while (item = next_element)
14-
block.call(item)
16+
# @return [NilClass, Enumerator] An Enumerator when no block is given.
17+
def each(&)
18+
return enum_for(:each) unless block_given?
19+
20+
# Each loop reads through its own copy of the page stream, so repeated or nested loops never
21+
# share a position with each other or with `next_element`.
22+
pass = @item_pages.dup.tap(&:rewind)
23+
while (page = pass.next_page)
24+
(read_field(page, @item_field) || []).each(&)
1525
end
26+
nil
27+
end
28+
29+
# Resets manual item-by-item iteration (`next_element` / `next?`) to the first page.
30+
#
31+
# @return [NilClass]
32+
def rewind
33+
@item_pages.rewind
34+
@page = nil
35+
@item_index = 0
36+
nil
1637
end
1738

1839
# Whether another item will be available from the API.
@@ -28,6 +49,19 @@ def next?
2849
any_items_in_cached_page?
2950
end
3051

52+
# Sends the request for the first page now instead of on the first iteration, so an API error for that page
53+
# is raised here. The page is kept, and every loop (over items or `pages`) that starts from the first page
54+
# reuses it instead of requesting it again. Does nothing if this pager already sent a request.
55+
#
56+
# @return [self]
57+
def load_first_page
58+
return self if @requested
59+
60+
@item_pages.load_first_page
61+
@page_iterator.reuse_first_page(@item_pages)
62+
self
63+
end
64+
3165
# Retrieves the next item from the API.
3266
def next_element
3367
item = next_item_from_cached_page
@@ -39,26 +73,47 @@ def next_element
3973

4074
private
4175

42-
def next_item_from_cached_page
43-
return unless @page
76+
# Wraps the page-fetching block so that `http_response` reflects the most recent request.
77+
def track_http_responses(get_page)
78+
proc do |*args|
79+
result = get_page.call(*args)
80+
@requested = true
81+
@http_response = result[1] if result.is_a?(Array)
82+
result
83+
end
84+
end
4485

45-
items = @page.send(@item_field)
46-
return nil if items.nil?
86+
def cached_page_items
87+
return [] unless @page
4788

48-
items.shift
89+
read_field(@page, @item_field) || []
4990
end
5091

51-
def any_items_in_cached_page?
52-
return false unless @page
92+
def next_item_from_cached_page
93+
items = cached_page_items
94+
return if @item_index >= items.length
5395

54-
items = @page.send(@item_field)
55-
return false if items.nil?
96+
item = items[@item_index]
97+
@item_index += 1
98+
item
99+
end
56100

57-
!items.empty?
101+
def any_items_in_cached_page?
102+
@item_index < cached_page_items.length
58103
end
59104

60105
def load_next_page
61-
@page = @page_iterator.next_page
106+
@page = @item_pages.next_page
107+
@item_index = 0
108+
end
109+
110+
# Reads a field from an API response, walking through enclosing objects when the field is nested.
111+
#
112+
# @param value [Object, nil] The API response.
113+
# @param field [Symbol, Array<Symbol>] The field name, or the path of field names for a nested field.
114+
# @return [Object, nil]
115+
def read_field(value, field)
116+
Array(field).reduce(value) { |object, name| object&.send(name) }
62117
end
63118
end
64119
end

0 commit comments

Comments
 (0)