Class: Scrobbler::Base

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

Direct Known Subclasses

Album, Artist, Auth, Event, Geo, Library, Playlist, Radio, Session, Shout, Tag, Track, User, Venue

Class Method Summary collapse

Class Method Details

.api_key=(api_key) ⇒ Object



12
13
14
# File 'lib/scrobbler/base.rb', line 12

def Base.api_key=(api_key) 
    @@api_key = api_key
end

.connectionObject



20
21
22
# File 'lib/scrobbler/base.rb', line 20

def Base.connection
    @connection ||= REST::Connection.new(API_URL)
end

.get(api_method, parent, element, parameters = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/scrobbler/base.rb', line 28

def Base.get(api_method, parent, element, parameters = {})
    scrobbler_class = "scrobbler/#{element.to_s}".camelize.constantize
    doc = request(api_method, parameters)
    elements = []
    doc.root.children.each do |child|
        next unless child.name == parent.to_s
        child.children.each do |child2|
            next unless child2.name == element.to_s
            elements << scrobbler_class.new_from_xml(child2)
        end
    end
    elements
end

.maybe_streamable_attribute(data, node) ⇒ Object

Check if the given libxml node has a streamable attribute defining if the given content is streamable. If so, set the flag in the given hash



18
19
20
21
22
# File 'lib/scrobbler/helper/streamable.rb', line 18

def Base.maybe_streamable_attribute(data, node)
  if node['streamable']
    data[:streamable] = ['1', 'true'].include?(node['streamable'])
  end
end

.maybe_streamable_node(data, node) ⇒ Object

Check if the given libxml node is defining if the given content is streamable. If so, set the flag in the given hash



10
11
12
13
14
# File 'lib/scrobbler/helper/streamable.rb', line 10

def Base.maybe_streamable_node(data, node)
  if node.name == 'streamable'
    data[:streamable] = ['1', 'true'].include?(node.content)
  end
end

.post_request(api_method, parameters = {}, request_method = 'get') ⇒ Object



42
43
44
# File 'lib/scrobbler/base.rb', line 42

def Base.post_request(api_method, parameters = {}, request_method = 'get')
  Base.request(api_method, parameters, 'post')
end

.request(api_method, parameters = {}, request_method = 'get') ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/scrobbler/base.rb', line 46

def Base.request(api_method, parameters = {}, request_method = 'get')
  parameters = {:signed => false}.merge(parameters)
  parameters['api_key'] = @@api_key
  parameters['method'] = api_method.to_s
  paramlist = []
  # Check if we want a signed call and pop :signed
  if parameters.delete :signed
    #1: Sort alphabetically
    params = parameters.sort{|a,b| a[0].to_s<=>b[0].to_s}
    #2: concat them into one string
    str = params.join('')
    #3: Append secret
    str = str + @@secret
    #4: Make a md5 hash
    md5 = Digest::MD5.hexdigest(str)
    params << [:api_sig, md5]
    params.each do |a|
      paramlist << "#{sanitize(a[0])}=#{sanitize(a[1])}"
    end
  else
    parameters.each do |key, value|
      paramlist << "#{sanitize(key)}=#{sanitize(value)}"
    end
  end
  url = '/2.0/?' + paramlist.join('&')
  XML::Document.string(self.connection.send(request_method,url))
end

.sanitize(param) ⇒ Object



24
25
26
# File 'lib/scrobbler/base.rb', line 24

def Base.sanitize(param)
  URI.escape(param.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
end

.secret=(secret) ⇒ Object



16
17
18
# File 'lib/scrobbler/base.rb', line 16

def Base.secret=(secret)
    @@secret = secret
end