Class: Scrobbler::Playing

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Playing) initialize(args = {})



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

- (Object) album

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



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

def album
  @album
end

- (Object) artist

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



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

def artist
  @artist
end

- (Object) length

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



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

def length
  @length
end

- (Object) mb_track_id

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

- (Object) now_playing_url

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

- (Object) session_id

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

- (Object) status (readonly)

Returns the value of attribute status



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

def status
  @status
end

- (Object) track

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



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

def track
  @track
end

- (Object) track_number

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

- (Object) submit!



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