Class: Ordinals::Cache
- Inherits:
-
Object
- Object
- Ordinals::Cache
- Defined in:
- lib/ordinals/cache.rb
Constant Summary collapse
- INSCRIBE_ID_RX =
INSCRIBE_ID_RX = %r{ /b # boundry (non-alphanum) (?
[a-f0-9]64i) /b # boundry (non-alphanum) }ix %r{ inscription/(?<id>[a-fi0-9]+) }ix
Instance Method Summary collapse
- #_find_blobs ⇒ Object
- #_find_meta ⇒ Object
- #_prepare_csv(path) ⇒ Object
- #_prepare_dir(dir) ⇒ Object
- #_prepare_html(path) ⇒ Object
- #_read_inscribe(path) ⇒ Object
- #add(id) ⇒ Object
- #add_csv(path) ⇒ Object
- #add_dir(dir) ⇒ Object
- #add_html(path) ⇒ Object
- #force=(value) ⇒ Object
-
#force? ⇒ Boolean
config helpers.
-
#initialize(dir) ⇒ Cache
constructor
A new instance of Cache.
- #json_to_txt(data) ⇒ Object
- #read(id) ⇒ Object
- #stats ⇒ Object
Constructor Details
#initialize(dir) ⇒ Cache
Returns a new instance of Cache.
6 7 8 9 |
# File 'lib/ordinals/cache.rb', line 6 def initialize( dir ) @dir = dir @force = false end |
Instance Method Details
#_find_blobs ⇒ Object
30 31 32 33 34 |
# File 'lib/ordinals/cache.rb', line 30 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_meta ⇒ Object
36 37 38 |
# File 'lib/ordinals/cache.rb', line 36 def Dir.glob( "#{@dir}/**/*.meta.txt" ) end |
#_prepare_csv(path) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/ordinals/cache.rb', line 105 def _prepare_csv( path ) ids = [] recs = read_csv( path ) recs.each do |rec| ids << rec['id'] end ids end |
#_prepare_dir(dir) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/ordinals/cache.rb', line 170 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
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/ordinals/cache.rb', line 152 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
204 205 206 |
# File 'lib/ordinals/cache.rb', line 204 def _read_inscribe( path ) read_headers( path ) end |
#add(id) ⇒ Object
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 102 |
# File 'lib/ordinals/cache.rb', line 53 def add( id ) ## step 1) get metadata records = "#{@dir}/#{id[0,2]}/#{id[2..-1]}.meta.txt" = nil if !@force && File.exist?( ) = read_headers( ) print "." else print " meta-#{id} " ## fetch and cache in cache = Ordinals.inscription( id ) pp = json_to_txt( ) write_text( , ) end content_type = ['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} " ## 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
114 115 116 117 118 119 |
# File 'lib/ordinals/cache.rb', line 114 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
188 189 190 191 192 193 194 195 196 |
# File 'lib/ordinals/cache.rb', line 188 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
163 164 165 166 167 168 |
# File 'lib/ordinals/cache.rb', line 163 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 |
#force=(value) ⇒ Object
14 |
# File 'lib/ordinals/cache.rb', line 14 def force=(value) @force=value; end |
#force? ⇒ Boolean
config helpers
13 |
# File 'lib/ordinals/cache.rb', line 13 def force?() @force; end |
#json_to_txt(data) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/ordinals/cache.rb', line 19 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
200 201 202 |
# File 'lib/ordinals/cache.rb', line 200 def read( id ) _read_inscribe( "#{@dir}/#{id[0,2]}/#{id[2..-1]}.meta.txt" ) end |
#stats ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/ordinals/cache.rb', line 40 def stats paths = puts " #{paths.size} inscribe metadatafile(s) found" paths = _find_blobs puts " #{paths.size} inscribe blob(s) found" end |