Class: Unisat::Cache
- Inherits:
-
Object
- Object
- Unisat::Cache
- Defined in:
- lib/unisat/cache.rb
Instance Method Summary collapse
- #_normalize(results) ⇒ Object
- #add_page(page, key, offset:) ⇒ Object
- #exist?(key, offset:) ⇒ Boolean
-
#initialize(dir) ⇒ Cache
constructor
A new instance of Cache.
-
#read(key) ⇒ Object
todo: - add offset: nil, limit: nil - why? why not?.
- #read_page(key, offset:) ⇒ Object
Constructor Details
#initialize(dir) ⇒ Cache
45 46 47 |
# File 'lib/unisat/cache.rb', line 45 def initialize( dir ) @dir = dir end |
Instance Method Details
#_normalize(results) ⇒ Object
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 |
# File 'lib/unisat/cache.rb', line 68 def _normalize( results ) recs = [] 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 recs end |
#add_page(page, key, offset:) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/unisat/cache.rb', line 111 def add_page( page, key, offset: ) results = Unisat.parse_page( page ) ## note: only write if results > 0 if results.size > 0 outpath = "#{@dir}/#{key}/#{offset}.json" write_json( outpath, results ) else puts "!! WARN - no results found in page #{offset} for key >#{key}<" end recs = _normalize( results ) recs end |
#exist?(key, offset:) ⇒ Boolean
98 99 100 101 |
# File 'lib/unisat/cache.rb', line 98 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?
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/unisat/cache.rb', line 51 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}<..." recs += _normalize( results ) end puts " #{recs.size} inscribe(s) - total" recs end |
#read_page(key, offset:) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/unisat/cache.rb', line 103 def read_page( key, offset: ) outpath = "#{@dir}/#{key}/#{offset}.json" results = read_json( outpath ) recs = _normalize( results ) recs end |