Class: MetalArchives::Band

Inherits:
Base
  • Object
show all
Defined in:
lib/metal_archives/models/band.rb

Overview

Represents an band (person or group)

Class Method Summary collapse

Instance Method Summary collapse

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

.allObject

Get all bands

Returns Collection of Band

Raises
  • MetalArchives::Errors::APIError when receiving a status code >= 400

  • MetalArchives::Errors::ParserError when parsing failed. Please report this error.



461
462
463
# File 'lib/metal_archives/models/band.rb', line 461

def all
  search_by({})
end

.find(id) ⇒ Object

Find by ID

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

id

Integer



279
280
281
282
283
# File 'lib/metal_archives/models/band.rb', line 279

def find(id)
  return MetalArchives.cache[cache(id)] if MetalArchives.cache.include? cache(id)

  Band.new id: id
end

.find!(id) ⇒ Object

Find by ID (no lazy loading)

Returns Band

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



298
299
300
301
302
303
# File 'lib/metal_archives/models/band.rb', line 298

def find!(id)
  obj = find id
  obj.load! if obj && !obj.loaded?

  obj
end

.find_by(query) ⇒ Object

Find by attributes

Refer to MA’s FAQ for search tips.

Returns Band 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.

query

Hash containing one or more of the following keys:

  • :name: String

  • :exact: Boolean

  • :genre: String

  • :country: ISO3166::Country

  • :year_formation: Range containing Date

  • :comment: String

  • :status: see Band.status

  • :lyrical_themes: String

  • :location: String

  • :label: Label

  • :independent: boolean



330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/metal_archives/models/band.rb', line 330

def find_by(query)
  params = Parsers::Band.map_params query

  response = MetalArchives.http.get "/search/ajax-advanced/searching/bands", 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)

Refer to MA’s FAQ for search tips.

Returns Band 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.

query

Hash containing one or more of the following keys:

  • :name: String

  • :exact: Boolean

  • :genre: String

  • :country: ISO3166::Country

  • :year_formation: Range containing Date

  • :comment: String

  • :status: see Band.status

  • :lyrical_themes: String

  • :location: String

  • :label: Label

  • :independent: boolean



369
370
371
372
373
374
# File 'lib/metal_archives/models/band.rb', line 369

def find_by!(query)
  obj = find_by query
  obj.load! if obj && !obj.loaded?

  obj
end

.search(name) ⇒ Object

Search by name, resolves to Band.search_by (:name => name)

Refer to MA’s FAQ for search tips.

Returns (possibly empty) Array of Band

Raises
  • MetalArchives::Errors::APIError when receiving a status code >= 400

  • MetalArchives::Errors::ArgumentError when name isn’t a String

name

String



446
447
448
449
450
# File 'lib/metal_archives/models/band.rb', line 446

def search(name)
  raise MetalArchives::Errors::ArgumentError unless name.is_a? String

  search_by name: name
end

.search_by(query) ⇒ Object

Search by attributes

Refer to MA’s FAQ for search tips.

Returns Collection of Band

Raises
  • MetalArchives::Errors::APIError when receiving a status code >= 400

  • MetalArchives::Errors::ParserError when parsing failed. Please report this error.

query

Hash containing one or more of the following keys:

  • :name: String

  • :exact: Boolean

  • :genre: String

  • :country: ISO3166::Country

  • :year_formation: Range containing Date

  • :comment: String

  • :status: see Band.status

  • :lyrical_themes: String

  • :location: String

  • :label: Label

  • :independent: boolean



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/metal_archives/models/band.rb', line 401

def search_by(query)
  params = Parsers::Band.map_params query

  l = lambda do
    @start ||= 0

    if @max_items && @start >= @max_items
      []
    else
      response = MetalArchives.http.get "/search/ajax-advanced/searching/bands", params.merge(iDisplayStart: @start)
      json = JSON.parse response.to_s

      @max_items = json["iTotalRecords"]

      objects = []

      json["aaData"].each do |data|
        # Create Band 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 << Band.find(id)
      end

      @start += 200

      objects
    end
  end

  MetalArchives::Collection.new l
end

Instance Method Details

#aliasesObject

: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/band.rb', line 39

property :aliases, multiple: true

#commentObject

:attr_reader: comment

Returns raw HTML String

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/band.rb', line 138

property :comment

#countryObject

: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/band.rb', line 50

property :country, type: ISO3166::Country

#date_formedObject

:attr_reader: date_formed

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/band.rb', line 72

property :date_formed, type: Date

#genresObject

:attr_reader: genres

Returns Array of 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/band.rb', line 94

property :genres, multiple: true

#idObject

:attr_reader: id

Returns Integer



17
# File 'lib/metal_archives/models/band.rb', line 17

property :id, type: Integer

#independentObject

:attr_reader: independent

Returns boolean

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/band.rb', line 127

enum :independent, values: [true, false]

#labelObject

:attr_reader: label

Returns Label

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/band.rb', line 116

property :label, type: Label

: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 or :merchandise

  • :title: String



230
# File 'lib/metal_archives/models/band.rb', line 230

property :links, multiple: true

#locationObject

: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/band.rb', line 61

property :location

#logoObject

:attr_reader: logo

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)



203
# File 'lib/metal_archives/models/band.rb', line 203

property :logo

#lyrical_themesObject

:attr_reader: lyrical_themes

Returns Array of String

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/band.rb', line 105

property :lyrical_themes, multiple: true

#membersObject

:attr_reader: members

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)

members
  • :artist: Artist

  • :current: Boolean

  • :years_active: Array of Range containing Integer

  • :role: String



177
# File 'lib/metal_archives/models/band.rb', line 177

property :members, type: Artist, multiple: true

#nameObject

: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/band.rb', line 28

property :name

#photoObject

: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)



214
# File 'lib/metal_archives/models/band.rb', line 214

property :photo

#releasesObject

:attr_reader: releases

Returns Array of Release

Raises
  • MetalArchives::Errors::InvalidIDError when no or invalid id

  • MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)



160
# File 'lib/metal_archives/models/band.rb', line 160

property :releases, type: Release, multiple: true

#similarObject

:attr_reader: similar

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)

similar
  • :band: Band

  • :score: Integer



192
# File 'lib/metal_archives/models/band.rb', line 192

property :similar, type: Hash, multiple: true

#statusObject

:attr_reader: status

Returns :active, :split_up, :on_hold, :unknown, :changed_name or :disputed

Raises
  • MetalArchives::Errors::InvalidIDError when no or invalid id

  • MetalArchives::Errors::APIError when receiving a status code >= 400 (except 404)



149
# File 'lib/metal_archives/models/band.rb', line 149

enum :status, values: [:active, :split_up, :on_hold, :unknown, :changed_name, :disputed]

#years_activeObject

:attr_reader: years_active

Returns Array of Range containing Integer

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/band.rb', line 83

property :years_active, type: Range, multiple: true