Class: SunDawg::LastFmClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLastFmClient

Returns a new instance of LastFmClient.



10
11
12
# File 'lib/last_fm_client.rb', line 10

def initialize
  @service_host = "http://ws.audioscrobbler.com"
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



8
9
10
# File 'lib/last_fm_client.rb', line 8

def api_key
  @api_key
end

#service_hostObject

Returns the value of attribute service_host.



8
9
10
# File 'lib/last_fm_client.rb', line 8

def service_host
  @service_host
end

Instance Method Details

#get_similar_artists(name, limit = 20) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/last_fm_client.rb', line 26

def get_similar_artists(name, limit = 20)
  xml = SunDawg::Util.do_http_get(service_host, "/2.0/" + SunDawg::Util::build_query_string({"method" => "artist.getsimilar", "artist" => name, "api_key" => @api_key}))
  doc = REXML::Document.new(xml)
  artists = []
  doc.elements.each("/lfm/similarartists/artist") { |e|
    artists << parse_element(SunDawg::Artist.new, e)
  }
  artists
end

#get_similar_tracks(name, artist) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/last_fm_client.rb', line 14

def get_similar_tracks(name, artist)
  endpoint = "/2.0/" + SunDawg::Util::build_query_string({"method" => "track.getsimilar", "artist" => artist, "track" => name, "api_key" => @api_key})
  xml = SunDawg::Util.do_http_get(service_host, endpoint)
  doc = REXML::Document.new(xml)
  tracks = []
  doc.elements.each("/lfm/similartracks/track") { |e|
    tracks << parse_element(SunDawg::Track.new, e) 
    tracks.last.artist = parse_element(SunDawg::Artist.new, e.elements["artist"])
  }
  tracks
end