Class: MineTunes
- Inherits:
-
Object
- Object
- MineTunes
- Defined in:
- lib/mine_tunes.rb
Instance Attribute Summary collapse
-
#tracks ⇒ Object
Returns the value of attribute tracks.
Class Method Summary collapse
Instance Method Summary collapse
- #get_tracks(file) ⇒ Object
-
#initialize(file) ⇒ MineTunes
constructor
A new instance of MineTunes.
- #most_played_tracks(number) ⇒ Object
- #most_recently_played_tracks(number) ⇒ Object
- #names_of_artists ⇒ Object
- #number_of_albums ⇒ Object
- #number_of_artists ⇒ Object
- #number_of_tracks ⇒ Object
- #total_play_time ⇒ Object
Constructor Details
#initialize(file) ⇒ MineTunes
Returns a new instance of MineTunes.
15 16 17 18 |
# File 'lib/mine_tunes.rb', line 15 def initialize file @tracks = [] @tracks = get_tracks file end |
Instance Attribute Details
#tracks ⇒ Object
Returns the value of attribute tracks.
13 14 15 |
# File 'lib/mine_tunes.rb', line 13 def tracks @tracks end |
Class Method Details
.run(file) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mine_tunes.rb', line 20 def self.run file mine = MineTunes.new file most_played_tracks = mine.most_played_tracks 10 most_recently_played_tracks = mine.most_recently_played_tracks 10 puts "STATS" puts "#{mine.number_of_tracks} tracks" puts "#{mine.number_of_artists} artists" puts "#{mine.number_of_albums} albums" puts "#{mine.total_play_time}ms of play time" puts puts "MOST PLAYED TRACKS" puts most_played_tracks puts puts "MOST RECENTLY PLAYED TRACKS" puts most_recently_played_tracks end |
Instance Method Details
#get_tracks(file) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/mine_tunes.rb', line 39 def get_tracks file doc = Nokogiri::XML(open("#{file}")) doc.xpath('//dict/dict/dict').each do |track| = {} track.xpath('key').each do |item| key = item.inner_text.downcase.gsub(" ", "_").to_sym if item.next.name == "date" value = Time.parse(item.next.inner_text) else value = item.next.inner_text end [key] = value end @tracks << Track.new() end return @tracks end |
#most_played_tracks(number) ⇒ Object
78 79 80 |
# File 'lib/mine_tunes.rb', line 78 def most_played_tracks(number) @tracks.sort_by{ |track| -track.play_count.to_i }.first(number).map{ |track| "[#{track.play_count}] #{track.artist} - #{track.name}" } end |
#most_recently_played_tracks(number) ⇒ Object
82 83 84 |
# File 'lib/mine_tunes.rb', line 82 def most_recently_played_tracks(number) @tracks.sort_by{ |track| -track.play_date_utc.to_i }.first(number).map{ |track| "[#{track.play_date_utc}] #{track.artist} - #{track.name}" } end |
#names_of_artists ⇒ Object
62 63 64 |
# File 'lib/mine_tunes.rb', line 62 def names_of_artists @tracks.map{ |track| track.artist }.uniq end |
#number_of_albums ⇒ Object
70 71 72 |
# File 'lib/mine_tunes.rb', line 70 def number_of_albums @tracks.map{ |track| track.album }.uniq.size end |
#number_of_artists ⇒ Object
66 67 68 |
# File 'lib/mine_tunes.rb', line 66 def number_of_artists @tracks.map{ |track| track.artist }.uniq.size end |
#number_of_tracks ⇒ Object
58 59 60 |
# File 'lib/mine_tunes.rb', line 58 def number_of_tracks @tracks.size end |
#total_play_time ⇒ Object
74 75 76 |
# File 'lib/mine_tunes.rb', line 74 def total_play_time @tracks.map{ |track| track.total_time.to_i }.inject(0){ |sum, time| sum + time } end |