class MetalArchives::Artist

Represents a single performer (but not a solo artist)

Attributes

aliases[R]

Returns Array of String

Raises
bands[R]

Returns Array of Hash containing the following keys

Raises
bands
  • :band: Band

  • :active: Boolean

  • :years_active: Array of Range containing Integer

  • :role: String

biography[R]

Returns raw HTML String

Raises
cause_of_death[R]

Returns String

Raises
country[R]

Returns ISO3166::Country

Raises
date_of_birth[R]

Returns Date

Raises
date_of_death[R]

Returns Date

Raises
gender[R]

Returns Symbol, either :male or :female

Raises
id[R]

Returns Integer

location[R]

Returns String

Raises
name[R]

Returns String

Raises
photo[R]

Returns URI (rewritten if config option was enabled)

Raises
trivia[R]

Returns raw HTML String

Raises

Public Class Methods

from_h(hash) click to toggle source

Deserialize from hash

# File lib/metal_archives/models/artist.rb, line 202
def self.from_h(hash)
  return unless hash.fetch(:type) == "artist"

  new(hash.slice(:id, :name, :aliases, :location, :cause_of_death, :gender, :biography, :trivial, :photo, :links, :bands))
    .tap { |m| m.country = ISO3166::Country[hash[:country]] }
    .tap { |m| m.date_of_birth = Date.parse(hash[:date_of_birth]) if hash[:date_of_birth] }
    .tap { |m| m.date_of_death = Date.parse(hash[:date_of_death]) if hash[:date_of_death] }
end

Protected Class Methods

all() click to toggle source

Get all artists

Returns Collection of Artist

Raises
# File lib/metal_archives/models/artist.rb, line 388
def all
  search ""
end
find(id) click to toggle source

Find by ID

Returns Artist, even when ID is invalid (because the data is lazily fetched)

id

Integer

# File lib/metal_archives/models/artist.rb, line 253
def find(id)
  return MetalArchives.cache[cache(id)] if MetalArchives.cache.include? cache(id)

  Artist.new id: id
end
find!(id) click to toggle source

Find by ID (no lazy loading)

Returns Artist

Raises
id

Integer

# File lib/metal_archives/models/artist.rb, line 272
def find!(id)
  obj = find id
  obj.load! if obj && !obj.loaded?

  obj
end
find_by(query) click to toggle source

Find by attributes

Returns Artist or nil when no results

Raises
query

Hash containing one or more of the following keys:

  • :name: String

# File lib/metal_archives/models/artist.rb, line 293
def find_by(query)
  raise MetalArchives::Errors::ArgumentError unless query.include? :name

  params = Parsers::Artist.map_params query

  response = MetalArchives.http.get "/search/ajax-artist-search/", params
  json = JSON.parse response.to_s

  return nil if json["aaData"].empty?

  data = json["aaData"].first
  id = Nokogiri::HTML(data.first).xpath("//a/@href").first.value.delete('\\').split("/").last.gsub(/\D/, "").to_i

  find id
end
find_by!(query) click to toggle source

Find by attributes (no lazy loading)

Returns Artist or nil when no results

Raises
query

Hash containing one or more of the following keys:

  • :name: String

# File lib/metal_archives/models/artist.rb, line 324
def find_by!(query)
  obj = find_by query
  obj.load! if obj && !obj.loaded?

  obj
end

Public Instance Methods

to_h() click to toggle source

Serialize to hash

# File lib/metal_archives/models/artist.rb, line 179
def to_h
  {
    type: "artist",
    id: id,
    name: name,
    aliases: aliases || [],
    country: country&.alpha3,
    location: location,
    date_of_birth: date_of_birth&.iso8601,
    date_of_death: date_of_death&.iso8601,
    cause_of_death: cause_of_death,
    gender: gender,
    biography: biography,
    trivia: trivia,
    photo: photo,
    links: links || [],
    bands: bands || [],
  }
end