itunes_api 1.4.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7d9d1bb43f0c302f38b5f6b1a39250a0b353b46e
4
- data.tar.gz: da5322824332f88448a44ffd0951719cb261bfe3
3
+ metadata.gz: b59950a6d1529d61e387ac0ef1306324959bc43d
4
+ data.tar.gz: d5bb63b84da411992c53a7178224e34023d2f02a
5
5
  SHA512:
6
- metadata.gz: 67350e54f9d59ac5545fcfc01f004aae3a46368da8c35213b35a1fcc28d5646182eaed7b9733597df78b33f65f10d76046dd83bc5dc6c3544faa4708f806088f
7
- data.tar.gz: 163f60f34e6995f5bd11c083956f85ffdbf28caa27db5251a9c3a60196d0ada629d64a5006317eab7e53839917af92b30feb673dbb06451c655ec6913590a6dc
6
+ metadata.gz: 4cabff1c9f4a1d757231f46d1623ed2d5506efee42e289ae92e63aa979bff1a3a6944e8170c885d468d2a0f314715192b5108385609d8a91e50317b0b802ee33
7
+ data.tar.gz: 63d044a01c39946370f2133a0190a0fad390db937fca70d43ac4b1885134154bff678ddccb489906602144e3dea5b66815b87a39115af2f795c904a9236d9ec6
data/.gitignore CHANGED
@@ -1,9 +1,8 @@
1
1
  /.bundle/
2
2
  /.yardoc
3
- /Gemfile.lock
4
3
  /_yardoc/
5
- /coverage/
6
4
  /doc/
5
+ /Gemfile.lock
7
6
  /pkg/
8
7
  /spec/reports/
9
8
  /tmp/
data/.rspec CHANGED
@@ -1,2 +1,4 @@
1
- --format documentation
2
1
  --color
2
+ --fail-fast
3
+ --format documentation
4
+ --warning
data/Gemfile CHANGED
@@ -1,5 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
  ruby '2.4.1'
3
3
 
