Class: LOLastfm::Song

Inherits:
Object
  • Object
show all
Defined in:
lib/LOLastfm/song.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(no_fill = false, data) ⇒ Song

Returns a new instance of Song.



33
34
35
36
37
38
39
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
65
66
67
# File 'lib/LOLastfm/song.rb', line 33

def initialize (no_fill = false, data)
	data = Hash[data.map { |key, value| [key.to_sym, value] }]

	@track       = data[:track] && data[:track].to_i
	@title       = data[:title] && data[:title].strip
	@album       = data[:album] && data[:album].strip
	@artist      = data[:artist] && data[:artist].strip
	@length      = data[:length] && data[:length].to_i
	@comment     = data[:comment]
	@listened_at = data[:listened_at]
	@path        = data[:path]
	@id          = data[:id]

	fill! unless no_fill

	if data[:stream] || (@length && @length < 0)
		stream!

		@length = nil
	end

	if @listened_at
		@listened_at = @listened_at.is_a?(String) ? DateTime.parse(@listened_at) : @listened_at.to_datetime
	else
		if @length
			@listened_at = (Time.now - @length).to_datetime
		else
			@listened_at = DateTime.now
		end
	end

	@title = nil if @title && @title.strip.empty?
	@album = nil if @album && @album.empty?
	@arist = nil if @artist && @artist.empty?
end

Instance Attribute Details

#albumObject

Returns the value of attribute album.



31
32
33
# File 'lib/LOLastfm/song.rb', line 31

def album
  @album
end

#artistObject

Returns the value of attribute artist.



31
32
33
# File 'lib/LOLastfm/song.rb', line 31

def artist
  @artist
end

#commentObject

Returns the value of attribute comment.



31
32
33
# File 'lib/LOLastfm/song.rb', line 31

def comment
  @comment
end

#idObject

Returns the value of attribute id.



31
32
33
# File 'lib/LOLastfm/song.rb', line 31

def id
  @id
end

#lengthObject

Returns the value of attribute length.



31
32
33
# File 'lib/LOLastfm/song.rb', line 31

def length
  @length
end

#listened_atObject

Returns the value of attribute listened_at.



31
32
33
# File 'lib/LOLastfm/song.rb', line 31

def listened_at
  @listened_at
end

#pathObject

Returns the value of attribute path.



31
32
33
# File 'lib/LOLastfm/song.rb', line 31

def path
  @path
end

#titleObject

Returns the value of attribute title.



31
32
33
# File 'lib/LOLastfm/song.rb', line 31

def title
  @title
end

#trackObject

Returns the value of attribute track.



31
32
33
# File 'lib/LOLastfm/song.rb', line 31

def track
  @track
end

Class Method Details

.is_scrobblable?(position, duration) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/LOLastfm/song.rb', line 19

def self.is_scrobblable? (position, duration)
	return false unless position && duration

	return false if duration < 30

	return true if position > 240

	return true if position >= duration / 2

	false
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



98
99
100
101
102
# File 'lib/LOLastfm/song.rb', line 98

def == (other)
	return true if super

	title == other.title && artist == other.artist
end

#fill!Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/LOLastfm/song.rb', line 69

def fill!
	return if !@path || (@track && @title && @album && @artist && @comment && @length && @id)

	TagLib::File.new(@path).tap {|f|
		@track   = f.track && f.track.to_i if f.track && !@track
		@title   = f.title if f.title && !@title
		@album   = f.album if f.album && !@album
		@artist  = f.artist if f.artist && !@artist
		@comment = f.comment if f.comment && !@comment

		begin
			@length = f.length.to_i if f.length && !@length
		rescue TagLib::BadAudioProperties; end

		f.close
	}
end

#hashObject



94
95
96
# File 'lib/LOLastfm/song.rb', line 94

def hash
	[title, artist].hash
end

#inspectObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/LOLastfm/song.rb', line 122

def inspect
	header = ''
	header << "#{id} "                      if id
	header << "listened at #{listened_at} " if listened_at
	header << "#{length} seconds "          if length
	header << "found at #{path} "           if path

	parts = ''
	parts << "track=#{track} "   if track
	parts << "title=#{title} "   if title
	parts << "artist=#{artist} " if artist
	parts << "album=#{album} "   if album

	"#<LOLastfm::Song#{"(#{header[0 .. -2]})" unless header.empty?}:#{" #{parts[0 .. -2]}" if parts}>"
end

#nil?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/LOLastfm/song.rb', line 90

def nil?
	title.nil? || artist.nil?
end

#stream!Object



88
# File 'lib/LOLastfm/song.rb', line 88

def stream!; @stream = true; end

#stream?Boolean

Returns:

  • (Boolean)


87
# File 'lib/LOLastfm/song.rb', line 87

def stream?; !!@stream;      end

#to_hashObject



106
107
108
109
110
111
112
# File 'lib/LOLastfm/song.rb', line 106

def to_hash
	{
		track: track, title: title, album: album, artist: artist, comment: comment,
		length: length, listened_at: listened_at, path: path, id: id,
		stream: stream?
	}
end

#to_jsonObject



114
115
116
# File 'lib/LOLastfm/song.rb', line 114

def to_json
	to_hash.to_json
end

#to_yamlObject



118
119
120
# File 'lib/LOLastfm/song.rb', line 118

def to_yaml
	to_hash.to_yaml
end