class OpenSea::Meta

read meta data into struct

Public Class Methods

new( data ) click to toggle source
# File lib/artbase/opensea.rb, line 15
def initialize( data )
  @data = data

  assets = @data['assets']
  if assets.size != 1
    puts "!! error - expected one asset per file only - got #{assets.size} - sorry"
    exit 1
  end

  @asset = assets[0]
end
read( path ) click to toggle source
# File lib/artbase/opensea.rb, line 8
def self.read( path )
  txt  = File.open( path, 'r:utf-8' ) { |f| f.read }
  data = JSON.parse( txt )
  new( data )
end

Public Instance Methods

_blank( o ) click to toggle source
# File lib/artbase/opensea.rb, line 77
def _blank( o )   ## auto-convert  "" (empty string) into nil
   if o && o.strip.empty?
     nil
   else
     o
   end
end
_normalize( str ) click to toggle source

“private” convenience / helper methods

# File lib/artbase/opensea.rb, line 65
def _normalize( str )
   return if str.nil?    ## check: check for nil - why? why not?

   ## normalize string
   ##   remove leading and trailing spaces
   ##   collapse two and more spaces into one
   ##    change unicode space to ascii
   str = str.gsub( "\u{00a0}", ' ' )
   str = str.strip.gsub( /[ ]{2,}/, ' ' )
   str
end
description() click to toggle source
# File lib/artbase/opensea.rb, line 31
def description
  @description ||= _normalize( @asset['description'] )
end
image_original_url() click to toggle source

note: auto-convert “” (empty string) to nil

# File lib/artbase/opensea.rb, line 36
def image_original_url() _blank( @asset['image_original_url'] );  end
image_url() click to toggle source
# File lib/artbase/opensea.rb, line 37
def image_url()          _blank( @asset['image_url'] ); end
name() click to toggle source
# File lib/artbase/opensea.rb, line 27
def name
  @name ||= _normalize( @asset['name'] )
end
token_id() click to toggle source
# File lib/artbase/opensea.rb, line 40
def token_id() @asset['token_id']; end
traits() click to toggle source
# File lib/artbase/opensea.rb, line 43
def traits
  @traits ||= begin
                 traits = []
                 ## keep traits as (simple)
                 ##   ordered array of pairs for now
                 ##
                 ##  in a step two make lookup via hash table
                 ##   or such easier / "automagic"

                 @asset[ 'traits' ].each do |t|
                    trait_type  = t['trait_type'].strip
                    trait_value = t['value'].strip
                    traits << [trait_type, trait_value]
                 end

                 traits
                end
end