Sha256: f6fa83722de934edf9c832eacf53366f26b5b87e89e3017e5e3be52ddc3acb8e

Contents?: true

Size: 818 Bytes

Versions: 2

Compression:

Stored size: 818 Bytes

Contents

module Notu

  class RecentTracks

    include Enumerable

    attr_reader :user_api

    def initialize(user_api)
      raise ArgumentError.new("#{self.class}#user_api must be specified") unless user_api
      @user_api = user_api
    end

    def each
      return unless block_given?
      pages_count = nil
      page = 1
      loop do
        json = JsonDocument.get(user_api.url(limit: 50, method: 'user.getRecentTracks', page:))
        pages_count = json['recenttracks']['@attr']['totalPages'].to_i
        json['recenttracks']['track'].each do |track_json|
          artist = track_json['artist']['#text'] || next
          title = track_json['name'] || next
          yield(Track.new(artist:, title:))
        end
        page += 1
        break if page > pages_count
      end
      nil
    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
notu-5.0.1 lib/notu/recent_tracks.rb
notu-5.0.0 lib/notu/recent_tracks.rb