Class: Scrobbler::Playing

Inherits:
Object
  • Object
show all
Defined in:
lib/scrobbler/playing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Playing

Returns a new instance of Playing.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/scrobbler/playing.rb', line 9

def initialize(args = {})
  @session_id = args[:session_id] # from Scrobbler::SimpleAuth
  @now_playing_url = args[:now_playing_url] # from Scrobbler::SimpleAuth (can change)
  @artist = args[:artist] # track artist
  @track = args[:track] # track name
  @album = args[:album] || '' # track album (optional)
  @length = args[:length] || '' # track length in seconds (optional)
  @track_number = args[:track_number] || '' # track number (optional)
  @mb_track_id = args[:mb_track_id] || '' # MusicBrainz track ID (optional)

  if [@session_id, @now_playing_url, @artist, @track].any?(&:blank?)
    raise ArgumentError, 'Missing required argument'
  elsif !@length.to_s.empty? && @length.to_i <= 30 # see last.fm/api
    raise ArgumentError, 'Length must be greater than 30 seconds'
  end 

  @connection = REST::Connection.new(@now_playing_url)
end

Instance Attribute Details

#albumObject

you should read last.fm/api/submissions#np first!



5
6
7
# File 'lib/scrobbler/playing.rb', line 5

def album
  @album
end

#artistObject

you should read last.fm/api/submissions#np first!



5
6
7
# File 'lib/scrobbler/playing.rb', line 5

def artist
  @artist
end

#lengthObject

you should read last.fm/api/submissions#np first!



5
6
7
# File 'lib/scrobbler/playing.rb', line 5

def length
  @length
end

#mb_track_idObject

you should read last.fm/api/submissions#np first!



5
6
7
# File 'lib/scrobbler/playing.rb', line 5

def mb_track_id
  @mb_track_id
end

#now_playing_urlObject

you should read last.fm/api/submissions#np first!



5
6
7
# File 'lib/scrobbler/playing.rb', line 5

def now_playing_url
  @now_playing_url
end

#session_idObject

you should read last.fm/api/submissions#np first!



5
6
7
# File 'lib/scrobbler/playing.rb', line 5

def session_id
  @session_id
end

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/scrobbler/playing.rb', line 7

def status
  @status
end

#trackObject

you should read last.fm/api/submissions#np first!



5
6
7
# File 'lib/scrobbler/playing.rb', line 5

def track
  @track
end

#track_numberObject

you should read last.fm/api/submissions#np first!



5
6
7
# File 'lib/scrobbler/playing.rb', line 5

def track_number
  @track_number
end

Instance Method Details

#submit!Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/scrobbler/playing.rb', line 28

def submit!
  query = { :s => @session_id,
            :a => @artist,
            :t => @track,
            :b => @album,
            :l => @length,
            :n => @track_number,
            :m => @mb_track_id }

  @status = @connection.post('', query)

  case @status
  when /OK/

  when /BADSESSION/
    raise BadSessionError # rerun Scrobbler::SimpleAuth#handshake!
  else
    raise RequestFailedError
  end
end