Class: Scrobbler::Scrobble

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Scrobble

Returns a new instance of Scrobble.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/scrobbler/scrobble.rb', line 13

def initialize(args = {})
  @session_id = args[:session_id] # from Scrobbler::SimpleAuth
  @submission_url = args[:submission_url] # from Scrobbler::SimpleAuth (can change)
  @artist = args[:artist] # track artist
  @track = args[:track] # track name
  @time = args[:time] # a Time object set to the time the track started playing
  @source = args[:source] || 'P' # track source, see last.fm/api/submissions#subs
  @length = args[:length].to_s || '' # track length in seconds
  @album = args[:album] || '' # track album name (optional)
  @track_number = args[:track_number] || '' # track number (optional)
  @mb_track_id = args[:mb_track_id] || '' # MusicBrainz track ID (optional)

  if [@session_id, @submission_url, @artist, @track].any?(&:blank?)
    raise ArgumentError, 'Missing required argument'
  elsif @time.class.to_s != 'Time'
    raise ArgumentError, ":time must be a Time object"
  elsif !['P','R','E','U'].include?(@source) # see last.fm/api/submissions#subs
    raise ArgumentError, "Invalid source"
  elsif @source == 'P' && @length.blank? # length is not optional if source is P
    raise ArgumentError, 'Length must be set'
  elsif !@length.blank? && @length.to_i <= 30 # see last.fm/api/submissions#subs
    raise ArgumentError, 'Length must be greater than 30 seconds'
  end

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

Instance Attribute Details

#albumObject

you need to read last.fm/api/submissions#subs first!



9
10
11
# File 'lib/scrobbler/scrobble.rb', line 9

def album
  @album
end

#artistObject

you need to read last.fm/api/submissions#subs first!



9
10
11
# File 'lib/scrobbler/scrobble.rb', line 9

def artist
  @artist
end

#lengthObject

you need to read last.fm/api/submissions#subs first!



9
10
11
# File 'lib/scrobbler/scrobble.rb', line 9

def length
  @length
end

#mb_track_idObject

you need to read last.fm/api/submissions#subs first!



9
10
11
# File 'lib/scrobbler/scrobble.rb', line 9

def mb_track_id
  @mb_track_id
end

#session_idObject

you need to read last.fm/api/submissions#subs first!



9
10
11
# File 'lib/scrobbler/scrobble.rb', line 9

def session_id
  @session_id
end

#sourceObject

you need to read last.fm/api/submissions#subs first!



9
10
11
# File 'lib/scrobbler/scrobble.rb', line 9

def source
  @source
end

#statusObject (readonly)

Returns the value of attribute status.



11
12
13
# File 'lib/scrobbler/scrobble.rb', line 11

def status
  @status
end

#submission_urlObject

you need to read last.fm/api/submissions#subs first!



9
10
11
# File 'lib/scrobbler/scrobble.rb', line 9

def submission_url
  @submission_url
end

#timeObject

you need to read last.fm/api/submissions#subs first!



9
10
11
# File 'lib/scrobbler/scrobble.rb', line 9

def time
  @time
end

#trackObject

you need to read last.fm/api/submissions#subs first!



9
10
11
# File 'lib/scrobbler/scrobble.rb', line 9

def track
  @track
end

#track_numberObject

you need to read last.fm/api/submissions#subs first!



9
10
11
# File 'lib/scrobbler/scrobble.rb', line 9

def track_number
  @track_number
end

Instance Method Details

#submit!Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/scrobbler/scrobble.rb', line 40

def submit!
  query = { :s => @session_id,
            'a[0]' => @artist,
            't[0]' => @track,
            'i[0]' => @time.utc.to_i,
            'o[0]' => @source,
            'r[0]' => '',
            'l[0]' => @length,
            'b[0]' => @album,
            'n[0]' => @track_number,
            'm[0]' => @mb_track_id }
            
  @status = @connection.post('', query)

  case @status
  when /OK/

  when /BADSESSION/
    raise BadSessionError # rerun Scrobbler::SimpleAuth#handshake!
  when /FAILED/
    raise RequestFailedError, @status
  else
    raise RequestFailedError
  end
end