Class: Imine

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

Constant Summary collapse

VERSION =
'0.1.3'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, counter = 0) ⇒ Imine

Returns a new instance of Imine.



18
19
20
# File 'lib/imine.rb', line 18

def initialize(file, counter = 0)
  @tracks = get_tracks(file, counter)
end

Instance Attribute Details

#tracksObject

Returns the value of attribute tracks.



12
13
14
# File 'lib/imine.rb', line 12

def tracks
  @tracks
end

Class Method Details

.run(args) ⇒ Object



14
15
16
# File 'lib/imine.rb', line 14

def self.run(args)
  miner = Imine.new(args.first)
end

Instance Method Details

#count_by_field(name) ⇒ Object



64
65
66
# File 'lib/imine.rb', line 64

def count_by_field (name)
  @tracks.map {|track| track.send(name).downcase}.uniq.size
end

#get_stars(rate, format = :integer) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/imine.rb', line 104

def get_stars(rate, format = :integer)
  rating = rate.to_i / 20
  case format
  when :integer
    return rating
  when :visual
    return "*" * rating
  end
end

#get_tracks(file, counter) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
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
# File 'lib/imine.rb', line 22

def get_tracks(file, counter)
  doc = Nokogiri::XML(File.read(file))
  tracks = []
  count = 0
  ecount = 0
  elipses = "   "
  doc.xpath('//dict/dict/dict').each do |d|
    info = {}
    # Begin superfluous counting code
    case counter
    when 1
      if count % 200 == 0
        if ecount == 4
          ecount = 0
          elipses = "   "
        end
      end
      count += 1
      print "\rProcessing#{elipses} " + bold("#{count}")
      if count % 200 == 0
        elipses[ecount] = "." unless ecount > 2
        ecount += 1
      end
      STDOUT.flush
    else
    end
    # End superfluous counting code
    d.xpath('key').each do |item|
      key = item.inner_text.downcase.gsub(" ", "_").to_sym
      case item.next.name
      when "integer"  ; value = item.next.inner_text.to_i
      when "date"     ; value = Time.parse(item.next.inner_text)
      else            ; value = item.next.inner_text
      end
      info[key] = value
    end
    tracks << Track.new(info)
  end
  puts negative("\r     DONE     ") + "#{" " * 30}\n\n\n" if counter == 1
  return tracks
end

#most_played(n = 10) ⇒ Object



80
81
82
83
84
# File 'lib/imine.rb', line 80

def most_played(n = 10)
  return @tracks.sort_by {|track| -track.play_count.to_i}.map do 
    |track| [track.name, track.play_count, track.rating.to_i, track.artist]
  end.first(n)
end

#number_of_albumsObject



68
69
70
# File 'lib/imine.rb', line 68

def number_of_albums
  count_by_field :album
end

#number_of_artistsObject



72
73
74
# File 'lib/imine.rb', line 72

def number_of_artists
  count_by_field :artist
end

#number_of_tracksObject



76
77
78
# File 'lib/imine.rb', line 76

def number_of_tracks
  @tracks.size
end

#top_yearObject



95
96
97
98
99
100
101
102
# File 'lib/imine.rb', line 95

def top_year
  years = Hash.new 0
  @tracks.each do |track|
    years[track.year] += 1
  end
  years.delete("") unless years.size == 1
  return years.sort_by {|year, count| -count}.first.first
end

#total_playtimeObject



86
87
88
89
90
91
92
93
# File 'lib/imine.rb', line 86

def total_playtime
  playtime = 0
  @tracks.each do |track|
    next if track.total_time == ""
    playtime += track.total_time
  end
  return playtime
end