Module: Bnm::API

Defined in:
lib/bnm/api.rb

Class Method Summary collapse

Class Method Details

.album_api_call(artist, album) ⇒ Object



29
30
31
32
33
# File 'lib/bnm/api.rb', line 29

def self.album_api_call(artist, album)
  album_response = Faraday.get "http://itunes.apple.com/lookup?id=#{album}&entity=album&attribute=albumTerm"
  album_json = JSON.parse(album_response.body)
  artist[:itunes] = results(artist, album_json)['collectionViewUrl']
end

.artist_api_call(artist) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/bnm/api.rb', line 12

def self.artist_api_call(artist)
  slugged_name = artist[:name].split(' ').join('+')
  artist_response = Faraday.get "http://itunes.apple.com/search?term=#{slugged_name}"
  artist_json = JSON.parse(artist_response.body)
  response = self.results(artist, artist_json)
  if response
    self.album_api_call(artist, response['collectionId'])
  end
end

.itunes(artists) ⇒ Object



5
6
7
8
9
# File 'lib/bnm/api.rb', line 5

def self.itunes(artists)
  artists.map do |artist|
    artist_response = artist_api_call(artist)
  end
end

.results(artist, res) ⇒ Object



22
23
24
25
26
27
# File 'lib/bnm/api.rb', line 22

def self.results(artist, res)
  response = res['results']
  if response.length > 0
    response.find {|item| item['collectionName'] == artist[:album] && item['artistName'] == artist[:name]}
  end
end