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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1dac75a6735f947bd3e7095a6a7fe04294c16275
4
- data.tar.gz: 6e9322d0cd82782f5b2aa0955cef84b7ff7d953e
3
+ metadata.gz: f093140def0544a9f01d175bfbc950e63626f6b0
4
+ data.tar.gz: f11e580bc9922f4bce4b5ffdf7446c374e87872d
5
5
  SHA512:
6
- metadata.gz: 70666914a1dd3a6ec81971dcfdac23df3ad78691fdfa0649be88f34f7f6263ff3460664d64375e277cb37db5eec7ce8d174623e9c75d0d7fcb5b5cdbbf55172e
7
- data.tar.gz: accc175ac2b6d76d35751a2cbc0dcecca2516b2f1f069576741126d1ddc6443300c5e54b74070a1960147633f05b35c7fae65ec132be8b322c3bd3e864f07041
6
+ metadata.gz: 616de967cf9baf0707117f9c617204e917dd7c4aa59bed13bef8d15d965e70ad79ccacc96eeaa926e516b76a1fe25a37d6bed82d62da7339d205fb7b35a4763b
7
+ data.tar.gz: 0f903eb6650fb2eac0509d4eaebfaff58b62fb291cb325d696f9f74d3da7e531ce92b4cdd7083e2982718f6ef74531343066200d0d126f0d8cd74561ffa70d4b
@@ -1,39 +1,46 @@
1
1
  module ItunesApi
2
2
  module Music
3
- # Use to retrieve available albums from artist's ids.
3
+ # Retrieves album tracks info.
4
4
  class Album
5
+ attr_accessor :name
5
6
  include Request
6
7
 
7
- TYPE = 'Album'.freeze
8
-
9
- def initialize(artists_id)
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 self.info(artists_id)
14
- new(artists_id).info
13
+ def tracks
14
+ @tracks ||= filtered_results.sort_by { |track| track['trackNumber'] }
15
15
  end
16
16
 
17
- def info
18
- @info ||= results
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 url
27
- "#{BASE_URL}/lookup?id=#{all_artists_id}&" \
28
- "entity=#{entity}&country=#{COUNTRY_CODE}"
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 all_artists_id
32
- @artists_id.join(',')
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 entity
36
- TYPE.downcase
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
@@ -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
- # Use to retrieve artist ids from their names.
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 self.apple_ids(artist_name)
14
- new(artist_name).apple_ids
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
- results.collect{ |r| r['artistId'] }.compact.uniq.sort
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 url
24
- "#{BASE_URL}/search?#{query}"
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 query
28
- Addressable::URI.new(
29
- query_values: { term: @artist_name }
30
- ).query
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
@@ -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 response
12
- JSON.parse(request)
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
@@ -1,3 +1,3 @@
1
1
  module ItunesApi
2
- VERSION = '0.0.3'.freeze
2
+ VERSION = '0.0.4'.freeze
3
3
  end
data/lib/itunes_api.rb CHANGED
@@ -2,6 +2,7 @@ require 'itunes_api/all'
2
2
 
3
3
  # Interface to the Itunes Api
4
4
  module ItunesApi
5
- COUNTRY_CODE = 'GB'.freeze
6
5
  BASE_URL = 'https://itunes.apple.com'.freeze
6
+ COUNTRY_CODE = 'GB'.freeze
7
+ LIMIT = 200
7
8
  end
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.3
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-18 00:00:00.000000000 Z
11
+ date: 2016-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable