Class: Scrobbler::Search

Inherits:
Base
  • Object
show all
Defined in:
lib/scrobbler/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

api_key=, connection, fetch_and_parse, sanitize

Constructor Details

#initialize(api_key) ⇒ Search

Returns a new instance of Search.



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

def initialize(api_key)
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#queryObject

Returns the value of attribute query.



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

def query
  @query
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Instance Method Details

#api_pathObject



55
56
57
# File 'lib/scrobbler/search.rb', line 55

def api_path
  "/2.0/?method=#{type}.search&#{type}=#{CGI::escape(query)}&api_key=#{api_key}"
end

#by_album(album_name) ⇒ Object



37
38
39
40
41
# File 'lib/scrobbler/search.rb', line 37

def by_album(album_name)
  @type = 'album'
  @query = album_name
  execute
end

#by_artist(artist_name) ⇒ Object



43
44
45
46
47
# File 'lib/scrobbler/search.rb', line 43

def by_artist(artist_name)
  @type = 'artist'
  @query = artist_name
  execute
end

#by_track(track_name) ⇒ Object



49
50
51
52
53
# File 'lib/scrobbler/search.rb', line 49

def by_track(track_name)
  @type = 'track'
  @query = track_name
  execute
end

#executeObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/scrobbler/search.rb', line 14

def execute
  doc = self.class.fetch_and_parse(api_path)
  results = []
  if type == 'album'
    (doc/"//album").each do |album|
      artist = album/"artist"
      name = album/"name"
      results << Album.new(artist.inner_html, name.inner_html, :include_info => true)
    end
  elsif type == 'artist'
    (doc/"//artist/name").each do |name|
      results << Artist.new(name.inner_html)
    end
  elsif type == 'track'
    (doc/"//track").each do |track|
      artist = track/"artist"
      name = track/"name"
      results << Track.new(artist.inner_html, name.inner_html)
    end
  end
  results
end