Class: Ordinals::Sandbox

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

Constant Summary collapse

ID_RX =
/[a-f0-9]{64}i[0-9]/x

Instance Method Summary collapse

Constructor Details

#initialize(dir = './content') ⇒ Sandbox

Returns a new instance of Sandbox.



5
6
7
8
# File 'lib/ordinals/sandbox.rb', line 5

def initialize( dir='./content' )
   @dir   = dir
   @force      = false
end

Instance Method Details

#_find_ids(values) ⇒ Object



68
69
70
# File 'lib/ordinals/sandbox.rb', line 68

def _find_ids( values )
   values.select { |val| val.is_a?(String) && ID_RX.match(val) }      
end

#add(id) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ordinals/sandbox.rb', line 21

def add( id )
   path = "#{@dir}/#{id}"
   if !@force && File.exist?( path )
      ## puts "  in sandbox"
   else        
     ## note: save text as blob - byte-by-byte as is  (might be corrupt text)
     content = Ordinals.content( id )
     ## pp content
     #=> #<Ordinals::Api::Content:0x000001a1352df938
     #      @data="RIFF\xF8\v\x00\x00WEBPVP8 \xEC\v\x00\x00...",
     #      @length=3072,
     #      @type="image/png"
  
     ## puts "data:"
     ## puts content.data
     write_blob( path, content.data )
   end
end

#add_collection(path) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ordinals/sandbox.rb', line 52

def add_collection( path )
   data = read_json( path )
   puts "  #{data['items'].size} record(s)"

   data['items'].each_with_index do |rec,i|
     name = rec['name']
     id   = rec['inscription_id']
     puts "==> #{i+1}/#{data['items'].size} - #{name} @ #{id}..."
     add( id )
   end
end

#add_csv(path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/ordinals/sandbox.rb', line 41

def add_csv( path )
   recs = read_csv( path )
   puts "  #{recs.size} record(s)"
 
   recs.each_with_index do |rec,i|
     id = rec['id']
     puts "==> #{i+1}/#{recs.size} - #{id}..."
     add( id )
   end
end

#add_data(obj) ⇒ Object



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
103
104
105
106
# File 'lib/ordinals/sandbox.rb', line 72

def add_data( obj )
   if obj.is_a?( Array )
      recs = obj
      puts "  #{recs.size} record(s)"
      recs.each_with_index do |rec,i|
           if rec.is_a?( String )
               id = rec
               puts "==> #{i+1}/#{recs.size} #{id}..."
               add( id ) 
           else  ## assume array of strings for now
               values = rec
             print "==> #{i+1}/#{recs.size} "
             ids = _find_ids( values )
             if ids.size == 1
               id = ids[0]
               puts " #{id}..."
               add( id )
             else 
               puts
               if ids.empty?
                 puts "!! ERROR - no inscribe id found in data row:"
               else
                 puts "!! ERROR - more than one (that is, #{ids.size}) inscribe id found in data row:"
               end
               pp values
               exit 1
             end
           end
      end        
   else
     ## todo/fix: raise ArgumentError - why? why not?
     puts "!! ERROR - sorry"
     exit 1
   end
end

#add_dir(rootdir) ⇒ Object



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

def add_dir( rootdir )
   ## glob for **.csv and **.html
   datasets = Dir.glob( "#{rootdir}/**/*.csv" )
   datasets.each do |path|
      add_csv( path )
   end
end

#exist?(id) ⇒ Boolean

add alias for different name(s) - why? why not?

Returns:

  • (Boolean)


11
12
13
14
# File 'lib/ordinals/sandbox.rb', line 11

def exist?( id )   ### add alias for different name(s) - why? why not?
  path = "#{@dir}/#{id}"
  File.exist?( path )
end

#read(id) ⇒ Object



16
17
18
19
# File 'lib/ordinals/sandbox.rb', line 16

def read( id )
  path = "#{@dir}/#{id}"
  read_blob( path )
end