Class: NBS::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/next-big-sound/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, options = {}) ⇒ Search

Returns a new instance of Search.



10
11
12
13
14
15
# File 'lib/next-big-sound/search.rb', line 10

def initialize(query, options={})
  self.options = {"q"=>query,"format"=>"xml"}.merge(options)
  self.query = query
  self.api_key = api_key
  self.base_url = base_url
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



8
9
10
# File 'lib/next-big-sound/search.rb', line 8

def api_key
  @api_key
end

#base_urlObject

Returns the value of attribute base_url.



8
9
10
# File 'lib/next-big-sound/search.rb', line 8

def base_url
  @base_url
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/next-big-sound/search.rb', line 8

def options
  @options
end

#queryObject

Returns the value of attribute query.



8
9
10
# File 'lib/next-big-sound/search.rb', line 8

def query
  @query
end

#xmlObject

Returns the value of attribute xml.



8
9
10
# File 'lib/next-big-sound/search.rb', line 8

def xml
  @xml
end

Instance Method Details

#artists(options = {}) ⇒ Object

puts self.xml



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/next-big-sound/search.rb', line 21

def artists(options={})
  hash = Hash.from_xml(self.to_xml)
  a = []
  #puts hash.inspect    
  begin
    hash["data"][0]["artists"][0]["artist"].each do |item|
      a << NBS::Artist.new(item["id"],item["name"][0],options)
    end
  rescue
  end
  return a
end

#fetchObject



16
17
18
19
20
# File 'lib/next-big-sound/search.rb', line 16

def fetch
  puts "#{$nbs_api_key}artists/search?#{self.options.to_url_params}"
  self.xml=Net::HTTP.get(URI.parse("#{$nbs_api_key}artists/search.#{self.options["format"]}?#{self.options.to_url_params}")).to_s
  #puts self.xml
end

#to_xmlObject



33
34
35
# File 'lib/next-big-sound/search.rb', line 33

def to_xml
  self.xml ||=fetch
end