Module: Spotifiery::Searchable::Base

Extended by:
ActiveSupport::Concern
Included in:
Album, Artist, Track
Defined in:
lib/spotifiery/searchable/base.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

BASE_SEARCH_URL =
"http://ws.spotify.com/search/1/"
BASE_LOOKUP_URL =
"http://ws.spotify.com/lookup/1/.json"

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/spotifiery/searchable/base.rb', line 59

def method_missing(method, *args, &block)
  
  # Try to find the method in base_attributes
  res = get_from_base_attrs(method)
  # Looked in spotify before?
  lookup_in_spotify if res.blank? && !defined?(@lookup_response)
  res = get_from_base_attrs(method)
  if res.blank?
  # If not try to see if an instance variable is defined          
    if instance_variable_defined?("@#{method}")            
      res = instance_variable_get("@#{method}")
    else
      super
    end        
  end
  res
end

Instance Method Details

#initialize(spotify_uri_or_hash) ⇒ Object

Can be initialized from a spotify uri or from an search result hash. This way Spotify is not asked when initialized from a search result when name, popularity, etc are called



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/spotifiery/searchable/base.rb', line 41

def initialize spotify_uri_or_hash
  @base_attributes = {}
  if spotify_uri_or_hash.is_a? Hash          
    initialize_base_attributes_getters HashWithIndifferentAccess.new spotify_uri_or_hash
    @href = spotify_uri_or_hash['href']

  elsif spotify_uri_or_hash.is_a? String          
    
    @href = spotify_uri_or_hash
    
  else
    raise "Wrong initialize params"
  end
end