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}"
= { 'User-Agent' => "ruby v#{RUBY_VERSION}" }
request = Net::HTTP::Get.new( uri.request_uri, )
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 = response..to_h
puts "htttp respone headers:"
pp
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
dirname = File.dirname( "#{dest}.#{format}" )
FileUtils.mkdir_p( dirname ) unless Dir.exist?( dirname )
if format == 'svg'
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
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}"
= { 'User-Agent' => "ruby v#{RUBY_VERSION}" }
request = Net::HTTP::Get.new( uri.request_uri, )
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] }
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 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|
res = r[1] <=> l[1]
res = l[0] <=> r[0] if res == 0
res
end 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 ) end
str
end
|
#pixelart ⇒ Object
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
begin
block.call
rescue Net::ReadTimeout => e
raise if errors.size >= max_tries
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}<"
'' end.gsub( ' ', '_')
end
|