Class: EchoNest::Artist

Inherits:
Object
  • Object
show all
Defined in:
lib/echonest/artist.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil) ⇒ Artist

Returns a new instance of Artist.



6
7
8
# File 'lib/echonest/artist.rb', line 6

def initialize(id=nil)
  @id = id if id
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/echonest/artist.rb', line 4

def id
  @id
end

Class Method Details

.find(query, options = {}) ⇒ Object



54
55
56
57
58
59
# File 'lib/echonest/artist.rb', line 54

def self.find(query, options={})
  options = {:query => query}.merge(options)
  request = ApiRequest.new("search_artists", options)
  results = Xml::ArtistSearchResults.parse(request.fetch)
  results.artists
end

.service(name, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/echonest/artist.rb', line 10

def self.service(name, options = {})

  define_method "get_#{name.to_s}" do |*args|
    options = args[0] if args.length == 1
    options = {:id => id}.merge(options)
    request = ApiRequest.new("get_#{name.to_s}", options)
    "Xml::#{name.to_s.titleize}Result".constantize.parse request.fetch
  end

  if !options[:singular]
    define_method name do
      return instance_variable_get("@#{name.to_s}") if instance_variable_get("@#{name.to_s}")
      result = self.send("get_#{name.to_s}")
      pager = Proc.new { |start, rows| 
        self.send("get_#{name.to_s}", :start => start, :rows => rows).results
      }
      page = PagedResult.new result.results, pager 
      instance_variable_set("@#{name.to_s}",  page)
    end
  else
    if !options[:provides]
      define_method name do
        return instance_variable_get("@#{name.to_s}") if instance_variable_get("@#{name.to_s}")
        result = self.send("get_#{name.to_s}")
        instance_variable_set("@#{name.to_s}", result.artist.send(name))
      end
    else

      define_method name do
        return instance_variable_get("@#{name.to_s}") if instance_variable_get("@#{name.to_s}")
        result = self.send("get_#{name.to_s}")
        instance_variable_set("@#{name.to_s}", result.artist)
      end

      options[:provides].each do |prop|
        define_method prop do
          return instance_variable_get("@#{prop.to_s}") if instance_variable_get("@#{prop.to_s}")
          instance_variable_set("@#{prop.to_s}",  self.send(name).send(prop))
        end
      end
    end
  end    
end