Skip to content

Commit 9d15d99

Browse files
committed
🧹 Add a return if json is empty
Sometimes the response is something like: ```json { "data": [], "totalCount": 0 } ``` which is not helpeful, so this commit will add a check and not override the existing data if the returned json data is empty.
1 parent 88fc62b commit 9d15d99

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

lib/tasks/pull.rake

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,32 @@ namespace :arcdex do
181181
sleep(0.5) # Be nice to the API
182182
end
183183

184+
# Validate we got data before saving
185+
if all_cards.empty?
186+
puts "⚠️ WARNING: No cards fetched for set #{id} (#{set_info['name']}). Skipping save to preserve existing data."
187+
return
188+
end
189+
184190
# Create the simplified structure with just data and totalCount
185191
simplified_data = {
186192
'data' => all_cards,
187193
'totalCount' => all_cards.size
188194
}
189195

196+
output_path = Rails.root.join('data', "#{id}.json")
197+
198+
# Check if file exists and compare counts
199+
if output_path.exist?
200+
existing_data = JSON.parse(output_path.read)
201+
existing_count = existing_data['totalCount'] || 0
202+
203+
if all_cards.size < existing_count
204+
puts "⚠️ WARNING: New data has fewer cards (#{all_cards.size}) than existing (#{existing_count}). Skipping save."
205+
return
206+
end
207+
end
208+
190209
puts "💾 Saving #{all_cards.size} cards for set #{id}"
191-
Rails.root.join('data', "#{id}.json").write(JSON.pretty_generate(simplified_data))
210+
output_path.write(JSON.pretty_generate(simplified_data))
192211
end
193212
end

0 commit comments

Comments
 (0)