Class: Ordinals::Cache

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

Constant Summary collapse

INSCRIBE_ID_RX =

INSCRIBE_ID_RX = %r

  /b   # boundry (non-alphanum)
     (?<id>[a-f0-9]{64i[0-9])
  /b   # boundry (non-alphanum)
}ix
%r{
 inscription/(?<id>[a-fi0-9]+)
}ix

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Cache

Returns a new instance of Cache.



6
7
8
9
10
11
# File 'lib/ordinals/cache.rb', line 6

def initialize( dir )
  @dir   = dir   
  @force = false
  @delay_in_s = 0.5  ## wait 0.5s before next (possible) request

  @requests = 0
end

Instance Method Details

#_find_blobsObject



26
27
28
29
30
# File 'lib/ordinals/cache.rb', line 26

def _find_blobs
  ## note:  *.{txt,json,png} will also include *.meta.txt

  ###            CANNOT use, thus, try *i?.*

  Dir.glob( "#{@dir}/**/*i?.{txt,json,png}" )
end

#_find_metaObject



32
33
34
# File 'lib/ordinals/cache.rb', line 32

def _find_meta
  Dir.glob( "#{@dir}/**/*.meta.txt" ) 
end

#_prepare_csv(path) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/ordinals/cache.rb', line 104

def _prepare_csv( path )
  ids = []
  recs = read_csv( path )
  recs.each do |rec|
    ids << rec['id']
  end
  ids
end

#_prepare_dir(dir) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/ordinals/cache.rb', line 169

def _prepare_dir( dir )
    ids = []

   ## in html format

   pages = Dir.glob( "#{dir}/**/*.html")
   pages.each do |path|
     ids += _prepare_html( path )
   end
      
   ### in csv format

   datasets = Dir.glob( "#{dir}/**/*.csv")
   datasets.each do |path|
     ids += _prepare_csv( path )
   end
      
   ids
end

#_prepare_html(path) ⇒ Object



151
152
153
154
155
156
157
158
159
160
# File 'lib/ordinals/cache.rb', line 151

def _prepare_html( path )
    ids = []
    txt = read_text( path )
    txt.scan( INSCRIBE_ID_RX ) do |_|
      m = Regexp.last_match
    
      ids  << m[:id]
    end
    ids
end

#_read_inscribe(path) ⇒ Object



203
204
205
# File 'lib/ordinals/cache.rb', line 203

def _read_inscribe( path )
  read_headers( path ) 
end

#add(id) ⇒ Object



49
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
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ordinals/cache.rb', line 49

def add( id )
   ## step 1)  get metadata records

   meta_path    = "#{@dir}/#{id[0,2]}/#{id[2..-1]}.meta.txt"
      
   meta = nil
   if !@force && File.exist?( meta_path )
      meta = read_headers( meta_path )
      print "."
   else
      print " meta-#{id} "
      sleep( @delay_in_s )   if @delay_in_s && @requests > 0 
 
      ## fetch and cache in cache

      meta = Ordinals.inscription( id )
      pp meta
      meta_txt = json_to_txt( meta )
      
      write_text( meta_path, meta_txt )
      @requests += 1
   end
      
      
   content_type = meta['content-type']
   extname =  if content_type.start_with?( 'text/plain' )
                '.txt'
              elsif content_type.start_with?( 'application/json' )
                '.json'
              elsif content_type.start_with?( 'image/png' )
                '.png'
              else
                puts "!! ERROR - unexpected content type; got: #{content_type}"
                ## pp meta

                exit 1
              end
      
      
   path    = "#{@dir}/#{id[0,2]}/#{id[2..-1]}#{extname}"
   if !@force && File.exist?( path )
       ## puts "  in cache"

       print "."
   else
       print " blob-#{id} "
       sleep( @delay_in_s )   if @delay_in_s && @requests > 0 
       
       ## note: save text as blob - byte-by-byte as is  (might be corrupt text)

       content = Ordinals.content( id )
       ##  pp content

       ## puts "data:"

       ## puts content.data

      
       write_blob( path, content.data )
   end
end

#add_csv(path) ⇒ Object



113
114
115
116
117
118
# File 'lib/ordinals/cache.rb', line 113

def add_csv( path )
  ids = _prepare_csv( path )
  ids = ids.uniq  ## make uniq by default - why? why not?

  puts "   #{ids.size} inscribe id(s)"
  ids.each { |id| add(id) } 
end

#add_dir(dir) ⇒ Object



187
188
189
190
191
192
193
194
195
# File 'lib/ordinals/cache.rb', line 187

def add_dir( dir )
    ids = _prepare_dir( dir )
    puts "   #{ids.size} inscribe id(s)"      
    ## turn into symbol (to make uniq work ??)

    ids = ids.uniq
    pp ids
    puts "   #{ids.size} inscribe id(s) - unique"
    ids.each { |id| add(id) } 
end

#add_html(path) ⇒ Object



162
163
164
165
166
167
# File 'lib/ordinals/cache.rb', line 162

def add_html( path )
    ids = _prepare_html( path )
    ids = ids.uniq  ## make uniq by default - why? why not?

    puts "   #{ids.size} inscribe id(s)"
    ids.each { |id| add(id) } 
end

#json_to_txt(data) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/ordinals/cache.rb', line 15

def json_to_txt( data )
  buf = ''
  data.each do |k,v|
    next if ['preview', 'content'].include?( k )
    buf << "#{k.gsub( ' ', '-')}: #{v}\n"
  end
  buf
end

#read(id) ⇒ Object



199
200
201
# File 'lib/ordinals/cache.rb', line 199

def read( id )
  _read_inscribe( "#{@dir}/#{id[0,2]}/#{id[2..-1]}.meta.txt" ) 
end

#statsObject



36
37
38
39
40
41
42
# File 'lib/ordinals/cache.rb', line 36

def stats
  paths = _find_meta
  puts "  #{paths.size} inscribe metadatafile(s) found"

  paths = _find_blobs
  puts "  #{paths.size} inscribe blob(s) found"
end