Class: Squib::DSL::Avatar

Inherits:
Object
  • Object
show all
Defined in:
lib/squib_plus/deck/avatar.rb

Overview

Add an avatar for placeholder art in your games using avatars.dicebear.com/. The image will be downloaded to your configured image directory if it doesn’t already exist.

Library can be male, female, human, identicon, initials, bottts, avataaars, jdenticon, gridy or micah.

Seed can be any random string

Example:

avatar library: 'micah', seed: '1234'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(deck, dsl_method) ⇒ Avatar

Returns a new instance of Avatar.



29
30
31
32
# File 'lib/squib_plus/deck/avatar.rb', line 29

def initialize(deck, dsl_method)
  @deck = deck
  @dsl_method = dsl_method
end

Instance Attribute Details

#deckObject (readonly)

Returns the value of attribute deck.



27
28
29
# File 'lib/squib_plus/deck/avatar.rb', line 27

def deck
  @deck
end

#dsl_methodObject (readonly)

Returns the value of attribute dsl_method.



27
28
29
# File 'lib/squib_plus/deck/avatar.rb', line 27

def dsl_method
  @dsl_method
end

Class Method Details

.accepted_paramsObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/squib_plus/deck/avatar.rb', line 34

def self.accepted_params
  %i[
    library seed
    x y width height
    blend mask
    crop_x crop_y crop_width crop_height
    crop_corner_radius crop_corner_x_radius crop_corner_y_radius
    flip_horizontal flip_vertical angle
    id force_id data
    range layout
    placeholder
  ]
end

Instance Method Details

#run(opts) ⇒ Object



48
49
50
51
52
53
54
55
56
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
# File 'lib/squib_plus/deck/avatar.rb', line 48

def run(opts)
  Dir.chdir(deck.img_dir) do
    defaults = { library: 'avataaars' }
    options = defaults.merge opts

    # Extract the default svg options
    range = Args.extract_range opts, deck
    svg_args = Args.extract_svg_special opts, deck
    paint = Args.extract_paint opts, deck
    box   = Args.extract_scale_box opts, deck
    trans = Args.extract_transform opts, deck

    deck.progress_bar.start('Loading Avatar(s)', range.size) do |bar|
      range.each do |i|
        library = options[:library]
        seed = options[:seed][i]
        next if seed.nil?

        file = "avatar-#{library}-#{seed}.svg"

        # Check if we need to download the image
        unless File.exist?(file)
          agent = Mechanize.new
          agent.follow_meta_refresh = true
          agent.keep_alive = false
          agent.history.max_size = 10

          response = agent.get_file("https://avatars.dicebear.com/api/#{library}/#{seed}.svg")
          response = response.encode('ascii-8bit').force_encoding('utf-8')

          File.open(file, 'w') { |f| f.write(response) }
        end

        deck.cards[i].svg(file, svg_args[i], box[i], paint[i], trans[i])
        bar.increment
      end
    end
  end
end