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.0'
- HOST =
'ws.audioscrobbler.com'
- API_VERSION =
'2.0'
Class Attribute Summary collapse
-
.api_key ⇒ Object
Returns the value of attribute api_key.
-
.api_secret ⇒ Object
Returns the value of attribute api_secret.
-
.auth_token ⇒ Object
Returns the value of attribute auth_token.
-
.logger ⇒ Object
Returns the value of attribute logger.
-
.session_key ⇒ Object
Returns the value of attribute session_key.
-
.username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
-
.authenticate! ⇒ String
Authenticate the service with provided login credentials.
-
.authenticated? ⇒ Boolean
Has the service been authenticated?.
-
.establish_session {|_self| ... } ⇒ String
Configure the module and begin a session.
-
.generate_auth_token(password) ⇒ String
Generate auth token from username and given password.
-
.get(method, params = {}, secure = false) ⇒ LibXML::XML::Document
Construct an HTTP GET call from params, and load the response into a LibXML Document.
-
.post(method, params) ⇒ LibXML::XML::Document
Construct an HTTP POST call from params, and check the response status.
-
.requires_authentication ⇒ Object
Ensure the service has been authenticated; raise an error if it hasn’t.
Class Attribute Details
.api_key ⇒ Object
Returns the value of attribute api_key.
43 44 45 |
# File 'lib/rscrobbler.rb', line 43 def api_key @api_key end |
.api_secret ⇒ Object
Returns the value of attribute api_secret.
43 44 45 |
# File 'lib/rscrobbler.rb', line 43 def api_secret @api_secret end |
.auth_token ⇒ Object
Returns the value of attribute auth_token.
43 44 45 |
# File 'lib/rscrobbler.rb', line 43 def auth_token @auth_token end |
.logger ⇒ Object
Returns the value of attribute logger.
43 44 45 |
# File 'lib/rscrobbler.rb', line 43 def logger @logger end |
.session_key ⇒ Object
Returns the value of attribute session_key.
43 44 45 |
# File 'lib/rscrobbler.rb', line 43 def session_key @session_key end |
.username ⇒ Object
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.
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?
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.
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.
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.
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.
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_authentication ⇒ Object
Ensure the service has been authenticated; raise an error if it hasn’t.
87 88 89 |
# File 'lib/rscrobbler.rb', line 87 def requires_authentication raise AuthenticationError, 'LastFM Authentication Required' unless authenticated? end |