Sha256: ef14a0392d4280b7b578cc0c884a25762b070163cffe93539227ff69c1b92ed7

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 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"]["plain"]
      end
    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.1.2 lib/rapgenius/line.rb
rapgenius-1.1.1 lib/rapgenius/line.rb
rapgenius-1.1.0 lib/rapgenius/line.rb
rapgenius-1.0.5 lib/rapgenius/line.rb