Module: LastFm

Defined in:
lib/last_fm.rb,
lib/last_fm/models/tag.rb,
lib/last_fm/models/base.rb,
lib/last_fm/models/user.rb,
lib/last_fm/models/album.rb,
lib/last_fm/models/chart.rb,
lib/last_fm/models/event.rb,
lib/last_fm/models/track.rb,
lib/last_fm/models/artist.rb

Defined Under Namespace

Classes: Album, Artist, Base, Chart, Event, Tag, Track, User

Class Method Summary collapse

Class Method Details

.authenticated_request(method, params, type = 'hash') ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/last_fm.rb', line 82

def authenticated_request(method, params,type = 'hash')
  http = Net::HTTP.new("ws.audioscrobbler.com",80)
  path = @prefix + method
  params.each_pair do |key,value|
    path << "&#{key}=#{value}"
  end
  path << '&api_sig=' + get_signature(method,params)
  resp, data = http.get(urlize(path))
  if resp.code == "200"
    if type == 'hash'
      hash = Hash.from_xml(data)['lfm']
      hash.shift
      return hash
    else 
      return data
    end 
  else 
    return false
  end
end

.fetch_data(path) ⇒ Object

fetch lastfm



41
42
43
44
45
# File 'lib/last_fm.rb', line 41

def fetch_data(path)
  http       = Net::HTTP.new("ws.audioscrobbler.com",80)
  resp, data = http.get(urlize(@prefix+path+"&api_key="+@key))
  return resp.code == "200" ? data : false  
end

.get_session(token) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/last_fm.rb', line 51

def get_session(token)
  signature = get_signature('auth.getsession',{:api_key => @key, :token => token}) 
  if @session = connect_with_auth('auth.getsession',{ :api_key => @ey, :token => token, :signature => signature })
    session[:lastfm_name] = @session['session']['name']
    session[:lastfm_key]  = @session['session']['key']
  else 
    return false
  end 
end

.get_signature(method, params) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/last_fm.rb', line 30

def get_signature(method,params)
  signature = []
  signature << 'method' + method
  params.each_pair do |key,value|
      signature << key.to_s + value
  end 
  signature = signature.sort.join + @secret
  return Digest::MD5.hexdigest(signature)
end

.load_configuration(key, secret = nil, prefix = nil) ⇒ Object



23
24
25
26
27
28
# File 'lib/last_fm.rb', line 23

def load_configuration(key, secret = nil, prefix = nil)
  @key    = key
  @secret = secret
  @prefix = prefix || "/2.0/?api_key=#{@key}&method="
  return true
end

.post(method, posted) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/last_fm.rb', line 103

def post(method,posted)
  posted[:api_key] = @key
  posted[:sk]      = session[:lastfm_key]
  signature        = get_signature(method,posted)
  posted[:api_sig] = get_signature(method,posted)
  posted[:method]  = method
  resp = Net::HTTP.post_form(URI.parse('http://ws.audioscrobbler.com/2.0/'),posted)
  case resp
    when Net::HTTPSuccess, Net::HTTPRedirection
      return true
    else
      return false
  end
end

.request(method, params, type = 'hash') ⇒ Object

get_lastfm



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/last_fm.rb', line 62

def request(method,params,type='hash')
  http = Net::HTTP.new("ws.audioscrobbler.com",80)
  path = @prefix + method
  params.each_pair do |key,value|
    path << "&#{key}=#{value}"
  end
  resp, data = http.get(urlize(path))
  if resp.code == "200"
    if type == 'hash'
      hash = Hash.from_xml(data)['lfm']
      hash.shift
      return hash
    else
      return data
    end
  else 
    return resp.body
  end 
end

.urlize(string) ⇒ Object



47
48
49
# File 'lib/last_fm.rb', line 47

def urlize(string)
  return string.gsub(/\ +/, '%20')
end