Class: Unisat::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/unisat/cache.rb

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Cache

Returns a new instance of Cache.



44
45
46
# File 'lib/unisat/cache.rb', line 44

def initialize( dir )
  @dir = dir
end

Instance Method Details

#add_page(page, key, offset:) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/unisat/cache.rb', line 96

def add_page( page, key, offset: )
   data = Unisat.parse_page( page )

   ## note: only write if results > 0
   if data.size > 0
      outpath = "#{@dir}/#{key}/#{offset}.json"
      write_json( outpath, data )   
   else
      puts "!! WARN - no results found in page #{offset} for key >#{key}<"
   end
end

#exist?(key, offset:) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
# File 'lib/unisat/cache.rb', line 91

def exist?( key, offset: )
  outpath = "#{@dir}/#{key}/#{offset}.json"
  File.exist?( outpath )
end

#read(key) ⇒ Object

todo:

  • add offset: nil, limit: nil - why? why not?



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/unisat/cache.rb', line 50

def read( key ) 
    recs = []

   paths = Dir.glob( "#{@dir}/#{key}/*.json" )
   puts "   #{paths.size} page(s) in cache"

   paths.each_with_index do |path,i|
     results = read_json( path )
     puts "==> #{i+1}/#{paths.size} - #{results.size} inscribe(s) in >#{path}<..."

     results.each do |h|
         date = h['date']  # note: might include unconfirmed!!!
        ##  always "auto-magically" filter out unconfirmed for now 
        if date == 'unconfirmed'
           puts "  !! INFO - skipping unconfirmed inscribe"
           next
        end

        ## todo/fix:
        ##  reformat date here/parse
        ##   and change to iso e.g. 2023-12-24 17:18 or such - why? why not!!!!


        ## id -- split href by / and get last part (is id)
        id  = h['href'].split( '/')[-1] 
        ## num -- remove/ strip leadingnumber sign (#)  
        num = h['num'].sub( '#', '' )  
        recs << { 
           'id'      => id,
           'num'     => num,
           'date'    => date,
           'address' => h['address'],
           'text'    => h['name'],  ## change name to text
        }
     end
    end
    puts "   #{recs.size} inscribe(s) - total"
    recs
end