itunes_api 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/itunes_api/music/album.rb +26 -19
- data/lib/itunes_api/music/all.rb +1 -1
- data/lib/itunes_api/music/artist.rb +33 -11
- data/lib/itunes_api/request.rb +23 -2
- data/lib/itunes_api/version.rb +1 -1
- data/lib/itunes_api.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f093140def0544a9f01d175bfbc950e63626f6b0
|
4
|
+
data.tar.gz: f11e580bc9922f4bce4b5ffdf7446c374e87872d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 616de967cf9baf0707117f9c617204e917dd7c4aa59bed13bef8d15d965e70ad79ccacc96eeaa926e516b76a1fe25a37d6bed82d62da7339d205fb7b35a4763b
|
7
|
+
data.tar.gz: 0f903eb6650fb2eac0509d4eaebfaff58b62fb291cb325d696f9f74d3da7e531ce92b4cdd7083e2982718f6ef74531343066200d0d126f0d8cd74561ffa70d4b
|
@@ -1,39 +1,46 @@
|
|
1
1
|
module ItunesApi
|
2
2
|
module Music
|
3
|
-
#
|
3
|
+
# Retrieves album tracks info.
|
4
4
|
class Album
|
5
|
+
attr_accessor :name
|
5
6
|
include Request
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
@artists_id = artists_id
|
8
|
+
def initialize(name, filtering_options = {})
|
9
|
+
@name = name
|
10
|
+
@filtering_options = filtering_options
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
14
|
-
|
13
|
+
def tracks
|
14
|
+
@tracks ||= filtered_results.sort_by { |track| track['trackNumber'] }
|
15
15
|
end
|
16
16
|
|
17
|
-
def
|
18
|
-
|
19
|
-
.find_all { |r| r['collectionType'] == TYPE }
|
20
|
-
.sort_by { |a| a['releaseDate'] }
|
21
|
-
.reverse
|
17
|
+
def apple_music?
|
18
|
+
tracks.any? { |track| track['isStreamable'] }
|
22
19
|
end
|
23
20
|
|
24
21
|
private
|
25
22
|
|
26
|
-
def
|
27
|
-
|
28
|
-
|
23
|
+
def album_id
|
24
|
+
@album_id ||= @filtering_options[:album_id]
|
25
|
+
end
|
26
|
+
|
27
|
+
def artist_id
|
28
|
+
@artist_id ||= @filtering_options[:artist_id]
|
29
29
|
end
|
30
30
|
|
31
|
-
def
|
32
|
-
@
|
31
|
+
def filtered_results
|
32
|
+
@filtered_results ||= results.find_all do |result|
|
33
|
+
(!album_id || result['collectionId'] == album_id) &&
|
34
|
+
(!artist_id || result['artistId'] == artist_id)
|
35
|
+
end
|
33
36
|
end
|
34
37
|
|
35
|
-
def
|
36
|
-
|
38
|
+
def query_values
|
39
|
+
{
|
40
|
+
attribute: 'albumTerm',
|
41
|
+
entity: 'musicTrack',
|
42
|
+
term: name
|
43
|
+
}.merge(super)
|
37
44
|
end
|
38
45
|
end
|
39
46
|
end
|
data/lib/itunes_api/music/all.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
1
|
require_relative 'album'
|
2
|
-
require_relative 'artist'
|
2
|
+
require_relative 'artist'
|
@@ -2,32 +2,54 @@ require 'addressable/uri'
|
|
2
2
|
|
3
3
|
module ItunesApi
|
4
4
|
module Music
|
5
|
-
#
|
5
|
+
# Retrieve apple ids and albums of artists.
|
6
6
|
class Artist
|
7
7
|
include Request
|
8
8
|
|
9
|
-
def initialize(artist_name)
|
9
|
+
def initialize(artist_name, apple_id = nil)
|
10
10
|
@artist_name = artist_name
|
11
|
+
@apple_id = apple_id
|
11
12
|
end
|
12
13
|
|
13
|
-
def
|
14
|
-
|
14
|
+
def albums(limit = 3)
|
15
|
+
@albums ||= filtered_results
|
16
|
+
.sort_by { |result| result['releaseDate'] }
|
17
|
+
.reverse
|
18
|
+
.first(limit)
|
19
|
+
.map { |result| album(result) }
|
15
20
|
end
|
16
21
|
|
17
22
|
def apple_ids
|
18
|
-
|
23
|
+
@apple_ids ||= filtered_results.collect do |album|
|
24
|
+
album['artistId']
|
25
|
+
end.compact.uniq.sort
|
19
26
|
end
|
20
27
|
|
21
28
|
private
|
22
29
|
|
23
|
-
def
|
24
|
-
|
30
|
+
def filtered_results
|
31
|
+
results.find_all do |result|
|
32
|
+
result['collectionType'] == 'Album' &&
|
33
|
+
(!@apple_id || result['artistId'] == @apple_id)
|
34
|
+
end
|
25
35
|
end
|
26
36
|
|
27
|
-
def
|
28
|
-
|
29
|
-
|
30
|
-
|
37
|
+
def album(result)
|
38
|
+
Album.new(
|
39
|
+
result['collectionName'],
|
40
|
+
{
|
41
|
+
artist_id: @apple_id,
|
42
|
+
album_id: result['collectionId']
|
43
|
+
}
|
44
|
+
)
|
45
|
+
end
|
46
|
+
|
47
|
+
def query_values
|
48
|
+
{
|
49
|
+
attribute: 'artistTerm',
|
50
|
+
entity: 'album',
|
51
|
+
term: @artist_name
|
52
|
+
}.merge(super)
|
31
53
|
end
|
32
54
|
end
|
33
55
|
end
|
data/lib/itunes_api/request.rb
CHANGED
@@ -4,16 +4,37 @@ require 'json'
|
|
4
4
|
module ItunesApi
|
5
5
|
# Allow requests to the iTunes API.
|
6
6
|
module Request
|
7
|
+
|
8
|
+
def response
|
9
|
+
JSON.parse(request)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
7
14
|
def request
|
8
15
|
@request ||= open(url).read
|
9
16
|
end
|
10
17
|
|
11
|
-
def
|
12
|
-
|
18
|
+
def query
|
19
|
+
Addressable::URI.new(
|
20
|
+
query_values: query_values
|
21
|
+
).query
|
22
|
+
end
|
23
|
+
|
24
|
+
def query_values
|
25
|
+
{
|
26
|
+
country: COUNTRY_CODE,
|
27
|
+
limit: LIMIT,
|
28
|
+
media: 'music'
|
29
|
+
}
|
13
30
|
end
|
14
31
|
|
15
32
|
def results
|
16
33
|
response.fetch('results', [])
|
17
34
|
end
|
35
|
+
|
36
|
+
def url
|
37
|
+
"#{BASE_URL}/search?#{query}"
|
38
|
+
end
|
18
39
|
end
|
19
40
|
end
|
data/lib/itunes_api/version.rb
CHANGED
data/lib/itunes_api.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: itunes_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mario D’Arco
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-11-
|
11
|
+
date: 2016-11-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: addressable
|