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
9
10
# File 'lib/ordinals/sandbox.rb', line 5

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

   @requests = 0
end

Instance Method Details

#_find_ids(values) ⇒ Object



73
74
75
# File 'lib/ordinals/sandbox.rb', line 73

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

#add(id) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ordinals/sandbox.rb', line 23

def add( id )
   path = "#{@dir}/#{id}"
   if !@force && File.exist?( path )
      ## puts "  in sandbox"

   else
     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

     #=> #<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 )
     @requests += 1
   end
end

#add_collection(path) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ordinals/sandbox.rb', line 57

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



46
47
48
49
50
51
52
53
54
55
# File 'lib/ordinals/sandbox.rb', line 46

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



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
107
108
109
110
111
# File 'lib/ordinals/sandbox.rb', line 77

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



118
119
120
121
122
123
124
# File 'lib/ordinals/sandbox.rb', line 118

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)


13
14
15
16
# File 'lib/ordinals/sandbox.rb', line 13

def exist?( id )   ### add alias for different name(s) - why? why not?

  path = "#{@dir}/#{id}"
  File.exist?( path )
end

#read(id) ⇒ Object



18
19
20
21
# File 'lib/ordinals/sandbox.rb', line 18

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