Class: OrdDb::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/ordlite/importer.rb

Constant Summary collapse

Inscribe =
Model::Inscribe
Blob =
Model::Blob
Collection =
Model::Collection

Instance Method Summary collapse

Instance Method Details

#_import(id, content: true) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/ordlite/importer.rb', line 193

def _import( id, content: true )
   ## check if inscription / inscribe is already in db?
   inscribe = Inscribe.find_by( id: id )
   if inscribe  ## already in db; dump record
     ## pp inscribe
   else         ## fetch via ordinals.com api and update db
      data = Ordinals.inscription( id )

      pp data
      Inscribe.create_from_api( data )
   end

   _import_content( id )  if content              
end

#_import_by_num(num, content: true) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/ordlite/importer.rb', line 208

def _import_by_num( num, content: true )
   ## check if inscription / inscribe is already in db?
   inscribe = Inscribe.find_by( num: num )
   if inscribe  ## already in db; dump record
     ## pp inscribe
   else         ## fetch via ordinals.com api and update db
      data = Ordinals.inscription( num )

      pp data
      inscribe = Inscribe.create_from_api( data )
   end

   _import_content( inscribe.id )   if content              
end

#_import_content(id) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ordlite/importer.rb', line 178

def _import_content( id )
     ## check if (content) blob is already in db?
     blob = Blob.find_by( id: id )
     if blob    ## already in db; do nothing
     else       ## fetch via ordinals.com api and update db
        content = Ordinals.content( id )

        puts "  content-type: #{content.type}"
        puts "  content-length: #{content.length}"
     
        Blob.create( id: id, content: content.data )
     end
end

#import(id_or_ids, content: true) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ordlite/importer.rb', line 129

def import( id_or_ids, content: true )
   ## note: support (integer) numbers too (e.g. 0/1/2, etc.)
   if id_or_ids.is_a?( String )
      id = id_or_ids
     _import( id, content: content )
   elsif id_or_ids.is_a?( Integer )  
      num = id_or_ids
     _import_by_num( num, content: content )
   elsif id_or_ids.is_a?( Array )
      if id_or_ids.empty?   ## id_or_ids.size == 0
         ## do nothing; empty array
      else
         first = id_or_ids[0]
         if first.is_a?( String ) 
           ids = id_or_ids
           ids.each do |id|
             _import( id, content: content )
           end
         elsif first.is_a?( Integer ) 
            nums = id_or_ids
            nums.each do |num|
              _import_by_num( num, content: content )
            end 
         elsif first.is_a?( Hash ) && first.has_key?( 'id' ) 
           ## try to get ids with records
           recs = id_or_ids
           ids = recs.map {|rec| rec['id'] }
           ids.each do |id|
             _import( id, content: content )
           end
         elsif first.is_a?( Hash ) && first.has_key?( 'num' ) 
            ## try to get nums with records
            recs = id_or_ids
            nums = recs.map {|rec| rec['num'] }
            nums.each do |num|
               ## note: support numbers as strings too
               num = num.to_i(10)  if num.is_a?( String )
              _import_by_num( num, content: content )
            end
          else
           raise ArgumentError, "expected Array of String|Integer or Hash (with keys id|num); got #{first.class.name}"
         end
      end
   else
      raise ArgumentError, "expected String or Array; got #{id_or_ids.class.name}"
   end
end

#import_collection(path, content: true) ⇒ Object



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
112
# File 'lib/ordlite/importer.rb', line 79

def import_collection( path, content: true )
   data = read_json( path )

   meta =  data['collection']
   pp meta

   name = meta['name']

   col = Collection.find_by( name: name )
   if col
     puts "!! WARN - collection already in db; delete first to reimport"
     return     
   end
   
   col = Collection.create(
      name:  name,
      desc:  meta['description'],
      max:   meta['max_supply']
   )

   items = data['items']
   puts "  #{items.size} inscribe id(s)"
   items.each_with_index do |rec,i|
      id   = rec['inscription_id']
      name = rec['name']
      puts "==> #{i+1}/#{items.size} @ #{id}..."
 
      col.items.create( pos: i,
                        inscribe_id: id,
                        name: name )

     _import( id, content: content )
   end
end

#import_collection_csv(path, name:, content: true) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ordlite/importer.rb', line 10

def import_collection_csv( path,
                                name:,
                                content: true )
## or use
##  import_collection( format: 'csv') - why? why not?
   recs = read_csv( path )
   puts "  #{recs.size} inscribe id(s)"

   col = Collection.find_by( name: name )
   if col && col.items.count > 0
     puts "!! WARN - collection already in db; delete first to reimport"
     return     
   elsif col
     ## do nothing; (re)use collection record; add items
   else
     col = Collection.create(
            name:  name
            ## max:   recs.size   ## auto-add max - why? why not?
          )
   end

 recs.each_with_index do |rec,i|
    id   = rec['id']
    name = rec['name'] || rec['title']
    puts "==> #{i+1}/#{recs.size} >#{name}< @ #{id}..."

    col.items.create( pos: i,
                      inscribe_id: id,
                      name: name )

    _import( id, content: content )
 end
end

#import_collection_inscriptions(path, name:, content: true) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ordlite/importer.rb', line 45

def import_collection_inscriptions( path, 
                                         name:, 
                                         content: true )
   recs = read_json( path )
   puts "  #{recs.size} inscribe id(s)"

   col = Collection.find_by( name: name )
   if col && col.items.count > 0
     puts "!! WARN - collection already in db; delete first to reimport"
     return     
   elsif col
      ## do nothing; (re)use collection record; add items
   else
     col = Model::Collection.create(
        name:  name  
        ## max:   recs.size   ## auto-add max - why? why not?
      )
   end

   recs.each_with_index do |rec,i|
      id   = rec['id']
      meta = rec['meta']
      name = meta['name']
      puts "==> #{i+1}/#{recs.size} >#{name}< @ #{id}..."
 
      col.items.create( pos: i,
                        inscribe_id: id,
                        name: name )

     _import( id, content: content ) 
   end
end

#import_csv(path, content: true) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ordlite/importer.rb', line 115

def import_csv( path, content: true )
    recs = read_csv( path )
    puts "  #{recs.size} inscribe id(s)"
    #=>  1000 inscribe id(s) 
     
    recs.each_with_index do |rec,i|
      id = rec['id']
      puts "==> #{i+1}/#{rec.size} @ #{id}..."
    
      _import( id, content: content )
    end  
end