1+ require 'zlib'
2+ require 'rubygems/package'
3+
14# Strict parser for the ERC-721 Ethscriptions collection protocol with canonical JSON validation
25class Erc721EthscriptionsCollectionParser
36 # Default return for invalid input
@@ -125,6 +128,7 @@ class ValidationError < StandardError; end
125128
126129 DEFAULT_ITEMS_PATH = ENV [ 'COLLECTIONS_ITEMS_PATH' ] || Rails . root . join ( 'items_by_ethscription.json' )
127130 DEFAULT_COLLECTIONS_PATH = ENV [ 'COLLECTIONS_META_PATH' ] || Rails . root . join ( 'collections_by_name.json' )
131+ DEFAULT_ARCHIVE_PATH = ENV [ 'COLLECTIONS_ARCHIVE_PATH' ] || Rails . root . join ( 'collections_data.tar.gz' )
128132
129133 # New API: validate and encode protocol params
130134 # Unified interface - accepts all possible parameters, uses what it needs
@@ -264,6 +268,47 @@ class << self
264268 include Memery
265269
266270 def load_import_data ( items_path :, collections_path :)
271+ archive_path = DEFAULT_ARCHIVE_PATH
272+
273+ # Check if we need to extract from the tar.gz archive
274+ if File . exist? ( archive_path )
275+ # Check if JSON files don't exist or archive is newer
276+ extract_needed = !File . exist? ( items_path ) || !File . exist? ( collections_path ) ||
277+ File . mtime ( archive_path ) > File . mtime ( items_path ) ||
278+ File . mtime ( archive_path ) > File . mtime ( collections_path )
279+
280+ if extract_needed
281+ Rails . logger . info "Extracting collections data from #{ archive_path } " if defined? ( Rails )
282+
283+ # Extract tar.gz archive
284+ Zlib ::GzipReader . open ( archive_path ) do |gz |
285+ Gem ::Package ::TarReader . new ( gz ) do |tar |
286+ tar . each do |entry |
287+ if entry . file?
288+ case entry . full_name
289+ when 'items_by_ethscription.json'
290+ File . open ( items_path , 'wb' ) do |f |
291+ f . write ( entry . read )
292+ end
293+ Rails . logger . info "Extracted #{ entry . full_name } to #{ items_path } " if defined? ( Rails )
294+ when 'collections_by_name.json'
295+ File . open ( collections_path , 'wb' ) do |f |
296+ f . write ( entry . read )
297+ end
298+ Rails . logger . info "Extracted #{ entry . full_name } to #{ collections_path } " if defined? ( Rails )
299+ end
300+ end
301+ end
302+ end
303+ end
304+ end
305+ end
306+
307+ # Ensure files exist before reading
308+ unless File . exist? ( items_path ) && File . exist? ( collections_path )
309+ raise "Collections data files not found. Please ensure #{ archive_path } exists or provide JSON files directly."
310+ end
311+
267312 items = JSON . parse ( File . read ( items_path ) )
268313 collections = JSON . parse ( File . read ( collections_path ) )
269314
@@ -275,7 +320,7 @@ def load_import_data(items_path:, collections_path:)
275320 items_by_id . each do |iid , it |
276321 cname = it [ 'collection_name' ]
277322 next unless cname . is_a? ( String ) && !cname . empty?
278- num = it [ 'ethscription_number' ] . to_i
323+ num = it . fetch ( 'ethscription_number' ) . to_i
279324 groups [ cname ] << [ iid , num ]
280325 end
281326
0 commit comments