Sha256: 5c0629a960b2fd596a346ade5429559b0751e0a98ff57aa77ccb37b6be986080

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module RSpotify

  class Base

    def self.find(id, type)
      pluralized_type = "#{type}s"
      type_class = eval type.capitalize

      json = RSpotify.get "#{pluralized_type}/#{id}"
      type_class.new json
    end

    def self.search(query, type, limit = 20, offset = 0)
      pluralized_type = "#{type}s"
      type_class = eval type.capitalize

      json = RSpotify.get 'search',
        params: {
          q:      query,
          type:   type,
          limit:  limit,
          offset: offset
        }

      items = json[pluralized_type]['items']
      items.map { |item| type_class.new item }
    end

    def initialize(options = {})
      @external_urls = options['external_urls']
      @href          = options['href']
      @id            = options['id']
      @type          = options['type']
      @uri           = options['uri']
    end

    def complete!
      pluralized_type = "#{type}s"
      initialize RSpotify.get("#{pluralized_type}/#{@id}")
    end

    def method_missing(method_name, *args)
      attr = "@#{method_name}".to_sym
      super unless instance_variables.include? attr 

      attr_value = instance_variable_get attr 
      return attr_value unless attr_value.nil?

      complete!
      instance_variable_get attr 
    end

    def respond_to?(method_name)
      attr = "@#{method_name}".to_sym
      return true if instance_variables.include? attr
      super
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspotify-0.5.0 lib/rspotify/base.rb