Class: ItunesTrack
- Inherits:
-
Object
- Object
- ItunesTrack
- Defined in:
- lib/itunes_track.rb,
lib/itunes_track/cli.rb,
lib/itunes_track/version.rb
Defined Under Namespace
Constant Summary collapse
- ATTRS =
%i(name time artist album genre rating played_count year composer track_count track_number disc_count disc_number lyrics)
- VERSION =
"0.0.1"
Class Method Summary collapse
- .build(*attrs, &blk) ⇒ Object
- .full_build(&blk) ⇒ Object
- .itunes ⇒ Object
- .itunes_tracks(&blk) ⇒ Object
- .size(&blk) ⇒ Object
- .to_csv(path, attrs = @attrs) ⇒ Object
- .track_properties ⇒ Object
- .tracks ⇒ Object
Class Method Details
.build(*attrs, &blk) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/itunes_track.rb', line 18 def build(*attrs, &blk) tracks.clear @attrs = attrs.empty? ? ATTRS : attrs track_attrs = [] itunes_tracks(&blk).each do |track| track_attrs << begin @attrs.inject({}) do |h, attr| val = track.send(attr).get rescue '' if val.is_a?(String) && val.encoding.to_s!='UTF-8' val = val.force_encoding('UTF-8') end h[attr.intern] = val h end end end tracks.push *track_attrs.map { |attrs| Track.new attrs } end |
.full_build(&blk) ⇒ Object
37 38 39 |
# File 'lib/itunes_track.rb', line 37 def full_build(&blk) build(*track_properties, &blk) end |
.itunes ⇒ Object
45 46 47 |
# File 'lib/itunes_track.rb', line 45 def itunes @itunes ||= app('iTunes').tracks end |
.itunes_tracks(&blk) ⇒ Object
49 50 51 |
# File 'lib/itunes_track.rb', line 49 def itunes_tracks(&blk) block_given? ? itunes.get.select(&blk) : itunes.get end |
.size(&blk) ⇒ Object
53 54 55 |
# File 'lib/itunes_track.rb', line 53 def size(&blk) itunes_tracks(&blk).size end |
.to_csv(path, attrs = @attrs) ⇒ Object
61 62 63 64 65 66 67 68 69 |
# File 'lib/itunes_track.rb', line 61 def to_csv(path, attrs=@attrs) raise 'Should be built first' if tracks.empty? CSV.open(path, 'wb') do |csv| csv << attrs tracks.each { |t| csv << attrs.map{ |attr| t.send attr } } end rescue => e puts "Something go wrong during csv building: #{e}" end |
.track_properties ⇒ Object
57 58 59 |
# File 'lib/itunes_track.rb', line 57 def track_properties itunes.properties.map(&:intern) end |
.tracks ⇒ Object
41 42 43 |
# File 'lib/itunes_track.rb', line 41 def tracks @tracks ||= [] end |