class MusicMetaFixer::ItunesAPI

Constants

ITUNES_API_URL
MEDIA

Attributes

term[RW]

Public Class Methods

new(term, options={}) click to toggle source
# File lib/music_meta_fixer/itunes_api.rb, line 8
def initialize(term, options={})
  @term = term
  @limit = options[:limit] || 1
end

Public Instance Methods

get_song_info() click to toggle source
# File lib/music_meta_fixer/itunes_api.rb, line 18
def get_song_info
  res = Typhoeus.get(query)

  if res.code == 200
    json = JSON.parse res.body

    if json["resultCount"].to_i > 0
      song_info = json["results"].first
      new_music = Song.new(song_info["trackName"])
      new_music.artist = song_info["artistName"]
      new_music.genre = song_info["primaryGenreName"]
      new_music.album = song_info["collectionName"]
      new_music.year = song_info["releaseDate"].split("-").first.to_i
      return new_music
    end
  end
  nil
end
query() click to toggle source
# File lib/music_meta_fixer/itunes_api.rb, line 13
def query
  encoded_term = CGI.escape(@term)
  ITUNES_API_URL + "term=#{encoded_term}&media=#{MEDIA}&limit=#{@limit}"
end