Sha256: 69963ef1513c57e18203a671056488a2b0db6b633d04c99ce583ca51e1a74e6e

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

module Notu

  class TopTracks

    include Enumerable

    PERIODS = %w(overall 7day 1month 3month 6month 12month).freeze

    attr_reader :period, :user_api

    def initialize(user_api, options = {})
      raise ArgumentError.new("#{self.class}#user_api must be specified") unless user_api
      @user_api = user_api
      options = options.symbolize_keys.reverse_merge(period: PERIODS.first)
      self.period = options[:period]
    end

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

    private

    def period=(value)
      string_value = value.to_s
      raise ArgumentError.new("#{self.class.name}#period is invalid: #{value.inspect}") unless PERIODS.include?(string_value)
      @period = string_value
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
notu-5.0.0 lib/notu/top_tracks.rb