class Discorb::Asset

Represents a single asset.

Attributes

hash[R]

@return [String] The hash of asset.

Public Class Methods

new(target, hash, path: nil) click to toggle source

@!visibility private

# File lib/discorb/asset.rb, line 15
def initialize(target, hash, path: nil)
  @hash = hash
  @target = target
  @path = path
end

Public Instance Methods

animated?() click to toggle source
# File lib/discorb/asset.rb, line 21
def animated?
  @hash.start_with? "a_"
end
inspect() click to toggle source
# File lib/discorb/asset.rb, line 38
def inspect
  "#<#{self.class} #{@target.class} #{@hash}>"
end
url(image_format: nil, size: 1024) click to toggle source

URL of the asset.

@param [String] image_format The image format. @param [Integer] size The size of the image.

@return [String] URL of the asset.

# File lib/discorb/asset.rb, line 33
def url(image_format: nil, size: 1024)
  path = @path || "#{endpoint}/#{@target.id}"
  "https://cdn.discordapp.com/#{path}/#{@hash}.#{image_format or (animated? ? "gif" : "webp")}?size=#{size}"
end

Private Instance Methods

endpoint() click to toggle source
# File lib/discorb/asset.rb, line 44
def endpoint
  case @target
  when User, Member, Webhook
    "avatars"
  when Guild, IncomingWebhook::Guild
    "icons"
  when Application
    "app-icons"
  when Application::Team
    "team-icons"
  end
end