Top Level Namespace

Defined Under Namespace

Modules: Artbase, Pixelart Classes: Collection, ImageCollection, TokenCollection

Instance Method Summary collapse

Instance Method Details

#copy_image(src, dest, dump_headers: false) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
# File 'lib/artbase/helper.rb', line 95

def copy_image( src, dest,
                 dump_headers: false )
  uri = URI.parse( src )

  http = Net::HTTP.new( uri.host, uri.port )

  puts "[debug] GET #{uri.request_uri} uri=#{uri}"

  headers = { 'User-Agent' => "ruby v#{RUBY_VERSION}" }

  request = Net::HTTP::Get.new( uri.request_uri, headers )
  if uri.instance_of? URI::HTTPS
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  response   = http.request( request )

  if response.code == '200'
    puts "#{response.code} #{response.message}"

    content_type   = response.content_type
    content_length = response.content_length
    puts "  content_type: #{content_type}, content_length: #{content_length}"

    if dump_headers   ## for debugging dump headers
      headers = response.each_header.to_h
      puts "htttp respone headers:"
      pp headers
    end


    format = if content_type =~ %r{image/jpeg}i
                'jpg'
             elsif content_type =~ %r{image/png}i
                'png'
              elsif content_type =~ %r{image/gif}i
                'gif'
              elsif content_type =~ %r{image/svg}i
                'svg'
             else
              puts "!! error:"
              puts " unknown image format content type: >#{content_type}<"
              exit 1
            end

    ##   make sure path exits - autocreate dirs
    ## make sure path exists
    dirname = File.dirname( "#{dest}.#{format}" )
    FileUtils.mkdir_p( dirname )  unless Dir.exist?( dirname )

    if format == 'svg'
      ## save as text  (note: assume utf-8 encoding for now)
      text = response.body.to_s
      text = text.force_encoding( Encoding::UTF_8 )

      File.open( "#{dest}.svg", 'w:utf-8' ) do |f|
        f.write( text )
      end
    else
      ## save as binary
      File.open( "#{dest}.#{format}", 'wb' ) do |f|
        f.write( response.body )
      end
    end
  else
    puts "!! error:"
    puts "#{response.code} #{response.message}"
    exit 1
  end
end

#copy_json(src, dest) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/artbase/helper.rb', line 57

def copy_json( src, dest )
  uri = URI.parse( src )

  http = Net::HTTP.new( uri.host, uri.port )

  puts "[debug] GET #{uri.request_uri} uri=#{uri}"

  headers = { 'User-Agent' => "ruby v#{RUBY_VERSION}" }


  request = Net::HTTP::Get.new( uri.request_uri, headers )
  if uri.instance_of? URI::HTTPS
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  response   = http.request( request )

  if response.code == '200'
    puts "#{response.code} #{response.message}"
    puts "  content_type: #{response.content_type}, content_length: #{response.content_length}"

    text = response.body.to_s
    text = text.force_encoding( Encoding::UTF_8 )

    data = JSON.parse( text )

    File.open( dest, "w:utf-8" ) do |f|
      f.write( JSON.pretty_generate( data ) )
    end
  else
    puts "!! error:"
    puts "#{response.code} #{response.message}"
    exit 1
  end
end

#counter_to_csv(counter) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/artbase/attributes.rb', line 71

def counter_to_csv( counter )

  puts "type, name, count"
  counter.each do |trait_type, h|
    puts "#{trait_type}, ∑ Total, #{h[:count]}"
    h[:by_type].each do |trait_value, count|
       puts "#{trait_type}, #{trait_value}, #{count}"
    end
  end
end

#counter_to_text(counter) ⇒ Object



3
4
5
6
7
8
9
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/artbase/attributes.rb', line 3

