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
-
.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.
47 48 49 |
# File 'lib/rscrobbler.rb', line 47 def api_key @api_key end |
.api_secret ⇒ Object
Returns the value of attribute api_secret.
47 48 49 |
# File 'lib/rscrobbler.rb', line 47 def api_secret @api_secret end |
.auth_token ⇒ Object
Returns the value of attribute auth_token.
47 48 49 |
# File 'lib/rscrobbler.rb', line 47 def auth_token @auth_token end |
.logger ⇒ Object
Returns the value of attribute logger.
47 48 49 |
# File 'lib/rscrobbler.rb', line 47 def logger @logger end |
.session_key ⇒ Object
Returns the value of attribute session_key.
47 48 49 |
# File 'lib/rscrobbler.rb', line 47 def session_key @session_key end |
.username ⇒ Object
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.
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?
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.
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.
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.
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.
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_authentication ⇒ Object
Ensure the service has been authenticated; raise an error if it hasn’t.
91 92 93 |
# File 'lib/rscrobbler.rb', line 91 def requires_authentication raise AuthenticationError, 'LastFM Authentication Required' unless authenticated? end |