Module: Genius::Artists

Defined in:
lib/genius/api/artists.rb

Overview

An artist is how Genius represents the creator of one or more songs (or other documents hosted on Genius). It's usually a musician or group of musicians.

Class Method Summary collapse

Class Method Details

.artists(token: nil, id: nil) ⇒ Hash, NilClass

Genius::Artists.artists -> Hash

Data for a specific artist.

Parameters:

Returns:

  • (Hash)
  • (NilClass)

    if TokenError exception raised.

Raises:

  • (ArgumentError)

    if id is nil.

  • (TokenError)

    if token or Genius::Auth.token are invalid.



18
19
20
21
22
23
24
25
# File 'lib/genius/api/artists.rb', line 18

def artists(token: nil, id: nil)
  Auth.authorized?(method_name: "#{Module.nesting[1].name}.#{__method__}") if token.nil?
  Errors.validate_token(token) unless token.nil?
  raise ArgumentError, "`id` can't be nil!" if id.nil?

  response = HTTParty.get("#{Api::RESOURCE}/artists/#{id}?access_token=#{token_ext(token)}").body
  JSON.parse(response)
end

.artists_songs(token: nil, id: nil, options: {}) ⇒ Hash, NilClass

Genius::Artists.artists_songs -> Hash | NilClass

Documents (songs) for the artist specified. By default, 20 items are returned for each request.

Parameters:

  • token (String) (defaults to: nil)

    Token to access api.genius.com.

  • id (String) (defaults to: nil)

    ID of the song.

  • options (Hash) (defaults to: {})

Options Hash (options:):

  • :per_page (Integer)

    Number of results to return per request.

  • :page (Integer)

    Paginated offset, (e.g., per_page=5&page=3 returns songs 11-15).

  • :sort (String)

    title (default) or popularity.

Returns:

  • (Hash)
  • (NilClass)

    if TokenError exception raised.

Raises:

  • (ArgumentError)

    if sort got incorrect value.

  • (ArgumentError)

    if per_page or page are negative.

  • (TokenError)

    if token or Genius::Auth.token are invalid.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/genius/api/artists.rb', line 42

def artists_songs(token: nil, id: nil, options: {})
  return if token.nil? && !Auth.authorized?.nil?

  Errors.validate_token(token) unless token.nil?

  sort_values = %w[title popularity]
  validate(sort_values, sort: options[:sort], per_page: options[:per_page], page: options[:page])

  params = options_helper(options, %i[sort per_page page])
  response = HTTParty.get("#{Api::RESOURCE}/artists/#{id}?access_token=#{token_ext(token)}#{params}").body
  JSON.parse(response)
end