Sha256: a0df647458ab21a04161567b05740e7d021c5fb1ea8e936912daf5d9d448bb93

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

# encoding: utf-8
module RapGenius
  class Artist
    include RapGenius::Client

    attr_reader :id

    def self.find(id)
      self.new(id: id).tap { |artist| artist.document }
    end

    def initialize(kwargs = {})
      @id = kwargs.delete(:id)
      @name = kwargs.delete(:name)
      @type = kwargs.delete(:type)
      self.url = "artists/#{@id}"
    end

    def response
      document["response"]["artist"]
    end

    def name
      @name ||= response["name"]
    end

    def image
      @image ||= response["image_url"]
    end

    def url
      response["url"]
    end

    def description
      @description ||= response["description"]["dom"]["children"].map do |node|
        parse_description(node)
      end.flatten.join("")
    end

    # You seem to be able to load 25 songs at a time for an artist. I haven't
    # found a way to vary the number you get back from the query, but you can
    # paginate through in blocks of 25 songs.
    def songs(options = {page: 1})
      songs_url = "/artists/#{@id}/songs/?page=#{options[:page]}"
      
      fetch(songs_url)["response"]["songs"].map do |song|
        Song.new(
          artist: Artist.new(
            name: song["primary_artist"]["name"],
            id: song["primary_artist"]["id"],
            type: :primary
          ),
          title: song["title"],
          id: song["id"]
        )
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rapgenius-1.0.3 lib/rapgenius/artist.rb