Class: Shortwave::Authentication::Session

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

Overview

Base functionality for session-based authentication mechanisms. Don’t use this directly - use one of its subclasses: Web, Desktop or Mobile.

Direct Known Subclasses

Desktop, Mobile, Web

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret, session_key = nil) ⇒ Session

Creates a new session with your api account key and secret. If you have already authenticated and stored a session key, you can provide it to save having to authenticate again.



25
26
27
28
# File 'lib/shortwave/authentication.rb', line 25

def initialize(api_key, secret, session_key=nil)
  @api_key, @secret, @session_key = api_key, secret, session_key
  @facade = Facade::Auth.new(self)
end

Instance Attribute Details

#session_keyObject (readonly)

Returns the value of attribute session_key.



20
21
22
# File 'lib/shortwave/authentication.rb', line 20

def session_key
  @session_key
end

Instance Method Details

#merge!(type, params) ⇒ Object

Merges relevant authentication details with method parameters.

Raises:



36
37
38
39
40
41
42
43
# File 'lib/shortwave/authentication.rb', line 36

def merge!(type, params)
  raise NotAuthenticated.new("Requires authentication!") if type == :session && @session_key.nil?

  params.merge!(:api_key => @api_key)
  params.merge!(:sk => @session_key) if type == :session
  params.merge!(:api_sig => signature(params)) if type == :session || type == :signed
  params
end

#signature(params) ⇒ Object

Generates a method signature for method parameters.



46
47
48
49
# File 'lib/shortwave/authentication.rb', line 46

def signature(params)
  sorted_params = params.map {|k,v| [k.to_s, v.to_s] }.sort_by {|a| a[0] }
  MD5.hexdigest(sorted_params.flatten.join("") + @secret)
end

#signed_in?Boolean

Is the user signed in to Last.fm?

Returns:

  • (Boolean)


31
32
33
# File 'lib/shortwave/authentication.rb', line 31

def signed_in?
  ! @session_key.nil?
end