Class: Spotifiery::SearchResult::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/spotifiery/search_result/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(response, response_class = nil) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
17
18
# File 'lib/spotifiery/search_result/base.rb', line 11

def initialize response, response_class = nil

  response_hash = HashWithIndifferentAccess.new response        
  @info = response_hash[:info]
  response_results = response_hash[ ActiveSupport::Inflector.pluralize(@info[:type]) ]
  response_class = ActiveSupport::Inflector.constantize( "Spotifiery::Searchable::" + ActiveSupport::Inflector.titleize(@info[:type])) if response_class.blank?
  @results = response_results.map{|result_hash| response_class.new(result_hash) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



25
26
27
28
29
30
31
32
33
# File 'lib/spotifiery/search_result/base.rb', line 25

def method_missing(method, *args, &block)
  if defined?(@info) && @info.has_key?(method)
    @info[method]
  elsif defined?(@info) && method.eql?( ActiveSupport::Inflector.pluralize(@info[:type]).to_sym )
    @results
  else
    super
  end
end

Instance Method Details

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/spotifiery/search_result/base.rb', line 35

def respond_to_missing?(method, include_private = false)
  @info.has_key?(method) || method.eql?(ActiveSupport::Inflector.pluralize(@info[:type]).to_sym)
end

#resultsObject



20
21
22
# File 'lib/spotifiery/search_result/base.rb', line 20

def results
  @results
end