Class: TokenCollection::Meta

Inherits:
Object
  • Object
show all
Defined in:
lib/artbase-cocos/collection/token_meta.rb

Overview

(nested) Meta classes

read meta data into struct

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Meta

Returns a new instance of Meta.



13
14
15
# File 'lib/artbase-cocos/collection/token_meta.rb', line 13

def initialize( data )
  @data = data
end

Class Method Details

.read(path) ⇒ Object



8
9
10
# File 'lib/artbase-cocos/collection/token_meta.rb', line 8

def self.read( path )
  new( read_json( path ))
end

Instance Method Details

#_blank(o) ⇒ Object

auto-convert “” (empty string) into nil



68
69
70
71
72
73
74
# File 'lib/artbase-cocos/collection/token_meta.rb', line 68

def _blank( o )   ## auto-convert  "" (empty string) into nil
   if o && o.strip.empty?
     nil
   else
     o
   end
end

#_normalize(str) ⇒ Object

“private” convenience / helper methods



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/artbase-cocos/collection/token_meta.rb', line 56

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

#attributesObject Also known as: traits



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/artbase-cocos/collection/token_meta.rb', line 35

def attributes
  @attributes ||= 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"

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

                 traits
                end
end

#descriptionObject



26
27
28
# File 'lib/artbase-cocos/collection/token_meta.rb', line 26

def description
  @description ||= _normalize( @data['description'] )
end

#imageObject Also known as: image_url

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



31
# File 'lib/artbase-cocos/collection/token_meta.rb', line 31

def image()          _blank( @data['image'] ); end

#nameObject



18
19
20
21
22
23
# File 'lib/artbase-cocos/collection/token_meta.rb', line 18

def name
  ## note: name might be an integer number e.g. 0/1/2 etc.
  ##      e.g. see crypto pudgy punks and others?
  ##    always auto-convert to string
  @name ||= _normalize( @data['name'].to_s )
end