def counter_to_text( counter )

  counter = counter.to_a

  attribute_counter = counter[0]
  more_counter      = counter[1..-1]


  puts "Attribute Counts\n"
  trait_type, h = attribute_counter

  total = h[:by_type].values.reduce(0) { |sum,count| sum+count }


  types = h[:by_type]
  types = types.sort { |l,r| l[0]<=>r[0] }  ## sort by name

  puts "\n"
  puts "|Name|Total (%)|"
  puts "|--------|----------:|"

  types.each do |rec|
      name     = rec[0]
      count    = rec[1]
      percent =  Float(count*100)/Float(total)

      puts "| **#{name} Attributes** | #{count} (#{'%.2f' % percent}) |"
  end
  puts "\n"

  more_counter.each_with_index do |(trait_type, h),i|
    print " · "  if i > 0   ## add separator
    print "#{trait_type } (#{h[:by_type].size})"
  end
  puts "\n\n"



  more_counter.each do |trait_type, h|
    print "### #{trait_type } (#{h[:by_type].size}) - "
    print "∑Total #{h[:count]}/#{total}\n"

    puts "\n"
    puts "|Name|Total (%)|"
    puts "|--------|----------:|"

      types = h[:by_type]
      types = types.sort do |l,r|
                            # sort by 1) by count
                            #         2) by name a-z
                                  res = r[1] <=> l[1]
                                  res = l[0] <=> r[0]  if res == 0
                                  res
                         end  ## sort by count
      types.each do |rec|
         name     = rec[0]
         count    = rec[1]
         percent =  Float(count*100)/Float(total)

         puts "| **#{name}** | #{count} (#{'%.2f' % percent}) |"
      end
      puts "\n\n"
    end
end

#handle_ipfs(str, normalize: true, ipfs_gateway: Artbase.config.ipfs_gateway) ⇒ Object

quick ipfs (interplanetary file system) hack

- make more reuseable
- different name  e.g. ipfs_to_http or such - why? why not?
change/rename parameter str to url or suc - why? why not?


60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/artbase.rb', line 60

def handle_ipfs( str, normalize:    true,
                      ipfs_gateway: Artbase.config.ipfs_gateway )

  if normalize && str.start_with?( 'https://ipfs.io/ipfs/' )
    str = str.sub( 'https://ipfs.io/ipfs/', 'ipfs://' )
  end

  if str.start_with?( 'ipfs://' )
    str = str.sub( 'ipfs://', ipfs_gateway )   # use/replace with public gateway
  end
  str
end

#pixelartObject

3rd party gems



6
# File 'lib/artbase.rb', line 6

require 'pixelart'

#retry_on_error(max_tries: 3, &block) ⇒ Object

todo/check: use a different name than retry - why? why not?



7
8
9
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
# File 'lib/artbase/retry.rb', line 7

def retry_on_error( max_tries: 3, &block )
  errors = []
  delay = 3  ## 3 secs

    begin
      block.call

    ## note: add more exception here (separated by comma) like
    ##         rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED => e
    rescue Net::ReadTimeout => e
        ##  (re)raise (use raise with arguments or such - why? why not?)
        raise    if errors.size >= max_tries

        ## ReadTimeout, a subclass of Timeout::Error, is raised if a chunk of the response cannot be read within the read_timeout.
        ##                subclass of RuntimeError
        ##                subclass of StandardError
        ##                subclass of Exception
        puts "!! ERROR - #{e}:"
        pp e

        errors << e

        puts
        puts "==> retrying (count=#{errors.size}, max_tries=#{max_tries}) in #{delay} sec(s)..."
        sleep( delay )
        retry
    end

  if errors.size > 0
    puts "  #{errors.size} retry attempt(s) on error(s):"
    pp errors
  end

  errors
end

#slugify(name) ⇒ Object



4
5
6
7
8
9
# File 'lib/artbase/helper.rb', line 4

def slugify( name )
  name.downcase.gsub( /[^a-z0-9 ()$_-]/ ) do |_|
     puts " !! WARN: asciify - found (and removing) non-ascii char >#{Regexp.last_match}<"
     ''  ## remove - use empty string
  end.gsub( ' ', '_')
end