itunes_api 0.2.0 → 0.3

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: 0cefe7c03674f77de95c0ad26c37f7bcf1846228
4
- data.tar.gz: d0ae1fa1c14205710238e2c49ec8d61f6c4fb332
3
+ metadata.gz: 0c1598295d32c9506679762afd2abca9f790ba68
4
+ data.tar.gz: 9a3afea483d0203ed586d925480e5cb2e83a6031
5
5
  SHA512:
6
- metadata.gz: 602db34f182e67fae632a47a5e3a955c964ed5c27c36e36ea6a910ffbaef40d04f2969250f613e0f4f9f184ab42813be6aa96d076e186b84375d93cb14a5d556
7
- data.tar.gz: ae5429fdef3d026b6c881d275d296781323d769db55b9f49d1474f736c2422b6835fbf2f5582b084423a1fef1b4cbec71fb481fdd1422d024207c91c4913d1e3
6
+ metadata.gz: c2ff9e9ef1bf7be358865930aae4d308e0819ce6537bdad89081a596117444744e6f921c5df4e956da11d716ee44f0436fa2d3e310fed1645e1420b0d1646bb8
7
+ data.tar.gz: '09e2991f926527d5558d3772c697d5e2aaa24aab87f4e6394981d7f22fe8fe1bdb41f172f0a22c248b8fd82041e152a367e3f611cd15c10f5907ba75a88a2fbb'
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
1
  source 'https://rubygems.org'
2
+ ruby '2.3.3'
2
3
 
3
4
  # Specify your gem's dependencies in itunes_api.gemspec
4
5
  gemspec
