Class: Notu::MostPlayedTracks

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/notu/most_played_tracks.rb

Constant Summary collapse

PERIODS =
{
  'last_week' => 'week',
  'last_month' => '1month',
  'last_3_months' => '3month',
  'last_6_months' => '6month',
  'last_year' => 'year',
  'overall' => 'overall',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(library, options = {}) ⇒ MostPlayedTracks

Returns a new instance of MostPlayedTracks.

Raises:

  • (ArgumentError)


18
19
20
21
22
23
# File 'lib/notu/most_played_tracks.rb', line 18

def initialize(library, options = {})
  raise ArgumentError.new("#{self.class}#library must be a library, #{library.inspect} given") unless library.is_a?(Library)
  @library = library
  options = options.stringify_keys.reverse_merge('period' => PERIODS.keys.first)
  self.period = options['period']
end

Instance Attribute Details

#libraryObject (readonly)

Returns the value of attribute library.



16
17
18
# File 'lib/notu/most_played_tracks.rb', line 16

def library
  @library
end

#periodObject

Returns the value of attribute period.



16
17
18
# File 'lib/notu/most_played_tracks.rb', line 16

def period
  @period
end

Instance Method Details

#each(&block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/notu/most_played_tracks.rb', line 25

def each(&block)
  return unless block_given?
  document = HtmlDocument.get(library.url(path: 'charts', query: { 'rangetype' => PERIODS[period], 'subtype' => 'tracks' }))
  (document/'table.chart tbody tr').each do |element|
    artist = (element/'td.subjectCell a').first.text
    plays_count = (element/'td.chartbarCell a span').text.strip
    title = (element/'td.subjectCell a').last.text
    yield(Track.new(artist: artist, plays_count: plays_count, title: title))
  end
  nil
end