Class Scrobbler::Scrobble
In: lib/scrobbler/scrobble.rb
Parent: Object

Methods

new   submit!  

Attributes

album  [RW]  you need to read last.fm/api/submissions#subs first!
artist  [RW]  you need to read last.fm/api/submissions#subs first!
length  [RW]  you need to read last.fm/api/submissions#subs first!
mb_track_id  [RW]  you need to read last.fm/api/submissions#subs first!
session_id  [RW]  you need to read last.fm/api/submissions#subs first!
source  [RW]  you need to read last.fm/api/submissions#subs first!
status  [R] 
submission_url  [RW]  you need to read last.fm/api/submissions#subs first!
time  [RW]  you need to read last.fm/api/submissions#subs first!
track  [RW]  you need to read last.fm/api/submissions#subs first!
track_number  [RW]  you need to read last.fm/api/submissions#subs first!

Public Class methods

[Source]

    # File lib/scrobbler/scrobble.rb, line 13
13:     def initialize(args = {})
14:       @session_id = args[:session_id] # from Scrobbler::SimpleAuth
15:       @submission_url = args[:submission_url] # from Scrobbler::SimpleAuth (can change)
16:       @artist = args[:artist] # track artist
17:       @track = args[:track] # track name
18:       @time = args[:time] # a Time object set to the time the track started playing
19:       @source = args[:source] || 'P' # track source, see last.fm/api/submissions#subs
20:       @length = args[:length].to_s || '' # track length in seconds
21:       @album = args[:album] || '' # track album name (optional)
22:       @track_number = args[:track_number] || '' # track number (optional)
23:       @mb_track_id = args[:mb_track_id] || '' # MusicBrainz track ID (optional)
24: 
25:       if [@session_id, @submission_url, @artist, @track].any?(&:empty?)
26:         raise ArgumentError, 'Missing required argument'
27:       elsif @time.class.to_s != 'Time'
28:         raise ArgumentError, ":time must be a Time object"
29:       elsif !['P','R','E','U'].include?(@source) # see last.fm/api/submissions#subs
30:         raise ArgumentError, "Invalid source"
31:       elsif @source == 'P' && @length.empty? # length is not optional if source is P
32:         raise ArgumentError, 'Length must be set'
33:       elsif !@length.empty? && @length.to_i <= 30 # see last.fm/api/submissions#subs
34:         raise ArgumentError, 'Length must be greater than 30 seconds'
35:       end
36: 
37:       @connection = REST::Connection.new(@submission_url)
38:     end

Public Instance methods

[Source]

    # File lib/scrobbler/scrobble.rb, line 40
40:     def submit!
41:       query = { :s => @session_id,
42:                 'a[0]' => @artist,
43:                 't[0]' => @track,
44:                 'i[0]' => @time.utc.to_i,
45:                 'o[0]' => @source,
46:                 'r[0]' => @rating,
47:                 'l[0]' => @length,
48:                 'b[0]' => @album,
49:                 'n[0]' => @track_number,
50:                 'm[0]' => @mb_track_id }
51:                 
52:       @status = @connection.post('', query)
53: 
54:       case @status
55:       when /OK/
56: 
57:       when /BADSESSION/
58:         raise BadSessionError # rerun Scrobbler::SimpleAuth#handshake!
59:       when /FAILED/
60:         raise RequestFailedError, @status
61:       else
62:         raise RequestFailedError
63:       end
64:     end

[Validate]