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_with_auth(method, query = {}, options = {}) ⇒ Object

implements signed requests



66
67
68
# File 'lib/scrobbler2/base.rb', line 66

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

.has_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
37
38
39
# File 'lib/scrobbler2/base.rb', line 16

def self.has_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]

    if options[:query]
      query.merge!(options[:query])
    elsif @query
      query.merge!(@query)
    end

    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

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

TODO this needs to be DRYed up, wrt has_resource



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/scrobbler2/base.rb', line 42

def self.has_singleton_resource(name, options={})
  (class << self; self; end).class_eval do
    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.merge!(options[:query]) if options[:query]

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

      instance_variable_set("@#{name}", value)
    end
  end
end

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/scrobbler2/base.rb', line 70

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



85
86
87
# File 'lib/scrobbler2/base.rb', line 85

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

.query_signature(query) ⇒ Object



94
95
96
97
98
# File 'lib/scrobbler2/base.rb', line 94

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



89
90
91
92
# File 'lib/scrobbler2/base.rb', line 89

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