Class: Notu::TopTracks
- Inherits:
-
Object
- Object
- Notu::TopTracks
- Includes:
- Enumerable
- Defined in:
- lib/notu/top_tracks.rb
Constant Summary collapse
- PERIODS =
%w(overall 7day 1month 3month 6month 12month).freeze
Instance Attribute Summary collapse
-
#period ⇒ Object
readonly
Returns the value of attribute period.
-
#user_api ⇒ Object
readonly
Returns the value of attribute user_api.
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(user_api, options = {}) ⇒ TopTracks
constructor
A new instance of TopTracks.
Constructor Details
#initialize(user_api, options = {}) ⇒ TopTracks
Returns a new instance of TopTracks.
11 12 13 14 15 16 |
# File 'lib/notu/top_tracks.rb', line 11 def initialize(user_api, = {}) raise ArgumentError.new("#{self.class}#user_api must be specified") unless user_api @user_api = user_api = .symbolize_keys.reverse_merge(period: PERIODS.first) self.period = [:period] end |
Instance Attribute Details
#period ⇒ Object
Returns the value of attribute period.
9 10 11 |
# File 'lib/notu/top_tracks.rb', line 9 def period @period end |
#user_api ⇒ Object (readonly)
Returns the value of attribute user_api.
9 10 11 |
# File 'lib/notu/top_tracks.rb', line 9 def user_api @user_api end |
Instance Method Details
#each ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/notu/top_tracks.rb', line 18 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 |