Module: LastFM

Defined in:
lib/lastfm/geo.rb,
lib/lastfm/tag.rb,
lib/rscrobbler.rb,
lib/lastfm/auth.rb,
lib/lastfm/user.rb,
lib/lastfm/wiki.rb,
lib/lastfm/album.rb,
lib/lastfm/chart.rb,
lib/lastfm/event.rb,
lib/lastfm/group.rb,
lib/lastfm/radio.rb,
lib/lastfm/shout.rb,
lib/lastfm/track.rb,
lib/lastfm/venue.rb,
lib/lastfm/artist.rb,
lib/lastfm/struct.rb,
lib/lastfm/buylink.rb,
lib/lastfm/library.rb,
lib/lastfm/playlist.rb,
lib/lastfm/tasteometer.rb

Defined Under Namespace

Classes: Album, Artist, Auth, AuthenticationError, Buylink, Chart, Event, Geo, Group, LastFMError, Library, Playlist, Radio, Shout, Struct, Tag, Tasteometer, Track, User, Venue, Wiki

Constant Summary collapse

VERSION =
'0.3.1'
HOST =
'ws.audioscrobbler.com'
API_VERSION =
'2.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



43
44
45
# File 'lib/rscrobbler.rb', line 43

def api_key
  @api_key
end

.api_secretObject

Returns the value of attribute api_secret.



43
44
45
# File 'lib/rscrobbler.rb', line 43

def api_secret
  @api_secret
end

.auth_tokenObject

Returns the value of attribute auth_token.



43
44
45
# File 'lib/rscrobbler.rb', line 43

def auth_token
  @auth_token
end

.loggerObject

Returns the value of attribute logger.



43
44
45
# File 'lib/rscrobbler.rb', line 43

def logger
  @logger
end

.session_keyObject

Returns the value of attribute session_key.



43
44
45
# File 'lib/rscrobbler.rb', line 43

def session_key
  @session_key
end

.usernameObject

Returns the value of attribute username.



43
44
45
# File 'lib/rscrobbler.rb', line 43

def username
  @username
end

Class Method Details

.authenticate!String

Authenticate the service with provided login credentials. Use mobile authentication to avoid redirecting to the website to log in.

Returns:

  • (String)

    session key provided from authentication

See Also:



70
71
72
73
74
75
# File 'lib/rscrobbler.rb', line 70

def authenticate!
  [:api_key, :api_secret, :username, :auth_token].each do |cred|
    raise AuthenticationError, "Missing credential: #{cred}" unless LastFM.send(cred)
  end
  self.session_key = Auth.get_mobile_session( username: username, auth_token: auth_token ).find_first('session/key').content
end

.authenticated?Boolean

Has the service been authenticated?

Returns:

  • (Boolean)

    whether the service has been authenticated



80
81
82
# File 'lib/rscrobbler.rb', line 80

def authenticated?
  !!session_key
end

.establish_session {|_self| ... } ⇒ String

Configure the module and begin a session. Once established (and successfully executed), the module is ready to send api calls to Last.fm.

Examples:

LastFM.establish_session do |session|
  session.username = ...
  session.auth_token = ...
  session.api_key = ...
  session.api_secret = ...
end

Parameters:

  • &block (Block)

    block used to configure the module’s attributes

Yields:

  • (_self)

Yield Parameters:

  • _self (LastFM)

    the object that the method was called on

Returns:

  • (String)

    session key if successfully connected

Raises:



60
61
62
63
# File 'lib/rscrobbler.rb', line 60

def establish_session(&block)
  yield self
  self.authenticate!
end

.generate_auth_token(password) ⇒ String

Generate auth token from username and given password.

Parameters:

  • password (String)

    password to use

Returns:

  • (String)

    md5 digest of the username and password



95
96
97
# File 'lib/rscrobbler.rb', line 95

def generate_auth_token( password )
  self.auth_token = Digest::MD5.hexdigest( username + Digest::MD5.hexdigest(password) )
end

.get(method, params = {}, secure = false) ⇒ LibXML::XML::Document

Construct an HTTP GET call from params, and load the response into a LibXML Document.

Parameters:

  • method (String)

    last.fm api method to call

  • secure (Boolean) (defaults to: false)

    whether to sign the request with a method signature and session key (one exception being auth methods, which require a method signature but no session key)

  • params (Hash) (defaults to: {})

    parameters to send, excluding method, api_key, api_sig, and sk

Returns:

  • (LibXML::XML::Document)

    xml document of the data contained in the response

Raises:



107
108
109
110
111
112
# File 'lib/rscrobbler.rb', line 107

def get( method, params = {}, secure = false )
  path = generate_path(method, secure, params)
  logger.debug( "Last.fm HTTP GET: #{HOST}#{path}" ) if logger
  response = Net::HTTP.get_response( HOST, path )
  validate( LibXML::XML::Parser.string( response.body ).parse )
end

.post(method, params) ⇒ LibXML::XML::Document

Construct an HTTP POST call from params, and check the response status.

Parameters:

  • method (String)

    last.fm api method to call

  • params (Hash)

    parameters to send, excluding method, api_key, api_sig, and sk

Returns:

  • (LibXML::XML::Document)

    xml document of the data contained in the response

Raises:



120
121
122
123
124
125
126
# File 'lib/rscrobbler.rb', line 120

def post( method, params )
  post_uri = URI.parse("http://#{HOST}/#{API_VERSION}/")
  params = construct_params( method, :secure, params )
  logger.debug( "Last.fm HTTP POST: #{post_uri}, #{params.to_s}" ) if logger
  response = Net::HTTP.post_form( post_uri, params )
  validate( LibXML::XML::Parser.string( response.body ).parse )
end

.requires_authenticationObject

Ensure the service has been authenticated; raise an error if it hasn’t.

Raises:



87
88
89
# File 'lib/rscrobbler.rb', line 87

def requires_authentication
  raise AuthenticationError, 'LastFM Authentication Required' unless authenticated?
end