squib-extensions 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/squib-extensions.rb +78 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1c8d3fe85b1e9a75c2cdce9f3eab48f1cb9a63a628c6392ffe400d9d6dfa42f6
|
4
|
+
data.tar.gz: 97bef9ee0a6cd76efef02efc6caeaa439b17a288d398eef524890f60e41c78ff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9a71c1e6ce465b056b133169dee8abde0fb8f0120db752d4e54100c13d35d9b142fd32d721593d2b0768790abe030caccd3f360d6d30e17e430244f0fa2b6a3
|
7
|
+
data.tar.gz: 01c8bb644a1da60ab6a1d0dc4ccf52f6ac3604f7967ee1710497debb9aa31838342824b58eead06fe55199d027678a402539952596a038d06ed5e84ca85376ac
|
data/lib/squib-extensions.rb
CHANGED
@@ -3,12 +3,15 @@ require 'net/http'
|
|
3
3
|
require 'squib'
|
4
4
|
|
5
5
|
module Squib
|
6
|
+
# usage: deck = Squib.gsheet('2PACX-1..., 123456789)
|
7
|
+
#
|
8
|
+
# Downloads a CSV from google sheets and plugs it into squib's built-in CSV function.
|
9
|
+
# The sheet must have a public CSV share URL.
|
6
10
|
def gsheet(sheet_id, index=0)
|
7
11
|
res = fetch("https://docs.google.com/spreadsheets/d/e/#{sheet_id}/pub?gid=#{index}&output=csv")
|
8
12
|
|
9
13
|
if res.is_a?(Net::HTTPSuccess)
|
10
14
|
data = res.body.force_encoding("utf-8").encode("ascii-8bit")
|
11
|
-
puts data
|
12
15
|
return Squib.csv data: data
|
13
16
|
else
|
14
17
|
Squib.logger.error "Unable to fetch google sheet (error #{res.code}). Is '#{uri}' public?"
|
@@ -16,8 +19,81 @@ module Squib
|
|
16
19
|
end
|
17
20
|
module_function :gsheet
|
18
21
|
|
19
|
-
|
22
|
+
# usage: one_widget = Squib.subset(deck['widgets'], -> (w) { w && w >= 1 })
|
23
|
+
#
|
24
|
+
# Takes a smaller subset of data based on some criteria. Can plug the output
|
25
|
+
# directly into the range parameter for other module functions like svg, str, etc
|
26
|
+
def subset(column, criteria, matching = true)
|
27
|
+
sub = {}; column.each_with_index{ |c, i| (sub[criteria.(c)] ||= []) << i }
|
28
|
+
|
29
|
+
return sub[matching]
|
30
|
+
end
|
31
|
+
module_function :subset
|
32
|
+
|
33
|
+
class Deck
|
34
|
+
# Generate an avatar
|
35
|
+
def avatar(opts = {})
|
36
|
+
DSL::Avatar.new(self, __callee__).run(opts)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Adds a card number to aid in finding a card in a spreadsheet
|
40
|
+
def card_num(opts = {})
|
41
|
+
defaults = { x: self.width - 100, y: self.height - 100, width: 50, height: 50, font: 'sans-serif', font_size: 9, color: '#000', align: :center, valign: :middle }
|
42
|
+
range = Args.extract_range opts, self
|
43
|
+
|
44
|
+
# Add 2 because our csvs start at 1 and has a header row
|
45
|
+
text(str: range.map{ |i| i+1 }, **(defaults.merge opts))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
module DSL
|
50
|
+
class Avatar
|
51
|
+
attr_reader :dsl_method, :deck
|
20
52
|
|
53
|
+
def initialize(deck, dsl_method)
|
54
|
+
@deck = deck
|
55
|
+
@dsl_method = dsl_method
|
56
|
+
end
|
57
|
+
|
58
|
+
def run(opts)
|
59
|
+
Dir.chdir(deck.img_dir) do
|
60
|
+
defaults = { library: 'avataaars' }
|
61
|
+
options = defaults.merge opts
|
62
|
+
|
63
|
+
# Extract the default svg options
|
64
|
+
range = Args.extract_range opts, deck
|
65
|
+
svg_args = Args.extract_svg_special opts, deck
|
66
|
+
paint = Args.extract_paint opts, deck
|
67
|
+
box = Args.extract_scale_box opts, deck
|
68
|
+
trans = Args.extract_transform opts, deck
|
69
|
+
|
70
|
+
deck.progress_bar.start('Loading Avatar(s)', range.size) do |bar|
|
71
|
+
range.each do |i|
|
72
|
+
library = options[:library]
|
73
|
+
seed = options[:seed][i]
|
74
|
+
if seed == nil then
|
75
|
+
next
|
76
|
+
end
|
77
|
+
|
78
|
+
file = "avatar-#{library}-#{seed}.svg"
|
79
|
+
|
80
|
+
# Check if we need to download the image
|
81
|
+
if ! File.exist?(file) then
|
82
|
+
res = fetch("https://avatars.dicebear.com/api/#{library}/#{seed}.svg")
|
83
|
+
res = res.body.encode("ascii-8bit").force_encoding("utf-8")
|
84
|
+
|
85
|
+
File.open(file, "w") { |f| f.write(response) }
|
86
|
+
end
|
87
|
+
|
88
|
+
deck.cards[i].svg(file, svg_args[i], box[i], paint[i], trans[i])
|
89
|
+
bar.increment
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
21
97
|
|
22
98
|
def fetch(uri_str, limit = 10)
|
23
99
|
raise ArgumentError, 'Too many redirects' if limit == 0
|