Class: MetaSpotify::Base

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/meta-spotify.rb

Direct Known Subclasses

Album, Artist, Track

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



14
15
16
# File 'lib/meta-spotify.rb', line 14

def name
  @name
end

#popularityObject (readonly)

Returns the value of attribute popularity.



14
15
16
# File 'lib/meta-spotify.rb', line 14

def popularity
  @popularity
end

#uriObject (readonly)

Returns the value of attribute uri.



14
15
16
# File 'lib/meta-spotify.rb', line 14

def uri
  @uri
end

Class Method Details

.lookup(uri, opts = {}) ⇒ Object

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/meta-spotify.rb', line 52

def self.lookup(uri, opts={})
  uri = uri.strip
  raise URIError.new("Spotify URI not in the correct syntax") unless uri_regex.match(uri)
  query = {:uri => uri}
  query[:extras] = opts[:extras] if opts.has_key? :extras
  result = get("/lookup/#{API_VERSION}/",:query => query, :format => :xml)
  raise_errors(result)
  result.each do |k,v|
    v.merge!({'href' => uri})
    case k
    when "artist"
      return Artist.new(v)
    when "album"
      return Album.new(v)
    when "track"
      return Track.new(v)
    end
  end
end

.search(string, opts = {}) ⇒ Object



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
# File 'lib/meta-spotify.rb', line 20

def self.search(string, opts={})
  item_name = self.name.downcase.gsub(/^.*::/,'')
  query = {:q => string}
  query[:page] = opts[:page].to_s if opts.has_key? :page
  result = get("/search/#{API_VERSION}/#{item_name}",
    :query => query,
    :format => :xml,
    :query_string_normalizer => self.method(:normalize))
  raise_errors(result)
  result = result[item_name+'s']
  items = []
  unless result[item_name].nil?
    if result[item_name].is_a? Array
      result[item_name].each do |item|
        items << self.new(item)
      end
    else
      items << self.new(result[item_name])
    end
  end
  return { (item_name+'s').to_sym => items,
           :query => {
             :start_page => result["Query"]["startPage"].to_i,
             :role => result["Query"]["role"],
             :search_terms => result["Query"]["searchTerms"]
           },
           :items_per_page => result["itemsPerPage"].to_i,
           :start_index => result["startIndex"].to_i,
           :total_results => result["totalResults"].to_i
          }
end

.uri_regexObject



16
17
18
# File 'lib/meta-spotify.rb', line 16

def self.uri_regex
  nil
end

Instance Method Details

#spotify_idObject



72
73
74
75
76
77
78
# File 'lib/meta-spotify.rb', line 72

def spotify_id
  if uri
    uri[self.class.uri_regex, 1]
  else
    nil
  end
end