module Bnm::API

Public Class Methods

album_api_call(artist, album) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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