notu 4.0.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: efbddd2bb568278da60c542fd1046d39a61d200bf1ca1a20614e7135ac07e66f
4
- data.tar.gz: 4c14831c8b1526a402cf47870e64d17fb5e3ffa4b04d0f2ecaefb30d493de4e3
3
+ metadata.gz: 43f1b5f5623c11b9b3ecf2bb19a64a65681ff6b4414a06eb26967d4655095d27
4
+ data.tar.gz: 4b581b8a75c6211194c3d0564edbd11c88b83fe66623fc1890fe3af7b54e148d
5
5
  SHA512:
6
- metadata.gz: 60c8869371be7e6b80a4b8f8b26ebfa2406f63cb576d7788cf6aeacb6d963c3f4d534455a5ae33bca7ea0a33236a0bb6ab529980fc3697ba302062e764f96d0c
7
- data.tar.gz: 8343035fb3d9a5ce7e9bba05676f13b0265ace6f514e2e492aa5280eeae0ddc25990eb553adf39f698f66767f4da2d5a5d55523e6b04d2fef428cb47129ece3b
6
+ metadata.gz: e6fae0ce54a0bce085f50531e859e9438a90745884271222600559b90dafcd39a5d0cbf270d80f959703f6108d268a1fe314f96d788a40ad818d26984f5c5c98
7
+ data.tar.gz: f2c0d4527a7eb783dcf728fa490d6a89669257ff3b628b9287ba54fd135f6ecf293a6fc2f5ab69e06314d176cd561586ead4938c057e087dbe3edd9e387b7aa4
data/README.mdown CHANGED
@@ -1,6 +1,6 @@
1
1
  # Notu
2
2
 
3
- API to get Last.fm tracks (most played, loved, etc.).
3
+ API to get Last.fm tracks (top, loved, etc.).
4
4
 
5
5
  ## Installation
6
6
 
@@ -15,17 +15,17 @@ Then, just run `bundle install`.
15
15
  ## Example
16
16
 
