Class: Scrobbler::Authentication

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

Instance Method Summary collapse

Methods inherited from Base

add_cache, api_key=, #call, #call_pageable, fetch_http, get, load_from_cache, #populate_data, post_request, #request, request, sanitize, save_to_cache, secret=, validate_response_document

Constructor Details

#initialize(data = {}) ⇒ Authentication



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/scrobbler/authentication.rb', line 7

def initialize(data = {})
  populate_data(data)
  super()
  
  @digest = Digest::MD5.new
  
  # Check if the input parameters are valid
  if @username.nil? || @username.empty?
    raise ArgumentError, 'Username is required'
  end
end

Instance Method Details

#auth_token(password) ⇒ Object



19
20
21
# File 'lib/scrobbler/authentication.rb', line 19

def auth_token(password)
  @digest.hexdigest(@username + @digest.hexdigest(password))
end

#desktop_session_url(tokenStr) ⇒ Object



59
60
61
# File 'lib/scrobbler/authentication.rb', line 59

def desktop_session_url(tokenStr)
  webservice_session_url + '&token=' + tokenStr 
end

#load_session_out_of_session_request(xml) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/scrobbler/authentication.rb', line 33

def load_session_out_of_session_request(xml)
  # Find the session node
  xml.root.children.each do |child|
      next unless child.name == 'session'
      return Session.new(:xml => child)
  end
end

#mobile_session(password) ⇒ Object



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

def mobile_session(password)
  xml = Base.request('auth.getMobileSession', {:username => @username, :signed => true, :authToken => auth_token(password)})
  load_session_out_of_session_request(xml)
end

#session(tokenStr) ⇒ Object



28
29
30
31
# File 'lib/scrobbler/authentication.rb', line 28

def session(tokenStr)
  xml = Base.request('auth.getSession', {:signed => true, :token => tokenStr})
  load_session_out_of_session_request(xml)
end

#tokenObject



41
42
43
44
45
46
47
48
# File 'lib/scrobbler/authentication.rb', line 41

def token
  xml = Base.request('auth.getToken', {:signed => true})
  # Grep the token
  xml.root.children.each do |child|
      next unless child.name == 'token'
      return child.content
  end
end

#webservice_session_url(callback = nil) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/scrobbler/authentication.rb', line 50

def webservice_session_url(callback=nil)
  if not callback.nil? then
    cb = "&cb=" + CGI::escape(callback)
  else
    cb = ''
  end
  'http://www.last.fm/api/auth/?api_key=' + @@api_key + cb 
end