Class: OrdDb::Cache
- Inherits:
-
Object
- Object
- OrdDb::Cache
- Defined in:
- lib/ordlite/cache.rb
Constant Summary collapse
- Inscribe =
Model::Inscribe
- Blob =
Model::Blob
- TITLE_RX =
“title”: “Inscription 9992615”,
/^Inscription (?<num>[0-9]+)$/i- CONTENT_LENGTH_RX =
/^(?<num>[0-9]+) bytes$/i
Instance Method Summary collapse
- #_content_length_to_bytes(str) ⇒ Object
- #_parse_inscribe(data) ⇒ Object
- #_read_blob(path) ⇒ Object
- #_read_inscribe(path) ⇒ Object
- #_read_text(path) ⇒ Object
- #_title_to_num(str) ⇒ Object
- #import(id) ⇒ Object
- #import_all ⇒ Object
-
#initialize(dir = '.') ⇒ Cache
constructor
A new instance of Cache.
- #read(id) ⇒ Object
Constructor Details
#initialize(dir = '.') ⇒ Cache
Returns a new instance of Cache.
10 11 12 |
# File 'lib/ordlite/cache.rb', line 10 def initialize( dir='.' ) @dir = dir end |
Instance Method Details
#_content_length_to_bytes(str) ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'lib/ordlite/cache.rb', line 106 def _content_length_to_bytes( str ) if m=CONTENT_LENGTH_RX.match( str ) m[:num].to_i(10) ## use base 10 else puts "!! ERROR - bytes found in content lenght >#{str}<" exit 1 ## not found - raise exception - why? why not? end end |
#_parse_inscribe(data) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/ordlite/cache.rb', line 67 def _parse_inscribe( data ) ## num via title attributes = { id: data['id'], num: _title_to_num( data['title'] ), bytes: _content_length_to_bytes( data['content length'] ), sat: data['sat'].to_i(10), content_type: data['content type'], block: data['genesis height'].to_i(10), fee: data['genesis fee'].to_i(10), tx: data['genesis transaction'], address: data['address'], output: data['output'], value: data['output value'].to_i(10), offset: data['offset'].to_i(10), # "2023-06-01 05:00:57 UTC" date: DateTime.strptime( data['timestamp'], '%Y-%m-%d %H:%M:%S %z') } attributes end |
#_read_blob(path) ⇒ Object
56 57 58 59 60 |
# File 'lib/ordlite/cache.rb', line 56 def _read_blob( path ) blob = File.open( path, 'rb' ) { |f| f.read } ## auto force to ASCII-7BIT if not already - why? why not? blob end |
#_read_inscribe(path) ⇒ Object
52 53 54 |
# File 'lib/ordlite/cache.rb', line 52 def _read_inscribe( path ) JSON.parse( _read_text( path )) end |
#_read_text(path) ⇒ Object
63 64 65 |
# File 'lib/ordlite/cache.rb', line 63 def _read_text( path ) File.open( path, 'r:utf-8' ){ |f| f.read } end |
#_title_to_num(str) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/ordlite/cache.rb', line 95 def _title_to_num( str ) if m=TITLE_RX.match( str ) m[:num].to_i(10) ## use base 10 else puts "!! ERROR - no inscribe num found in title >#{str}<" exit 1 ## not found - raise exception - why? why not? end end |
#import(id) ⇒ Object
42 43 44 45 46 |
# File 'lib/ordlite/cache.rb', line 42 def import( id ) data = read( id ) rec = Inscribe.create( _parse_inscribe( data )) rec end |
#import_all ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ordlite/cache.rb', line 16 def import_all paths = Dir.glob( "#{@dir}/**.json" ) puts " #{paths.size} inscribe datafile(s) found" paths.each_with_index do |path, i| puts "==> inscribe #{i+1}/#{paths.size}..." data = _read_inscribe( path ) Inscribe.create( _parse_inscribe( data )) end paths = Dir.glob( "#{@dir}/content/**.txt" ) puts " #{paths.size} content datafile(s) found" paths.each_with_index do |path, i| puts "==> blob #{i+1}/#{paths.size}..." content = _read_blob( path ) id = File.basename( path, File.extname( path )) Blob.create( id: id, content: content ) end end |
#read(id) ⇒ Object
48 49 50 |
# File 'lib/ordlite/cache.rb', line 48 def read( id ) _read_inscribe( "#{@dir}/#{id}.json" ) end |