class Squib::DSL::Avatar

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'

Attributes

deck[R]
dsl_method[R]

Public Class Methods

accepted_params() click to toggle source
# 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
new(deck, dsl_method) click to toggle source
# File lib/squib_plus/deck/avatar.rb, line 29
def initialize(deck, dsl_method)
  @deck = deck
  @dsl_method = dsl_method
end

Public Instance Methods

run(opts) click to toggle source
# 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