Sha256: 484ad886a4897460536431285f6adee0065ce9e27e2bfa96e88c8f336950ee27

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

Contents

module RapGenius
  class Line
    include RapGenius::Client

    attr_reader :id

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

    def initialize(kwargs)
      @id = kwargs.delete(:id)
      @song = kwargs.delete(:song)
      @lyric = kwargs.delete(:lyric)
      self.url = "referents/#{@id}" if @id
    end

    def response
      return nil unless @id
      document["response"]["referent"]
    end

    def lyric
      if @id
        @lyric ||= response["fragment"]
      else
        @lyric
      end
    end

    def annotated?
      !!@id
    end

    alias_method :explained?, :annotated?

    # A line can have multiple annotations, usually if it has a community one
    # and a verified one. Ideally, these would be encapsulated into an
    # Annotation class, but I don't have time for now.
    def explanations
      return nil unless @id
      @explanation ||= response["annotations"].map do |annotation|
        annotation["body"]["dom"]["children"].map do |node|
          parse_description(node)
        end.join("")
      end.flatten
    end

    alias_method :annotations, :explanations

    def song
      if @id
        @song ||= Song.find(response['song_id'])
      else
        @song
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rapgenius-1.0.3 lib/rapgenius/line.rb
rapgenius-1.0.2 lib/rapgenius/line.rb
rapgenius-1.0.1 lib/rapgenius/line.rb
rapgenius-1.0.0 lib/rapgenius/line.rb