4
- # Specify your gem's dependencies in itunes_api.gemspec
5
4
  gemspec
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2016 Mario D’Arco
3
+ Copyright (c) 2017 Mario D’Arco
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ItunesApi
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/itunes_api.svg)](https://badge.fury.io/rb/itunes_api) [![Dependency Status](https://gemnasium.com/badges/github.com/mariodarco/itunes-api.svg)](https://gemnasium.com/github.com/mariodarco/itunes-api) [![CircleCI](https://circleci.com/gh/mariodarco/itunes-api/tree/master.svg?style=shield)](https://circleci.com/gh/mariodarco/itunes-api/tree/master) [![Coverage Status](https://coveralls.io/repos/github/mariodarco/itunes-api/badge.svg?branch=master)](https://coveralls.io/github/mariodarco/itunes-api?branch=master)
3
+ [![Gem Version](https://badge.fury.io/rb/itunes_api.svg)](https://badge.fury.io/rb/itunes_api) [![Dependency Status](https://gemnasium.com/badges/github.com/mariodarco/itunes-api.svg)](https://gemnasium.com/github.com/mariodarco/itunes-api) [![CircleCI](https://circleci.com/gh/mariodarco/itunes-api/tree/master.svg?style=shield)](https://circleci.com/gh/mariodarco/itunes-api/tree/master)
4
4
 
5
5
  A simple interface for the Itunes Api.
6
6
 
@@ -37,37 +37,40 @@ end
37
37
 
38
38
  ## Usage
39
39
 
40
- To return all the Apple ids associated with the search for a Artist name.
40
+ To return all the Apple ids associated with the search for a Artist name
41
41
 
42
42
  ```ruby
43
- ItunesApi.artist_ids('Pink Floyd')
43
+ ItunesApi::Music.all_apple_ids('Pink Floyd')
44
44
  ```
45
45
 
46
- To get all artists returning from a search term, and their albums.
46
+ To get all artists returning from a search term
47
47
 
48
48
  ```ruby
49
- ItunesApi.artist_search('Led Zeppelin')
49
+ ItunesApi::Music.all_artists_by_name('Led Zeppelin')
50
50
  ```
51
51
 
52
- To return a specific artist with albums, based on the Apple id.
52
+ To return a specific artist, based on the artist's Apple id
53
53
 
54
54
  ```ruby
55
- ItunesApi.artist_lookup(265766061)
55
+ ItunesApi::Music.find_by_apple_id(265766061)
56
56
  ```
57
57
 
58
- To return the tracklist for a specific albums, based on the Apple collection id.
58
+ Note that each request that returns artists does not automatically return albums.
59
+ They are lazily fetched when calling artist.albums.
60
+
61
+ To return only the albums for a specific artist, based on the artist's Apple id
59
62
 
60
63
  ```ruby
61
- ItunesApi.album_lookup(286930912)
64
+ ItunesApi::Music.albums_for_artist(265766061)
62
65
  ```
63
66
 
64
- An argument holding the country code can be used to look into different stores than the default one.
67
+ An argument holding the country code can be used to look into different stores than the default one
65
68
 
66
69
  ```ruby
67
- ItunesApi.artist_ids('Pink Floyd', :it)
68
- ItunesApi.artist_search('Led Zeppelin', :fr)
69
- ItunesApi.artist_lookup(265766061, :us)
70
- ItunesApi.album_lookup(286930912, :gb)
70
+ ItunesApi::Music.all_apple_ids('Pink Floyd', :it)
71
+ ItunesApi::Music.all_artists_by_name('Led Zeppelin', :fr)
72
+ ItunesApi::Music.find_by_apple_id(265766061, :us)
73
+ ItunesApi::Music.albums_for_artist(265766061, :es)
71
74
  ```
72
75
 
73
76
  ## Development
data/Rakefile CHANGED
@@ -2,5 +2,4 @@ require 'bundler/gem_tasks'
2
2
  require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
-
6
5
  task default: :spec
data/bin/console CHANGED
@@ -2,13 +2,6 @@
2
2
 
3
3
  require 'bundler/setup'
4
4
  require 'itunes_api'
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
5
  require 'irb'
6
+
14
7
  IRB.start
data/bin/setup CHANGED
@@ -1,8 +1,7 @@
1
1
  #!/usr/bin/env bash
2
+
2
3
  set -euo pipefail
3
4
  IFS=$'\n\t'
4
5
  set -vx
5
6
 
6
7
  bundle install
7
-
8
- # Do any other automated setup that you need to do here
data/itunes_api.gemspec CHANGED
@@ -27,10 +27,8 @@ Gem::Specification.new do |spec|
27
27
  spec.add_dependency 'selfies'
28
28
 
29
29
  spec.add_development_dependency 'bundler'
30
- spec.add_development_dependency 'coveralls'
31
30
  spec.add_development_dependency 'rake'
32
31
  spec.add_development_dependency 'rspec'
33
- spec.add_development_dependency 'simplecov'
34
32
  spec.add_development_dependency 'rubocop'
35
33
  spec.add_development_dependency 'vcr'
36
34
  end
@@ -3,64 +3,57 @@ module ItunesApi
3
3
  module Music
4
4
  # Wrapper for album results.
5
5
  class Album
6
- attr_reader_init :data, :store
7
- private :data
6
+ attr_reader_init :artwork, :collection_id, :genre, :name, :release_on, :store
8
7
 
9
- def self.build(albums_data, store)
10
- albums_data.map { |data| new(data, store) }
11
- .sort_by(&:released)
12
- .reverse
13
- end
14
-
15
- def artwork
16
- @artwork ||= data[:artworkUrl100]
17
- end
18
-
19
- def availability
20
- prefix = pre_release? ? 'pre_release_' : ''
21
-
22
- if apple_music?
23
- "#{prefix}streaming".to_sym
24
- else
25
- "#{prefix}sale".to_sym
8
+ class << self
9
+ def for_artist(apple_id, store)
10
+ albums(apple_id, store).map { |album| new(*album.attributes) }
26
11
  end
27
- end
28
12
 
29
- def collection_id
30
- @collection_id ||= data[:collectionId]
31
- end
13
+ private
32
14
 
33
- def name
34
- @name ||= data[:collectionName]
15
+ def albums(apple_id, store)
16
+ Requests::Albums.find_by_apple_id(apple_id, store)
17
+ end
35
18
  end
36
19
 
37
- def released
38
- @released ||= Date.parse(data[:releaseDate])
20
+ def availability
21
+ @availability ||= build_availability
39
22
  end
40
23
 
41
24
  def to_hash
42
25
  {
43
26
  artwork: artwork,
27
+ availability: availability,
44
28
  collection_id: collection_id,
45
29
  name: name,
46
- released: released,
47
- availability: availability,
30
+ release_on: release_on,
48
31
  store: store
49
32
  }
50
33
  end
51
34
 
35
+ def tracklist
36
+ @tracklist ||= songs.map { |song| Song.new(*song.attributes) }
37
+ end
38
+
52
39
  private
53
40
 
54
41
  def apple_music?
55
42
  tracklist.any?(&:streamable?)
56
43
  end
57
44
 
45
+ def build_availability
46
+ prefix = pre_release? ? 'pre_release_' : ''
47
+ suffix = apple_music? ? 'streaming' : 'sale'
48
+ "#{prefix}#{suffix}".to_sym
49
+ end
50
+
58
51
  def pre_release?
59
- released > Date.today
52
+ release_on > Date.today
60
53
  end
61
54
 
62
- def tracklist
63
- @tracklist ||= AlbumLookup.tracklist(collection_id, store)
55
+ def songs
56
+ @songs ||= Requests::Songs.find_by_collection_id(collection_id, store)
64
57
  end
65
58
  end
66
59
  end
@@ -2,19 +2,33 @@ module ItunesApi
2
2
  module Music
3
3
  # Artist or Band from the Apple catalog
4
4
  class Artist
5
- attr_reader_init :data, :store
6
- private :data
5
+ attr_reader_init :apple_id, :genre, :name, :store
7
6
 
8
- def albums
9
- @albums ||= Album.build(data[:albums], store)
10
- end
7
+ class << self
8
+ def all_apple_ids(name, store)
9
+ find_all_by_name(name, store).map(&:apple_id)
10
+ end
11
+
12
+ def find_all_by_name(name, store)
13
+ Requests::Search.artists(name, store).map do |result|
14
+ new(*result.attributes)
15
+ end
16
+ end
11
17
 
12
- def apple_id
13
- @apple_id ||= data[:artistId]
18
+ def find_by_apple_id(apple_id, store)
19
+ artist = artist(apple_id, store)
20
+ artist ? new(*artist.attributes) : nil
21
+ end
22
+
23
+ private
24
+
25
+ def artist(apple_id, store)
26
+ Requests::Artist.find_by_apple_id(apple_id, store)
27
+ end
14
28
  end
15
29
 
16
- def name
17
- @name ||= data[:artistName]
30
+ def albums
31
+ @albums ||= Album.for_artist(apple_id, store)
18
32
  end
19
33
 
20
34
  def to_hash
@@ -0,0 +1,41 @@
1
+ module ItunesApi
2
+ module Music
3
+ module Requests
4
+ # Allows querying the API via lookup for albums.
5
+ class Albums
6
+ include Request
7
+ attr_reader_init :apple_id, :store
8
+ selfie :find_by_apple_id
9
+
10
+ def find_by_apple_id
11
+ unwrapped_results.map do |result|
12
+ Results::Album.new(result, store)
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def action
19
+ 'lookup'
20
+ end
21
+
22
+ def query
23
+ {
24
+ id: apple_id,
25
+ country: store.to_s.upcase,
26
+ entity: :album,
27
+ limit: LIMIT
28
+ }
29
+ end
30
+
31
+ def unwrapped_results
32
+ return [] unless results.any?
33
+
34
+ results.find_all do |wrappers|
35
+ wrappers['wrapperType'] == 'collection'
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,32 @@
1
+ module ItunesApi
2
+ module Music
3
+ module Requests
4
+ # Allows querying the API via lookup for artists.
5
+ class Artist
6
+ include Request
7
+ attr_reader_init :apple_id, :store
8
+ selfie :find_by_apple_id
9
+
10
+ def find_by_apple_id
11
+ return nil unless results.any?
12
+
13
+ Results::Artist.new(results.first, store)
14
+ end
15
+
16
+ private
17
+
18
+ def action
19
+ 'lookup'
20
+ end
21
+
22
+ def query
23
+ {
24
+ id: apple_id,
25
+ country: store.to_s.upcase,
26
+ limit: LIMIT
27
+ }
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,39 @@
1
+ module ItunesApi
2
+ module Music
3
+ module Requests
4
+ # Allows querying the API via search for artists.
5
+ class Search
6
+ include Request
7
+ attr_reader_init :artist_name, :store
8
+ selfie :artists
9
+
10
+ def artists
11
+ @artists ||= decorated_results.find_all(&:relevant?)
12
+ end
13
+
14
+ private
15
+
16
+ def action
17
+ 'search'
18
+ end
19
+
20
+ def decorated_results
21
+ results.map do |result|
22
+ Results::Artist.new(result, store, artist_name)
23
+ end
24
+ end
25
+
26
+ def query
27
+ {
28
+ attribute: 'artistTerm',
29
+ country: store.to_s.upcase,
30
+ entity: 'musicArtist',
31
+ limit: LIMIT,
32
+ media: 'music',
33
+ term: artist_name
34
+ }
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,42 @@
1
+ module ItunesApi
2
+ module Music
3
+ module Requests
4
+ # Allows querying the API via lookup for album songs.
5
+ class Songs
6
+ include Request
7
+ attr_reader_init :collection_id, :store
8
+ selfie :find_by_collection_id
9
+
10
+ def find_by_collection_id
11
+ unwrapped_results.map do |result|
12
+ Results::Song.new(result)
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def action
19
+ 'lookup'
20
+ end
21
+
22
+ def query
23
+ {
24
+ entity: 'song',
25
+ id: collection_id,
26
+ country: store.to_s.upcase,
27
+ limit: LIMIT,
28
+ sort: 'trackNumber'
29
+ }
30
+ end
31
+
32
+ def unwrapped_results
33
+ return [] unless results.any?
34
+
35
+ results.find_all do |wrappers|
36
+ wrappers['wrapperType'] == 'track'
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,12 @@
1
+ require_relative 'requests/albums'
2
+ require_relative 'requests/artist'
3
+ require_relative 'requests/search'
4
+ require_relative 'requests/songs'
5
+
6
+ module ItunesApi
7
+ module Music
8
+ # Starting module for request wrappers
9
+ module Requests
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,35 @@
1
+ module ItunesApi
2
+ module Music
3
+ module Results
4
+ # Wrapper for album search results.
5
+ class Album
6
+ attr_reader_init :data, :store
7
+ private :data
8
+
9
+ def artwork
10
+ @artwork ||= data['artworkUrl100']
11
+ end
12
+
13
+ def attributes
14
+ [artwork, collection_id, genre, name, release_on, store]
15
+ end
16
+
17
+ def collection_id
18
+ @collection_id ||= data['collectionId']
19
+ end
20
+
21
+ def genre
22
+ @genre ||= data['primaryGenreName']
23
+ end
24
+
25
+ def name
26
+ @name ||= data['collectionName']
27
+ end
28
+
29
+ def release_on
30
+ @release_on ||= Date.parse(data['releaseDate'])
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,33 @@
1
+ module ItunesApi
2
+ module Music
3
+ module Results
4
+ # Wrapper for artist search results.
5
+ class Artist
6
+ attr_reader_init :data, :store, search_term: nil
7
+ private :data, :search_term
8
+
9
+ def apple_id
10
+ @apple_id ||= data['artistId']
11
+ end
12
+
13
+ def attributes
14
+ [apple_id, genre, name, store]
15
+ end
16
+
17
+ def genre
18
+ @genre ||= data['primaryGenreName']
19
+ end
20
+
21
+ def name
22
+ @name ||= data['artistName']
23
+ end
24
+
25
+ def relevant?
26
+ return true unless search_term
27
+
28
+ name.downcase.include?(search_term.downcase)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,39 @@
1
+ module ItunesApi
2
+ module Music
3
+ module Results
4
+ # Wrapper for song search results.
5
+ class Song
6
+ attr_reader_init :data
7
+ private :data
8
+
9
+ def duration
10
+ @duration ||= track_lenght(data['trackTimeMillis'])
11
+ end
12
+
13
+ def attributes
14
+ [duration, name, number, streamable]
15
+ end
16
+
17
+ def name
18
+ @name ||= data['trackName']
19
+ end
20
+
21
+ def number
22
+ @number ||= data['trackNumber']
23
+ end
24
+
25
+ def streamable
26
+ @streamable ||= data['isStreamable']
27
+ end
28
+
29
+ private
30
+
31
+ def track_lenght(milliseconds)
32
+ minutes, milliseconds = milliseconds.divmod(1000 * 60)
33
+ seconds = milliseconds / 1000
34
+ "#{minutes}:#{seconds}"
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,11 @@
1
+ require_relative 'results/album'
2
+ require_relative 'results/artist'
3
+ require_relative 'results/song'
4
+
5
+ module ItunesApi
6
+ module Music
7
+ # Starting module for result wrappers
8
+ module Results
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ module ItunesApi
2
+ module Music
3
+ # Wrapper for song results.
4
+ class Song
5
+ attr_reader_init :duration, :name, :number, :streamable
6
+ alias streamable? streamable
7
+
8
+ def to_hash
9
+ {
10
+ duration: duration,
11
+ name: name,
12
+ number: number,
13
+ stremable: streamable?,
14
+ }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'request'
2
+ require_relative 'music/requests'
3
+ require_relative 'music/results'
4
+ require_relative 'music/album'
5
+ require_relative 'music/artist'
6
+ require_relative 'music/song'
7
+
8
+ module ItunesApi
9
+ # Public interface for the Music Api
10
+ module Music
11
+ class << self
12
+ def all_apple_ids(name, store = default_store)
13
+ Artist.all_apple_ids(name, store)
14
+ end
15
+
16
+ def all_artists_by_name(name, store = default_store)
17
+ Artist.find_all_by_name(name, store)
18
+ end
19
+
20
+ def find_by_apple_id(apple_id, store = default_store)
21
+ Music::Artist.find_by_apple_id(apple_id, store)
22
+ end
23
+
24
+ def albums_for_artist(apple_id, store = default_store)
25
+ Music::Album.for_artist(apple_id, store)
26
+ end
27
+
28
+ private
29
+
30
+ def default_store
31
+ ItunesApi::Configuration.instance.country_code || :gb
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,24 @@
1
+ require 'faraday'
2
+ require 'json'
3
+ module ItunesApi
4
+ # Allow requests to the iTunes API.
5
+ module Request
6
+ def results
7
+ @results ||= parsed_response.fetch('results') { [] }
8
+ end
9
+
10
+ private
11
+
12
+ def parsed_response
13
+ JSON.parse(response.body)
14
+ end
15
+
16
+ def connection
17
+ Faraday.new(url: BASE_URL)
18
+ end
19
+
20
+ def response
21
+ connection.get(action, query)
22
+ end
23
+ end
24
+ end
@@ -1,3 +1,3 @@
1
1
  module ItunesApi
2
- VERSION = '1.4.1'.freeze
2
+ VERSION = '2.0.0'.freeze
3
3
  end
data/lib/itunes_api.rb CHANGED
@@ -1,36 +1,13 @@
1
1
  require 'selfies'
2
2
  require 'itunes_api/configuration'
3
- require 'itunes_api/requests/all'
4
- require 'itunes_api/music/all'
3
+ require 'itunes_api/music'
5
4
 
6
5
  # Interface to the Itunes Api
7
6
  module ItunesApi
8
7
  BASE_URL = 'https://itunes.apple.com'.freeze
9
8
  LIMIT = 200
10
9
 
11
- def self.artist_ids(name, store = default_store)
12
- Requests::Search.artist_ids(name, store)
13
- end
14
-
15
- def self.album_lookup(album_id, store = default_store)
16
- Music::AlbumLookup.tracklist(album_id, store)
17
- end
18
-
19
- def self.artist_lookup(artist_id, store = default_store)
20
- Music::ArtistLookup.artist(artist_id, store)
21
- end
22
-
23
- def self.artist_search(name, store = default_store)
24
- Music::ArtistSearch.artists(name, store)
25
- end
26
-
27
10
  def self.configure
28
11
  yield(Configuration.instance)
29
12
  end
30
-
31
- private
32
-
33
- def self.default_store
34
- Configuration.instance.country_code rescue :gb
35
- end
36
13
  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: 1.4.1
4
+ version: 2.0.0
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-08-03 00:00:00.000000000 Z
11
+ date: 2017-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -52,20 +52,6 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: coveralls
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
55
  - !ruby/object:Gem::Dependency
70
56
  name: rake
71
57
  requirement: !ruby/object:Gem::Requirement
@@ -94,20 +80,6 @@ dependencies:
94
80
  - - ">="
95
81
  - !ruby/object:Gem::Version
96
82
  version: '0'
97
- - !ruby/object:Gem::Dependency
98
- name: simplecov
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
83
  - !ruby/object:Gem::Dependency
112
84
  name: rubocop
113
85
  requirement: !ruby/object:Gem::Requirement
@@ -155,18 +127,20 @@ files:
155
127
  - itunes_api.gemspec
156
128
  - lib/itunes_api.rb
157
129
  - lib/itunes_api/configuration.rb
130
+ - lib/itunes_api/music.rb
158
131
  - lib/itunes_api/music/album.rb
159
- - lib/itunes_api/music/album_lookup.rb
160
- - lib/itunes_api/music/all.rb
161
132
  - lib/itunes_api/music/artist.rb
162
- - lib/itunes_api/music/artist_lookup.rb
163
- - lib/itunes_api/music/artist_search.rb
164
- - lib/itunes_api/music/track.rb
165
- - lib/itunes_api/requests/album_tracklist.rb
166
- - lib/itunes_api/requests/all.rb
167
- - lib/itunes_api/requests/base.rb
168
- - lib/itunes_api/requests/lookup.rb
169
- - lib/itunes_api/requests/search.rb
133
+ - lib/itunes_api/music/requests.rb
134
+ - lib/itunes_api/music/requests/albums.rb
135
+ - lib/itunes_api/music/requests/artist.rb
136
+ - lib/itunes_api/music/requests/search.rb
137
+ - lib/itunes_api/music/requests/songs.rb
138
+ - lib/itunes_api/music/results.rb
139
+ - lib/itunes_api/music/results/album.rb
140
+ - lib/itunes_api/music/results/artist.rb
141
+ - lib/itunes_api/music/results/song.rb
142
+ - lib/itunes_api/music/song.rb
143
+ - lib/itunes_api/request.rb
170
144
  - lib/itunes_api/version.rb
171
145
  homepage: https://github.com/mariodarco/itunes-api
172
146
  licenses:
@@ -1,21 +0,0 @@
1
- module ItunesApi
2
- module Music
3
- # Album Tracklist resulting from a specific lookup
4
- class AlbumLookup
5
- attr_reader_init :album_id, :store
6
- selfie :tracklist
7
-
8
- def tracklist
9
- return [] unless songs_data
10
-
11
- Track.build(songs_data)
12
- end
13
-
14
- private
15
-
16
- def songs_data
17
- @songs_data ||= Requests::AlbumTracklist.songs(album_id, store)
18
- end
19
- end
20
- end
21
- end
@@ -1,6 +0,0 @@
1
- require_relative 'album'
2
- require_relative 'artist'
3
- require_relative 'album_lookup'
4
- require_relative 'artist_lookup'
5
- require_relative 'artist_search'
6
- require_relative 'track'
@@ -1,25 +0,0 @@
1
- module ItunesApi
2
- module Music
3
- # Artist or Band resulting from a specific lookup
4
- class ArtistLookup
5
- attr_reader_init :artist_id, :store
6
- selfie :artist, :store
7
-
8
- def artist
9
- build_artist
10
- end
11
-
12
- private
13
-
14
- def build_artist
15
- return lookup unless lookup
16
-
17
- Artist.new(lookup, store)
18
- end
19
-
20
- def lookup
21
- @lookup ||= Requests::Lookup.artist_with_albums(artist_id, store)
22
- end
23
- end
24
- end
25
- end
@@ -1,31 +0,0 @@
1
- module ItunesApi
2
- module Music
3
- # Artists or Bands resulting from a search
4
- class ArtistSearch
5
- attr_reader_init :name, :store
6
- selfie :artists
7
-
8
- def artists
9
- @artists ||= artist_ids.map { |id| build_artist(id) }.compact
10
- end
11
-
12
- private
13
-
14
- def artist_ids
15
- @search ||= ItunesApi.artist_ids(name, store)
16
- end
17
-
18
- def build_artist(id)
19
- if (lookup = lookup(id))
20
- Artist.new(lookup, store)
21
- end
22
- end
23
-
24
- def lookup(id)
25
- Requests::Lookup.artist_with_albums(id, store)
26
- rescue
27
- nil
28
- end
29
- end
30
- end
31
- end
@@ -1,46 +0,0 @@
1
- module ItunesApi
2
- module Music
3
- # Wrapper for song results.
4
- class Track
5
- attr_reader_init :data
6
- private :data
7
-
8
- def self.build(songs_data)
9
- songs_data.map { |data| new(data) }
10
- end
11
-
12
- def duration
13
- @duration ||= track_lenght(data[:trackTimeMillis])
14
- end
15
-
16
- def name
17
- @name ||= data[:trackName]
18
- end
19
-
20
- def number
21
- data[:trackNumber]
22
- end
23
-
24
- def streamable?
25
- data[:isStreamable]
26
- end
27
-
28
- def to_hash
29
- {
30
- duration: duration,
31
- name: name,
32
- number: number,
33
- stremable: stremable?,
34
- }
35
- end
36
-
37
- private
38
-
39
- def track_lenght(milliseconds)
40
- minutes, milliseconds = milliseconds.divmod(1000 * 60)
41
- seconds = milliseconds / 1000
42
- "#{minutes}:#{seconds}"
43
- end
44
- end
45
- end
46
- end
@@ -1,36 +0,0 @@
1
- module ItunesApi
2
- module Requests
3
- # Allows querying the API via lookup for album songs.
4
- class AlbumTracklist
5
- include Base
6
- attr_reader_init :album_id, :store
7
- selfie :songs
8
-
9
- def songs
10
- songs_data.map do |song|
11
- symbolize_keys(unwrapped(song))
12
- end
13
- end
14
-
15
- private
16
-
17
- def action
18
- 'lookup'
19
- end
20
-
21
- def songs_data
22
- results.find_all { |wrappers| wrappers['wrapperType'] == 'track' }
23
- end
24
-
25
- def query
26
- {
27
- entity: 'song',
28
- id: album_id,
29
- country: store.to_s.upcase,
30
- limit: LIMIT,
31
- sort: 'trackNumber'
32
- }
33
- end
34
- end
35
- end
36
- end
@@ -1,4 +0,0 @@
1
- require_relative 'base'
2
- require_relative 'album_tracklist'
3
- require_relative 'lookup'
4
- require_relative 'search'
@@ -1,37 +0,0 @@
1
- require 'faraday'
2
- require 'json'
3
- module ItunesApi
4
- module Requests
5
- # Allow requests to the iTunes API.
6
- module Base
7
- def results
8
- @results ||= parsed_response.fetch('results') { [] }
9
- end
10
-
11
- private
12
-
13
- def parsed_response
14
- JSON.parse(response.body)
15
- end
16
-
17
- def connection
18
- Faraday.new(url: BASE_URL)
19
- end
20
-
21
- def response
22
- connection.get(action, query)
23
- end
24
-
25
- def symbolize_keys(hash)
26
- hash.each_with_object({}) do |(key, value), new_hash|
27
- new_hash[key.to_sym] = value
28
- new_hash
29
- end
30
- end
31
-
32
- def unwrapped(data_hash)
33
- data_hash.tap { |hash| hash.delete('wrapperType') }
34
- end
35
- end
36
- end
37
- end
@@ -1,54 +0,0 @@
1
- module ItunesApi
2
- module Requests
3
- # Allows querying the API via lookup.
4
- class Lookup
5
- include Base
6
- attr_reader_init :artist_id, :store
7
- selfie :artist_with_albums
8
-
9
- def artist_with_albums
10
- return artist unless artist
11
-
12
- artist.merge(albums: albums)
13
- end
14
-
15
- private
16
-
17
- def action
18
- 'lookup'
19
- end
20
-
21
- def albums
22
- albums_data.map do |album|
23
- symbolize_keys(unwrapped(album))
24
- end
25
- end
26
-
27
- def albums_data
28
- results.find_all { |wrappers| wrappers['wrapperType'] == 'collection' }
29
- end
30
-
31
- def artist
32
- return artist_data unless artist_data
33
-
34
- symbolize_keys(unwrapped(artist_data))
35
- end
36
-
37
- def artist_data
38
- @artist_data ||= results.find do |wrappers|
39
- wrappers['wrapperType'] == 'artist'
40
- end
41
- end
42
-
43
- def query
44
- {
45
- entity: 'album',
46
- id: artist_id,
47
- country: store.to_s.upcase,
48
- limit: LIMIT,
49
- sort: 'recent'
50
- }
51
- end
52
- end
53
- end
54
- end
@@ -1,38 +0,0 @@
1
- module ItunesApi
2
- module Requests
3
- # Fetch all the artist ids corresponding to a search term
4
- class Search
5
- include Base
6
- attr_reader_init :artist_name, :store
7
- selfie :artist_ids
8
-
9
- def artist_ids
10
- results.collect do |result|
11
- name_in?(result['artistName']) && result['artistId']
12
- end.compact.uniq
13
- end
14
-
15
- private
16
-
17
- def action
18
- 'search'
19
- end
20
-
21
- def name_in?(complete_name)
22
- complete_name.downcase.include?(artist_name.downcase) ? true : nil
23
- end
24
-
25
- def query
26
- {
27
- attribute: 'artistTerm',
28
- entity: 'musicArtist',
29
- term: artist_name,
30
- country: store.to_s.upcase,
31
- limit: LIMIT,
32
- media: 'music',
33
- sort: 'recent'
34
- }
35
- end
36
- end
37
- end
38
- end