Sha256: 647f76cdb3be6d00bd3fe6a13ffcb4e35bca4dc680fe8beb1be3504ce4185f79

Contents?: true

Size: 1021 Bytes

Versions: 6

Compression:

Stored size: 1021 Bytes

Contents

module Notu

  class LovedTracks

    include Enumerable

    attr_reader :library

    def initialize(library)
      raise ArgumentError.new("#{self.class}#library must be a library, #{library.inspect} given") unless library.is_a?(Library)
      @library = library
    end

    def each(&block)
      return unless block_given?
      page_urls.each do |url|
        document = HtmlDocument.get(url)
        (document/'#lovedTracks td.subjectCell').each do |element|
          yield(Track.new(artist: (element/'a').first.text, title: (element/'a').last.text))
        end
      end
      nil
    end

    private

    def page_urls
      @loved_pages ||= (1..pages_count).map do |index|
        library.url(path: 'library/loved', query: { 'sortBy' => 'date', 'sortOrder' => 'desc', 'page' => index })
      end
    end

    def pages_count
      document = HtmlDocument.get(library.url(path: 'library/loved'))
      [1, (document/'div.whittle-pagination a').map { |link| link.text.to_i }].flatten.max
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
notu-0.1.5 lib/notu/loved_tracks.rb
notu-0.1.4 lib/notu/loved_tracks.rb
notu-0.1.3 lib/notu/loved_tracks.rb
notu-0.1.2 lib/notu/loved_tracks.rb
notu-0.1.1 lib/notu/loved_tracks.rb
notu-0.1.0 lib/notu/loved_tracks.rb