Class: Scrobbler2::Base

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

Direct Known Subclasses

Album, Artist, Auth, Event, Geo, Group, Library, Playlist, Tag, Track, User, Venue

Class Method Summary collapse

Class Method Details

.get(method, query = {}, options = {}) ⇒ Object



9
10
11
12
13
14
# File 'lib/scrobbler2/base.rb', line 9

def self.get(method, query={}, options={})
  query = query.merge({:api_key => api_key, :method => method, :format => 'json'})
  options = options.merge({:query => query})

  response = HTTParty.get('http://ws.audioscrobbler.com/2.0/', options)
end

.get_resource(name, options = {}) ⇒ Object



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

def self.get_resource(name, options = {})
  define_method name do |*args|
    query = args[1] || {}
    local_options = args[0] || {}
    local_value = instance_variable_get("@#{name.to_s}")
    return local_value if local_value && !options[:force]

    query = query.merge options[:query] if options[:query]
    query = query.merge(@query) if @query && !options[:query]
    
    method_name = options[:resource_name] || self.class.name.split("::").last.downcase + ".get#{name.to_s.tr('_', '').downcase}" 
    if options[:auth]
      value = self.class.get_with_auth(method_name, query, local_options)
    else
      value = self.class.get(method_name, query, local_options)                
    end
    value = value[options[:root]] if options[:root]
    
    instance_variable_set("@#{name}", value)
  end   
end

.get_with_auth(method, query = {}, options = {}) ⇒ Object

implements signed requests



39
40
41
# File 'lib/scrobbler2/base.rb', line 39

def self.get_with_auth(method, query={}, options={})
  http_with_auth(:get, method, query, options)
end

.http_with_auth(http_method, method, query = {}, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/scrobbler2/base.rb', line 43

def self.http_with_auth(http_method, method, query={}, options={})
  query = query.merge({:api_key => api_key, :method => method})
  query = query.merge({:sk => session_key}) if session_key
  
  signature = sign(query)      
  query = query.merge({:api_sig => signature})

  if(http_method == :get)
    options = options.merge({:query => query})
  else
    options = options.merge({:body => query})
  end
  response = HTTParty.send(http_method, 'http://ws.audioscrobbler.com/2.0/', options)
end

.post_with_auth(method, query = {}, options = {}) ⇒ Object



58
59
60
# File 'lib/scrobbler2/base.rb', line 58

def self.post_with_auth(method, query={}, options={})
  http_with_auth(:post, method, query, options)      
end

.query_signature(query) ⇒ Object



67
68
69
70
71
# File 'lib/scrobbler2/base.rb', line 67

def self.query_signature(query)
  signature = query.sort {|a,b| a[0].to_s <=> b[0].to_s }.map { |param| param.join('') }.join('')
  signature = "#{signature}#{api_secret}"
  signature
end

.sign(query) ⇒ Object



62
63
64
65
# File 'lib/scrobbler2/base.rb', line 62

def self.sign(query)
  signature = query_signature(query)
  md5 = Digest::MD5.hexdigest(signature)
end