data/README.md CHANGED
@@ -1,3 +1,6 @@
1
+ [![CircleCI](https://circleci.com/gh/mariodarco/itunes-api/tree/master.svg?style=shield)](https://circleci.com/gh/mariodarco/itunes-api/tree/master)
2
+
3
+
1
4
  # ItunesApi
2
5
 
3
6
  A simple interface for the Itunes Api.
@@ -12,18 +15,41 @@ gem 'itunes_api'
12
15
 
13
16
  And then execute:
14
17
 
15
- $ bundle
18
+ ```ruby
19
+ $ bundle
20
+ ```
16
21
 
17
22
  Or install it yourself as:
18
23
 
19
- $ gem install itunes_api
24
+ ```ruby
25
+ $ gem install itunes_api
26
+ ```
20
27
 
21
28
  ## Usage
22
29
 
23
- TODO: Write usage instructions here
30
+ To return all the AMG ids associated with a search term.
31
+ ```ruby
32
+ ItunesApi.artist_ids('Pink Floyd')
33
+ ```
34
+
35
+ To return structured information abut artists and albums associated with a search term.
36
+ ```ruby
37
+ ItunesApi.artist_ids('Pink Floyd')
38
+ ```
24
39
 
25
40
  ## Development
26
41
 
42
+ Ruby version:
43
+ ```
44
+ 2.3.3
45
+ ```
46
+
47
+ Fork the project, clone the repository and bundle:
48
+ ```
49
+ >> git clone https://github.com/{your_account}/itunes-api.git
50
+ >> cd itunes-api
51
+ ```
52
+
27
53
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
54
 
29
55
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
@@ -32,8 +58,6 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
58
 
33
59
  Bug reports and pull requests are welcome on GitHub at https://github.com/mariodarco/itunes-api. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
34
60
 
35
-
36
61
  ## License
37
62
 
38
63
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
39
-
data/itunes_api.gemspec CHANGED
@@ -1,4 +1,5 @@
1
1
  # coding: utf-8
2
+
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'itunes_api/version'
@@ -30,10 +31,10 @@ Gem::Specification.new do |spec|
30
31
 
31
32
  spec.metadata['allowed_push_host'] = 'https://rubygems.org'
32
33
 
33
- spec.add_dependency 'addressable', '~> 2.5'
34
+ spec.add_dependency 'addressable'
34
35
 
35
- spec.add_development_dependency 'bundler', '~> 1.13'
36
- spec.add_development_dependency 'rake', '~> 10.0'
37
- spec.add_development_dependency 'rspec', '~> 3.0'
36
+ spec.add_development_dependency 'bundler'
37
+ spec.add_development_dependency 'rake'
38
+ spec.add_development_dependency 'rspec'
38
39
  spec.add_development_dependency 'rubocop'
39
40
  end
@@ -1,44 +1,17 @@
1
1
  module ItunesApi
2
2
  module Music
3
- # Retrieves album tracks info.
3
+ # Wrapper for album results.
4
4
  class Album
5
- attr_accessor :name, :artwork, :track_count, :release_date
6
- include Request
5
+ attr_reader :name, :artwork, :released
7
6
 
8
- def initialize(info)
9
- @name = info['collectionName']
10
- @album_id = info['collectionId']
11
- @artwork = info['artworkUrl100']
12
- @track_count = info['trackCount']
13
- @release_date = Date.parse(info['releaseDate'])
7
+ def initialize(data)
8
+ @name = data['collectionName']
9
+ @artwork = data['artworkUrl100']
10
+ @released = Date.parse(data['releaseDate'])
14
11
  end
15
12
 
16
- def apple_music?
17
- streamable_tracks.any?
18
- end
19
-
20
- def streamable_tracks
21
- tracks.select { |track| track['isStreamable'] }
22
- end
23
-
24
- def tracks
25
- @tracks ||= filtered_results.sort_by { |track| track['trackNumber'] }
26
- end
27
-
28
- private
29
-
30
- def filtered_results
31
- @filtered_results ||= results.find_all do |result|
32
- result['collectionId'] == @album_id
33
- end
34
- end
35
-
36
- def query_values
37
- {
38
- attribute: 'albumTerm',
39
- entity: 'musicTrack',
40
- term: name
41
- }.merge(super)
13
+ def self.build(albums_data)
14
+ albums_data.map { |data| new(data) }
42
15
  end
43
16
  end
44
17
  end
@@ -1,54 +1,13 @@
1
- require 'addressable/uri'
2
-
3
1
  module ItunesApi
4
2
  module Music
5
- # Retrieve apple ids and albums of artists.
3
+ # Artist or Band from the Apple catalog
6
4
  class Artist
7
- include Request
8
-
9
- def initialize(artist_name, apple_id = nil)
10
- @artist_name = artist_name
11
- @apple_id = normalize(apple_id.to_i)
12
- end
13
-
14
- def albums(limit = 3)
15
- @albums ||= filtered_results
16
- .sort_by { |result| result['releaseDate'] }
17
- .reverse
18
- .first(limit)
19
- .map { |info| Album.new(info) }
20
- end
21
-
22
- def normalize(apple_id)
23
- apple_id.zero? ? nil : apple_id
24
- end
25
-
26
- def apple_ids
27
- return [@apple_id] if @apple_id
28
-
29
- @apple_ids ||= filtered_albums.collect do |album|
30
- album['artistId']
31
- end.compact.uniq.sort
32
- end
33
-
34
- private
35
-
36
- def filtered_albums
37
- results.find_all { |result| result['collectionType'] == 'Album' }
38
- end
39
-
40
- def filtered_results
41
- filtered_albums.find_all do |album|
42
- (!@apple_id || album['artistId'] == @apple_id)
43
- end
44
- end
5
+ attr_reader :name, :artist_id, :albums
45
6
 
46
- def query_values
47
- {
48
- attribute: 'artistTerm',
49
- entity: 'album',
50
- term: @artist_name
51
- }.merge(super)
7
+ def initialize(data)
8
+ @name = data['artistName']
9
+ @artist_id = data['amgArtistId']
10
+ @albums = Album.build(data[:albums])
52
11
  end
53
12
  end
54
13
  end
@@ -0,0 +1,30 @@
1
+ module ItunesApi
2
+ module Music
3
+ # Artists or Bands resulting from a search
4
+ class ArtistSearch
5
+ def initialize(name)
6
+ @name = name
7
+ end
8
+
9
+ def self.artists(name)
10
+ new(name).artists
11
+ end
12
+
13
+ def artists
14
+ @artists ||= artist_ids.map { |id| build_artist(id) }
15
+ end
16
+
17
+ private
18
+
19
+ def artist_ids
20
+ @search ||= ItunesApi.artist_ids(@name)
21
+ end
22
+
23
+ def build_artist(id)
24
+ Artist.new(
25
+ Requests::Lookup.artist_with_albums(id)
26
+ )
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,39 @@
1
+ require 'open-uri'
2
+ require 'json'
3
+ module ItunesApi
4
+ module Requests
5
+ # Allow requests to the iTunes API.
6
+ module Base
7
+ def results
8
+ response.fetch('results') { [] }
9
+ end
10
+
11
+ private
12
+
13
+ def query
14
+ Addressable::URI.new(
15
+ query_values: query_values
16
+ ).query
17
+ end
18
+
19
+ def request
20
+ open(url).read
21
+ end
22
+
23
+ def response
24
+ @response ||= JSON.parse(request)
25
+ end
26
+
27
+ def symbolize_keys(hash)
28
+ hash.each_with_object({}) do |(key, value), new_hash|
29
+ new_hash[key.to_sym] = value
30
+ new_hash
31
+ end
32
+ end
33
+
34
+ def url
35
+ "#{BASE_URL}/#{action}?#{query}"
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,65 @@
1
+ module ItunesApi
2
+ module Requests
3
+ # Allows querying the API via lookup.
4
+ class Lookup
5
+ include Base
6
+
7
+ def initialize(artist_id)
8
+ @artist_id = artist_id
9
+ end
10
+
11
+ def self.artist_with_albums(id)
12
+ new(id).artist_with_albums
13
+ end
14
+
15
+ def artist_with_albums
16
+ artist.merge(albums: albums)
17
+ end
18
+
19
+ private
20
+
21
+ def action
22
+ 'lookup'
23
+ end
24
+
25
+ def albums
26
+ albums_data.map do |album|
27
+ symbolize_keys(unwrapped(album))
28
+ end
29
+ end
30
+
31
+ def albums_data
32
+ results.find_all { |wrappers| wrappers['wrapperType'] == 'collection' }
33
+ end
34
+
35
+ def artist
36
+ symbolize_keys(unwrapped(artist_data))
37
+ end
38
+
39
+ def artist_data
40
+ symbolize_keys(
41
+ unwrapped(
42
+ results.find do |wrappers|
43
+ wrappers['wrapperType'] == 'artist'
44
+ end
45
+ )
46
+ )
47
+ end
48
+
49
+ def query_values
50
+ {
51
+ entity: 'album',
52
+ amgArtistId: @artist_id,
53
+ country: COUNTRY_CODE,
54
+ limit: LIMIT,
55
+ sort: 'recent'
56
+ }
57
+ end
58
+
59
+ def unwrapped(hash)
60
+ hash.delete('wrapperType')
61
+ hash
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,41 @@
1
+ require 'addressable/uri'
2
+ module ItunesApi
3
+ module Requests
4
+ # Fetch all the artist ids corresponding to a search term
5
+ class Search
6
+ include Base
7
+
8
+ def initialize(artist_name)
9
+ @artist_name = artist_name
10
+ end
11
+
12
+ def self.artist_ids(artist_name)
13
+ new(artist_name).artist_ids
14
+ end
15
+
16
+ def artist_ids
17
+ results.collect do |result|
18
+ result['amgArtistId']
19
+ end.compact.uniq.sort
20
+ end
21
+
22
+ private
23
+
24
+ def action
25
+ 'search'
26
+ end
27
+
28
+ def query_values
29
+ {
30
+ attribute: 'artistTerm',
31
+ entity: 'album',
32
+ term: @artist_name,
33
+ country: COUNTRY_CODE,
34
+ limit: LIMIT,
35
+ media: 'music',
36
+ sort: 'recent'
37
+ }
38
+ end
39
+ end
40
+ end
41
+ end
@@ -1,3 +1,3 @@
1
1
  module ItunesApi
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3'.freeze
3
3
  end
data/lib/itunes_api.rb CHANGED
@@ -1,8 +1,21 @@
1
- require 'itunes_api/all'
1
+ require 'itunes_api/requests/base'
2
+ require 'itunes_api/requests/lookup'
3
+ require 'itunes_api/requests/search'
4
+ require 'itunes_api/music/album'
5
+ require 'itunes_api/music/artist'
6
+ require 'itunes_api/music/artist_search'
2
7
 
3
8
  # Interface to the Itunes Api
4
9
  module ItunesApi
5
10
  BASE_URL = 'https://itunes.apple.com'.freeze
6
11
  COUNTRY_CODE = 'GB'.freeze
7
12
  LIMIT = 200
13
+
14
+ def self.artist_ids(name)
15
+ Requests::Search.artist_ids(name)
16
+ end
17
+
18
+ def self.artist_search(name)
19
+ Music::ArtistSearch.artists(name)
20
+ end
8
21
  end
metadata CHANGED
@@ -1,71 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itunes_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: '0.3'
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: 2017-01-08 00:00:00.000000000 Z
11
+ date: 2017-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '2.5'
19
+ version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '2.5'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '1.13'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '1.13'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '3.0'
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '3.0'
68
+ version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rubocop
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -89,8 +89,6 @@ extra_rdoc_files: []
89
89
  files:
90
90
  - ".gitignore"
91
91
  - ".rspec"
92
- - ".ruby-version"
93
- - ".travis.yml"
94
92
  - CODE_OF_CONDUCT.md
95
93
  - Gemfile
96
94
  - LICENSE.txt
@@ -100,11 +98,12 @@ files:
100
98
  - bin/setup
101
99
  - itunes_api.gemspec
102
100
  - lib/itunes_api.rb
103
- - lib/itunes_api/all.rb
104
101
  - lib/itunes_api/music/album.rb
105
- - lib/itunes_api/music/all.rb
106
102
  - lib/itunes_api/music/artist.rb
107
- - lib/itunes_api/request.rb
103
+ - lib/itunes_api/music/artist_search.rb
104
+ - lib/itunes_api/requests/base.rb
105
+ - lib/itunes_api/requests/lookup.rb
106
+ - lib/itunes_api/requests/search.rb
108
107
  - lib/itunes_api/version.rb
109
108
  homepage: https://github.com/mariodarco/itunes-api
110
109
  licenses:
@@ -127,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
126
  version: '0'
128
127
  requirements: []
129
128
  rubyforge_project:
130
- rubygems_version: 2.6.8
129
+ rubygems_version: 2.5.2
131
130
  signing_key:
132
131
  specification_version: 4
133
132
  summary: iTunes Api
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- ruby-2.4
data/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.3.1
5
- before_install: gem install bundler -v 1.13.6
@@ -1,2 +0,0 @@
1
- require_relative 'request'
2
- require_relative 'music/all'
@@ -1,2 +0,0 @@
1
- require_relative 'album'
2
- require_relative 'artist'
@@ -1,40 +0,0 @@
1
- require 'open-uri'
2
- require 'json'
3
-
4
- module ItunesApi
5
- # Allow requests to the iTunes API.
6
- module Request
7
-
8
- def response
9
- JSON.parse(request)
10
- end
11
-
12
- private
13
-
14
- def request
15
- @request ||= open(url).read
16
- end
17
-
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
- }
30
- end
31
-
32
- def results
33
- response.fetch('results', [])
34
- end
35
-
36
- def url
37
- "#{BASE_URL}/search?#{query}"
38
- end
39
- end
40
- end