Sha256: 7282d63eca9ce84ef25fa7818650b1293856c91e2a03be8106f7151872f76962

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

module Genius
  class Referent < Resource
    attr_reader :id, :url, :song_id, :annotator_id, :fragment, :range, :classification,
                :annotatable, :annotations

    def self.find(*)
      raise NotImplementedError, "A Referent cannot be loaded by its ID in the public API"
    end

    def self.where(params = {})
      headers = default_headers.merge(params.delete(:headers) || {})
      params = default_params.merge(params)

      response = http_get("/#{resource_name}s/", query: params, headers: headers)

      response.parsed_response["response"]["referents"].map do |referent|
        self.from_hash(referent, text_format: params[:text_format])
      end
    end

    def reload
      raise NotReloadableError, "A Referent cannot be reloaded"
    end

    def parse_resource!
      @url = resource["url"]

      @annotations = resource["annotations"].map do |annotation|
        Annotation.from_hash(annotation)
      end

      @song_id = resource["song_id"]
      @annotator_id = resource["annotator_id"]
      @fragment = resource["fragment"]
      @range = resource["range"]
      @classification = resource["classification"]
      @annotatable = resource["annotatable"]
      @id = resource["id"]
    end

    def song
      return nil unless song_id
      Song.find(song_id)
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
genius-0.1.2 lib/genius/referent.rb
genius_fixed-0.1.2 lib/genius/referent.rb
genius-0.1.1 lib/genius/referent.rb