class Bunto::Avatar

Constants

API_VERSION
DEFAULT_SIZE
SERVERS
VERSION

Public Class Methods

new(_tag_name, text, _tokens) click to toggle source
Calls superclass method
# File lib/bunto-avatar.rb, line 12
def initialize(_tag_name, text, _tokens)
  super
  @text = text
end

Public Instance Methods

render(context) click to toggle source
# File lib/bunto-avatar.rb, line 17
def render(context)
  @context = context
  @text    = Liquid::Template.parse(@text).render(@context)
  attrs    = attributes.map { |k, v| "#{k}=\"#{v}\"" }.join(" ")
  "<img #{attrs} />"
end

Private Instance Methods

attributes() click to toggle source
# File lib/bunto-avatar.rb, line 26
def attributes
  {
    :class                => classes,
    :src                  => url,
    :alt                  => username,
    :srcset               => srcset,
    :width                => size,
    :height               => size,
    "data-proofer-ignore" => true
  }
end
classes() click to toggle source

See primercss.io/avatars/#small-avatars

# File lib/bunto-avatar.rb, line 73
def classes
  size < 48 ? "avatar avatar-small" : "avatar"
end
host() click to toggle source
# File lib/bunto-avatar.rb, line 60
def host
  "avatars#{server_number}.githubusercontent.com"
end
path(scale = 1) click to toggle source
# File lib/bunto-avatar.rb, line 52
def path(scale = 1)
  "#{username}?v=#{API_VERSION}&s=#{size * scale}"
end
server_number() click to toggle source
# File lib/bunto-avatar.rb, line 56
def server_number
  Zlib.crc32(path) % SERVERS
end
size() click to toggle source
# File lib/bunto-avatar.rb, line 47
def size
  matches = @text.match(%r!\bsize=(\d+)\b!i)
  matches ? matches[1].to_i : DEFAULT_SIZE
end
srcset() click to toggle source
# File lib/bunto-avatar.rb, line 68
def srcset
  (1..4).map { |scale| "#{url(scale)} #{scale}x" }.join(", ")
end
url(scale = 1) click to toggle source
# File lib/bunto-avatar.rb, line 64
def url(scale = 1)
  "https://#{host}/#{path(scale)}"
end
username() click to toggle source
# File lib/bunto-avatar.rb, line 38
def username
  matches = @text.match(%r!\buser=([\w\.]+)\b!)
  if matches
    lookup_variable(@context, matches[1])
  else
    @text.split(" ").first.sub("@", "")
  end
end