Class: MetalArchives::Artist
- Defined in:
- lib/metal_archives/models/artist.rb
Overview
Represents a single performer (but not a solo artist)
Class Method Summary collapse
-
.all ⇒ Object
Get all artists.
-
.find(id) ⇒ Object
Find by ID.
-
.find!(id) ⇒ Object
Find by ID (no lazy loading).
-
.find_by(query) ⇒ Object
Find by attributes.
-
.find_by!(query) ⇒ Object
Find by attributes (no lazy loading).
-
.from_h(hash) ⇒ Object
Deserialize from hash.
-
.search(name) ⇒ Object
Search by name.
Instance Method Summary collapse
-
#aliases ⇒ Object
:attr_reader: aliases.
-
#bands ⇒ Object
:attr_reader: bands.
-
#biography ⇒ Object
:attr_reader: biography.
-
#cause_of_death ⇒ Object
:attr_reader: cause_of_death.
-
#country ⇒ Object
:attr_reader: country.
-
#date_of_birth ⇒ Object
:attr_reader: date_of_birth.
-
#date_of_death ⇒ Object
:attr_reader: date_of_death.
-
#gender ⇒ Object
:attr_reader: gender.
-
#id ⇒ Object
:attr_reader: id.
-
#links ⇒ Object
:attr_reader: links.
-
#location ⇒ Object
:attr_reader: location.
-
#name ⇒ Object
:attr_reader: name.
-
#photo ⇒ Object
:attr_reader: photo.
-
#to_h ⇒ Object
Serialize to hash.
-
#trivia ⇒ Object
:attr_reader: trivia.
Methods inherited from Base
#==, cache, #cached?, #initialize, #inspect, #load!, #loaded?, properties, #set
Constructor Details
This class inherits a constructor from MetalArchives::Base
Class Method Details
.all ⇒ Object
Get all artists
Returns Collection of Artist
- Raises
-
MetalArchives::Errors::APIError when receiving a status code >= 400
-
MetalArchives::Errors::ParserError when parsing failed. Please report this error.
388 389 390 |
# File 'lib/metal_archives/models/artist.rb', line 388 def all search "" end |
.find(id) ⇒ Object
Find by ID
Returns Artist, even when ID is invalid (because the data is lazily fetched)
id
-
Integer
253 254 255 256 257 |
# 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) ⇒ Object
Find by ID (no lazy loading)
Returns Artist
- Raises
-
MetalArchives::Errors::InvalidIDError when no or invalid id
-
MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
-
MetalArchives::Errors::ParserError when parsing failed. Please report this error.
id
-
Integer
272 273 274 275 276 277 |
# 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) ⇒ Object
Find by attributes
Returns Artist or nil when no results
- Raises
-
MetalArchives::Errors::APIError when receiving a status code >= 400
-
MetalArchives::Errors::ParserError when parsing failed. Please report this error.
-
MetalArchives::Errors::ArgumentError when query contains no :name key
query
-
Hash containing one or more of the following keys:
-
:name
:String
-
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# 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) ⇒ Object
Find by attributes (no lazy loading)
Returns Artist or nil when no results
- Raises
-
MetalArchives::Errors::InvalidIDError when no or invalid id
-
MetalArchives::Errors::APIError when receiving a status code >= 400
-
MetalArchives::Errors::ParserError when parsing failed. Please report this error.
-
MetalArchives::Errors::ArgumentError when query contains no :name key
query
-
Hash containing one or more of the following keys:
-
:name
:String
-
324 325 326 327 328 329 |
# 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 |
.from_h(hash) ⇒ Object
Deserialize from hash
202 203 204 205 206 207 208 209 |
# 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 |
.search(name) ⇒ Object
Search by name
Returns Collection of Artist
- Raises
-
MetalArchives::Errors::APIError when receiving a status code >= 400
-
MetalArchives::Errors::ParserError when parsing failed. Please report this error.
-
MetalArchives::Errors::ArgumentError when
name
isn’t aString
name
-
String
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/metal_archives/models/artist.rb', line 344 def search(name) raise MetalArchives::Errors::ArgumentError unless name.is_a? String query = { name: name } params = Parsers::Artist.map_params query l = lambda do @start ||= 0 if @max_items && @start >= @max_items [] else response = MetalArchives.http.get "/search/ajax-artist-search/", params.merge(iDisplayStart: @start) json = JSON.parse response.to_s @max_items = json["iTotalRecords"] objects = [] json["aaData"].each do |data| # Create Artist object for every ID in the results list id = Nokogiri::HTML(data.first).xpath("//a/@href").first.value.delete('\\').split("/").last.gsub(/\D/, "").to_i objects << Artist.find(id) end @start += 200 objects end end MetalArchives::Collection.new l end |
Instance Method Details
#aliases ⇒ Object
:attr_reader: aliases
Returns Array
of String
- Raises
-
MetalArchives::Errors::InvalidIDError when no or invalid id
-
MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
39 |
# File 'lib/metal_archives/models/artist.rb', line 39 property :aliases, multiple: true |
#bands ⇒ Object
:attr_reader: bands
Returns Array
of Hash
containing the following keys
- Raises
-
MetalArchives::Errors::InvalidIDError when no or invalid id
-
MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
bands
-
:band
: Band -
:active
: Boolean -
:years_active
:Array
of Range containingInteger
-
:role
:String
-
171 |
# File 'lib/metal_archives/models/artist.rb', line 171 property :bands, type: Hash, multiple: true |
#biography ⇒ Object
:attr_reader: biography
Returns raw HTML String
- Raises
-
MetalArchives::Errors::InvalidIDError when no or invalid id
-
MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
116 |
# File 'lib/metal_archives/models/artist.rb', line 116 property :biography |
#cause_of_death ⇒ Object
:attr_reader: cause_of_death
Returns String
- Raises
-
MetalArchives::Errors::InvalidIDError when no or invalid id
-
MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
94 |
# File 'lib/metal_archives/models/artist.rb', line 94 property :cause_of_death |
#country ⇒ Object
:attr_reader: country
Returns ISO3166::Country
- Raises
-
MetalArchives::Errors::InvalidIDError when no or invalid id
-
MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
50 |
# File 'lib/metal_archives/models/artist.rb', line 50 property :country, type: ISO3166::Country |
#date_of_birth ⇒ Object
:attr_reader: date_of_birth
Returns Date
- Raises
-
MetalArchives::Errors::InvalidIDError when no or invalid id
-
MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
72 |
# File 'lib/metal_archives/models/artist.rb', line 72 property :date_of_birth, type: Date |
#date_of_death ⇒ Object
:attr_reader: date_of_death
Returns Date
- Raises
-
MetalArchives::Errors::InvalidIDError when no or invalid id
-
MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
83 |
# File 'lib/metal_archives/models/artist.rb', line 83 property :date_of_death, type: Date |
#gender ⇒ Object
:attr_reader: gender
Returns Symbol
, either :male
or :female
- Raises
-
MetalArchives::Errors::InvalidIDError when no or invalid id
-
MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
105 |
# File 'lib/metal_archives/models/artist.rb', line 105 enum :gender, values: [:male, :female] |
#id ⇒ Object
:attr_reader: id
Returns Integer
17 |
# File 'lib/metal_archives/models/artist.rb', line 17 property :id, type: Integer |
#links ⇒ Object
:attr_reader: links
Returns Array
of Hash
containing the following keys
- Raises
-
MetalArchives::Errors::InvalidIDError when no or invalid id
-
MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
links
-
:url
:String
-
:type
:Symbol
, either:official
,:unofficial
or:unlisted_bands
-
:title
:String
-
154 |
# File 'lib/metal_archives/models/artist.rb', line 154 property :links, multiple: true |
#location ⇒ Object
:attr_reader: location
Returns String
- Raises
-
MetalArchives::Errors::InvalidIDError when no or invalid id
-
MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
61 |
# File 'lib/metal_archives/models/artist.rb', line 61 property :location |
#name ⇒ Object
:attr_reader: name
Returns String
- Raises
-
MetalArchives::Errors::InvalidIDError when no or invalid id
-
MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
28 |
# File 'lib/metal_archives/models/artist.rb', line 28 property :name |
#photo ⇒ Object
:attr_reader: photo
Returns URI
(rewritten if config option was enabled)
- Raises
-
MetalArchives::Errors::InvalidIDError when no or invalid id
-
MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
138 |
# File 'lib/metal_archives/models/artist.rb', line 138 property :photo |
#to_h ⇒ Object
Serialize to hash
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# 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 |
#trivia ⇒ Object
:attr_reader: trivia
Returns raw HTML String
- Raises
-
MetalArchives::Errors::InvalidIDError when no or invalid id
-
MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)
127 |
# File 'lib/metal_archives/models/artist.rb', line 127 property :trivia |