class BridgetownAvatar::Builder

Constants

API_VERSION
DEFAULT_SIZE
SCALES
SERVERS

Attributes

size[R]

Public Instance Methods

build() click to toggle source
# File lib/bridgetown-avatar/builder.rb, line 13
def build
  liquid_tag "avatar" do |attributes, tag|
    @attributes = attributes # .split(",").map(&:strip)
    @size = compute_size
    @context = tag.context

    "<img src=\"#{url}\" class=\"#{classes}\"/>"
  end
end

Private Instance Methods

classes() click to toggle source

See primercss.io/avatars/#small-avatars

# File lib/bridgetown-avatar/builder.rb, line 61
def classes
  size.to_i < 48 ? "avatar avatar-small" : "avatar"
end
compute_size() click to toggle source
# File lib/bridgetown-avatar/builder.rb, line 25
def compute_size
  matches = @attributes.match(%r!\bsize=(\d+)\b!i)
  matches ? matches[1] : DEFAULT_SIZE
end
host() click to toggle source
# File lib/bridgetown-avatar/builder.rb, line 49
def host
  "https://avatars#{server_number}.githubusercontent.com"
end
path(scale = 1) click to toggle source
# File lib/bridgetown-avatar/builder.rb, line 37
def path(scale = 1)
  "#{username}?v=#{API_VERSION}&s=#{scale == 1 ? size : (size.to_i * scale)}"
end
server_number() click to toggle source
# File lib/bridgetown-avatar/builder.rb, line 45
def server_number
  Zlib.crc32(path) % SERVERS
end
srcset() click to toggle source
# File lib/bridgetown-avatar/builder.rb, line 56
def srcset
  SCALES.map { |scale| "#{url(scale.to_i)} #{scale}x" }.join(", ")
end
url(scale = 1) click to toggle source
# File lib/bridgetown-avatar/builder.rb, line 41
def url(scale = 1)
  "#{host}/#{path(scale)}"
end
username() click to toggle source
# File lib/bridgetown-avatar/builder.rb, line 30
def username
  return @context["user"] if @context["user"]

  result = @attributes.include?(" ") ? @attributes.split(" ")[0] : @attributes
  result.start_with?("@") ? result.sub("@", "") : result
end