Module: EchonestResource::Base::ClassMethods

Defined in:
lib/echonest_resource/base.rb

Instance Method Summary collapse

Instance Method Details

#echonest_resource(resource) ⇒ Object

Raises:

  • (StandardError)


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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/echonest_resource/base.rb', line 17

def echonest_resource(resource)
  raise StandardError if resource.blank?

  include HTTParty
  base_uri 'http://developer.echonest.com/api/v4'
  format :json 
  
  case resource
  when :artist
    instance_eval <<-EOV
      def search(name, options={})
        raise StandardError if name.blank?

        results = Hashie::Mash.new(self.get('/artist/search', :query => 
          request_params.merge({:name => name}).merge(options)
        ))
        if results.response && results.response.artists; results.response.artists; end;
      end
    EOV
  when :song
    instance_eval <<-EOV
      def find_by_artist_id(id, options={})
        raise StandardError if id.blank?
    
        results = Hashie::Mash.new(self.get('/song/search', :query =>
          request_params.merge(options).merge({
            :artist_id => id
          })
        ))
        if results.response && results.response.songs; results.response.songs; end;
      end
      
      def find_by_artist_name(name, options={})
        raise StandardError if name.blank?
        
        if artist = Artist.search(name)
          Song.find_by_artist_id(artist.first.id, options)
        end
      end
      
      def search(title, options={})
        raise StandardError if title.blank?
        
        results = Hashie::Mash.new(self.get('/song/search', :query => 
          request_params.merge({:title => title}).merge(options)
        ))
        results
      end
    EOV
  end
        
end

#request_paramsObject



10
11
12
13
14
15
# File 'lib/echonest_resource/base.rb', line 10

def request_params
  {
    :api_key  => EchonestResource.api_key,
    :results  => 1
  }
end