17
17
  ```ruby
18
- library = Notu::Library.new(username: 'johndoe')
18
+ user_api = Notu::UserApi.new(username: 'johndoe')
19
19
 
20
- library.loved_tracks.each do |track|
20
+ user_api.loved_tracks.each do |track|
21
21
  puts track.artist
22
22
  end
23
23
 
24
- library.most_played_tracks(period: 'last_month').each do |track|
24
+ user_api.top_tracks(period: '3month').each do |track|
25
25
  puts "#{track.artist}: #{track.plays_count}"
26
26
  end
27
27
 
28
- library.played_tracks.each do |track|
28
+ user_api.recent_tracks.each do |track|
29
29
  puts track.title
30
30
  end
31
31
  ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 4.0.0
1
+ 5.0.0
data/lib/notu/api.rb CHANGED
@@ -2,20 +2,20 @@ module Notu
2
2
 
3
3
  class Api
4
4
 
5
- DEFAULT_KEY = '91f5d6a201de58e0c0a0d858573dddf0'.freeze
5
+ DEFAULT_API_KEY = '91f5d6a201de58e0c0a0d858573dddf0'.freeze
6
6
  FORMAT = 'json'.freeze
7
7
  HOST = 'ws.audioscrobbler.com'.freeze
8
8
  VERSION = '2.0'.freeze
9
9
 
10
- attr_reader :key
10
+ attr_reader :api_key
11
11
 
12
- def initialize(key: DEFAULT_KEY)
13
- @key = key.try(:squish).presence || raise(Error.new('API key must be specified'))
12
+ def initialize(api_key: DEFAULT_API_KEY)
13
+ @api_key = api_key.try(:squish).presence || raise(Error.new('API key must be specified'))
14
14
  end
15
15
 
16
16
  def url(params = {})
17
17
  params = (params || {}).symbolize_keys
18
- params.merge!(api_key: key, format: FORMAT)
18
+ params.merge!(api_key:, format: FORMAT)
19
19
  query_string = params.map { |name, value| "#{CGI.escape(name.to_s)}=#{CGI.escape(value.to_s)}" }.join('&')
20
20
  "https://#{HOST}/#{VERSION}?#{query_string}"
21
21
  end
@@ -4,11 +4,11 @@ module Notu
4
4
 
5
5
  include Enumerable
6
6
 
7
- attr_reader :library
7
+ attr_reader :user_api
8
8
 
9
- def initialize(library)
10
- raise ArgumentError.new("#{self.class}#library must be a library, #{library.inspect} given") unless library.is_a?(Library)
11
- @library = library
9
+ def initialize(user_api)
10
+ raise ArgumentError.new("#{self.class}#user_api must be specified") unless user_api
11
+ @user_api = user_api
12
12
  end
13
13
 
14
14
  def each
@@ -16,7 +16,7 @@ module Notu
16
16
  pages_count = nil
17
17
  page = 1
18
18
  loop do
19
- json = JsonDocument.get(library.url(limit: 50, method: 'user.getLovedTracks', page:))
19
+ json = JsonDocument.get(user_api.url(limit: 50, method: 'user.getLovedTracks', page:))
20
20
  pages_count = json['lovedtracks']['@attr']['totalPages'].to_i
21
21
  json['lovedtracks']['track'].each do |track_json|
22
22
  artist = track_json['artist']['name'] || next
@@ -4,11 +4,11 @@ module Notu
4
4
 
5
5
  include Enumerable
6
6
 
7
- attr_reader :library
7
+ attr_reader :user_api
8
8
 
9
- def initialize(library)
10
- raise ArgumentError.new("#{self.class}#library must be a library, #{library.inspect} given") unless library.is_a?(Library)
11
- @library = library
9
+ def initialize(user_api)
10
+ raise ArgumentError.new("#{self.class}#user_api must be specified") unless user_api
11
+ @user_api = user_api
12
12
  end
13
13
 
14
14
  def each
@@ -16,7 +16,7 @@ module Notu
16
16
  pages_count = nil
17
17
  page = 1
18
18
  loop do
19
- json = JsonDocument.get(library.url(limit: 50, method: 'user.getRecentTracks', page:))
19
+ json = JsonDocument.get(user_api.url(limit: 50, method: 'user.getRecentTracks', page:))
20
20
  pages_count = json['recenttracks']['@attr']['totalPages'].to_i
21
21
  json['recenttracks']['track'].each do |track_json|
22
22
  artist = track_json['artist']['#text'] || next
@@ -1,16 +1,16 @@
1
1
  module Notu
2
2
 
3
- class MostPlayedTracks
3
+ class TopTracks
4
4
 
5
5
  include Enumerable
6
6
 
7
7
  PERIODS = %w(overall 7day 1month 3month 6month 12month).freeze
8
8
 
9
- attr_reader :library, :period
9
+ attr_reader :period, :user_api
10
10
 
11
- def initialize(library, options = {})
12
- raise ArgumentError.new("#{self.class}#library must be a library, #{library.inspect} given") unless library.is_a?(Library)
13
- @library = library
11
+ def initialize(user_api, options = {})
12
+ raise ArgumentError.new("#{self.class}#user_api must be specified") unless user_api
13
+ @user_api = user_api
14
14
  options = options.symbolize_keys.reverse_merge(period: PERIODS.first)
15
15
  self.period = options[:period]
16
16
  end
@@ -20,7 +20,7 @@ module Notu
20
20
  pages_count = nil
21
21
  page = 1
22
22
  loop do
23
- json = JsonDocument.get(library.url(limit: 50, method: 'user.getTopTracks', page:))
23
+ json = JsonDocument.get(user_api.url(limit: 50, method: 'user.getTopTracks', page:))
24
24
  pages_count = json['toptracks']['@attr']['totalPages'].to_i
25
25
  json['toptracks']['track'].each do |track_json|
26
26
  artist = track_json['artist']['name'] || next
@@ -38,7 +38,7 @@ module Notu
38
38
 
39
39
  def period=(value)
40
40
  string_value = value.to_s
41
- raise ArgumentError.new("Notu::MostPlayedTracks#period is invalid: #{value.inspect}") unless PERIODS.include?(string_value)
41
+ raise ArgumentError.new("#{self.class.name}#period is invalid: #{value.inspect}") unless PERIODS.include?(string_value)
42
42
  @period = string_value
43
43
  end
44
44
 
@@ -0,0 +1,30 @@
1
+ module Notu
2
+
3
+ class UserApi < Api
4
+
5
+ attr_reader :username
6
+
7
+ def initialize(username:, api_key: DEFAULT_API_KEY)
8
+ super(api_key:)
9
+ @username = username.try(:squish).presence || raise(Error.new('Username must be specified'))
10
+ end
11
+
12
+ def loved_tracks
13
+ LovedTracks.new(self)
14
+ end
15
+
16
+ def recent_tracks
17
+ RecentTracks.new(self)
18
+ end
19
+
20
+ def top_tracks(options = {})
21
+ TopTracks.new(self, options)
22
+ end
23
+
24
+ def url(params = {})
25
+ super((params || {}).symbolize_keys.merge(user: username))
26
+ end
27
+
28
+ end
29
+
30
+ end
data/lib/notu.rb CHANGED
@@ -20,8 +20,8 @@ require "#{lib_path}/parse_error"
20
20
  require "#{lib_path}/api"
21
21
  require "#{lib_path}/http_download"
22
22
  require "#{lib_path}/json_document"
23
- require "#{lib_path}/library"
24
23
  require "#{lib_path}/loved_tracks"
25
- require "#{lib_path}/most_played_tracks"
26
24
  require "#{lib_path}/recent_tracks"
25
+ require "#{lib_path}/top_tracks"
27
26
  require "#{lib_path}/track"
27
+ require "#{lib_path}/user_api"
data/notu.gemspec CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
6
6
  s.email = 'al@alweb.org'
7
7
  s.homepage = 'https://github.com/alexistoulotte/notu'
8
8
  s.summary = 'API for Last.fm'
9
- s.description = 'API to get Last.fm tracks (most played, loved, etc.)'
9
+ s.description = 'API to get Last.fm tracks (top, loved, etc.)'
10
10
  s.license = 'MIT'
11
11
 
12
12
  s.files = %x(git ls-files | grep -vE '^(spec/|test/|\\.|Gemfile|Rakefile)').split("\n")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notu
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 5.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexis Toulotte
@@ -190,7 +190,7 @@ dependencies:
190
190
  - - "<"
191
191
  - !ruby/object:Gem::Version
192
192
  version: 4.0.0
193
- description: API to get Last.fm tracks (most played, loved, etc.)
193
+ description: API to get Last.fm tracks (top, loved, etc.)
194
194
  email: al@alweb.org
195
195
  executables: []
196
196
  extensions: []
@@ -204,13 +204,13 @@ files:
204
204
  - lib/notu/error.rb
205
205
  - lib/notu/http_download.rb
206
206
  - lib/notu/json_document.rb
207
- - lib/notu/library.rb
208
207
  - lib/notu/loved_tracks.rb
209
- - lib/notu/most_played_tracks.rb
210
208
  - lib/notu/network_error.rb
211
209
  - lib/notu/parse_error.rb
212
210
  - lib/notu/recent_tracks.rb
211
+ - lib/notu/top_tracks.rb
213
212
  - lib/notu/track.rb
213
+ - lib/notu/user_api.rb
214
214
  - notu.gemspec
215
215
  homepage: https://github.com/alexistoulotte/notu
216
216
  licenses:
data/lib/notu/library.rb DELETED
@@ -1,30 +0,0 @@
1
- module Notu
2
-
3
- class Library
4
-
5
- attr_reader :api, :username
6
-
7
- def initialize(username:, api: Api.new)
8
- @api = api.presence || raise(Error.new('API must be specified'))
9
- @username = username.try(:squish).presence || raise(Error.new('Username must be specified'))
10
- end
11
-
12
- def loved_tracks
13
- LovedTracks.new(self)
14
- end
15
-
16
- def most_played_tracks(options = {})
17
- MostPlayedTracks.new(self, options)
18
- end
19
-
20
- def recent_tracks
21
- RecentTracks.new(self)
22
- end
23
-
24
- def url(params = {})
25
- api.url((params || {}).symbolize_keys.merge(user: username))
26
- end
27
-
28
- end
29
-
30
- end