Module: LastFM

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

Defined Under Namespace

Modules: Api Classes: Album, Artist, AuthenticationError, Buylink, Event, LastFMError, Shout, Struct, Tag, Track, Venue, Wiki

Constant Summary collapse

VERSION =
'0.2.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.



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

def api_key
  @api_key
end

.api_secretObject

Returns the value of attribute api_secret.



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

def api_secret
  @api_secret
end

.auth_tokenObject

Returns the value of attribute auth_token.



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

def auth_token
  @auth_token
end

.loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

.session_keyObject

Returns the value of attribute session_key.



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

def session_key
  @session_key
end

.usernameObject

Returns the value of attribute username.



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

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:



74
75
76
77
78
79
# File 'lib/rscrobbler.rb', line 74

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 = Api::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



84
85
86
# File 'lib/rscrobbler.rb', line 84

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:



64
65
66
67
# File 'lib/rscrobbler.rb', line 64

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



99
100
101
# File 'lib/rscrobbler.rb', line 99

def generate_auth_token( password )
  self.auth_token = Digest::MD5.hexdigest( username.dup << 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:



111
112
113
114
115
116
# File 'lib/rscrobbler.rb', line 111

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:



124
125
126
127
128
129
130
# File 'lib/rscrobbler.rb', line 124

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:



91
92
93
# File 'lib/rscrobbler.rb', line 91

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