class MetalArchives::Agent

Constants

ALBUM_URL_AND_NAME_REGEXP
BAND_NAME_AND_COUNTRY_REGEXP
NO_BAND_REGEXP
RELEASE_DATE_REGEXP
SITE_URL

Public Class Methods

new(year=Time.now.year) click to toggle source

An agent accesses the website and holds the HTML source.

# File lib/metal-archives.rb, line 13
def initialize(year=Time.now.year)
  @year = year
  @total_results = 0
end

Public Instance Methods

album_name(album) click to toggle source
# File lib/metal-archives.rb, line 53
def album_name(album)
  album[1].match(ALBUM_URL_AND_NAME_REGEXP)[2]
end
album_url(album) click to toggle source
# File lib/metal-archives.rb, line 49
def album_url(album)
  album[1].match(ALBUM_URL_AND_NAME_REGEXP)[1]
end
band_name(album) click to toggle source
# File lib/metal-archives.rb, line 41
def band_name(album)
  band_array(album)[3].match(BAND_NAME_AND_COUNTRY_REGEXP)[1]
end
band_url(album) click to toggle source
# File lib/metal-archives.rb, line 37
def band_url(album)
  band_array(album)[1]
end
country(album) click to toggle source
# File lib/metal-archives.rb, line 45
def country(album)
  band_array(album)[3].match(BAND_NAME_AND_COUNTRY_REGEXP)[2]
end
paginated_albums() click to toggle source

Finds all the url to the search results pages as they are paginated.

# File lib/metal-archives.rb, line 27
def paginated_albums
  albums = []
  (total_albums / 100 + 1).times do |i|
    display_start = i * 100
    results = json_results("http://www.metal-archives.com/search/ajax-advanced/searching/albums/?&releaseYearFrom=#{@year}&releaseMonthFrom=1&releaseYearTo=#{@year}&releaseMonthTo=12&_=1&sEcho=0&iColumns=4&sColumns=&iDisplayStart=#{display_start}&iDisplayLength=100&sNames=%2C%2C%2C")
    albums << results['aaData']
  end
  albums
end
release_date(album) click to toggle source
# File lib/metal-archives.rb, line 61
def release_date(album)
  release_date_string = album[3].match(RELEASE_DATE_REGEXP)[1] # "2011-04-00"
  year, month, day = release_date_string.split('-')
  (day == '00') ? Date.civil(year.to_i, month.to_i, -1) : release_date = Date.parse(release_date_string)
end
release_type(album) click to toggle source
# File lib/metal-archives.rb, line 57
def release_type(album)
  album[2]
end
total_albums() click to toggle source

Find the total results to search through and memoize it.

# File lib/metal-archives.rb, line 19
def total_albums
  return @total_results if @total_results > 0
  results = json_results("http://www.metal-archives.com/search/ajax-advanced/searching/albums/?&releaseYearFrom=#{@year}&releaseMonthFrom=1&releaseYearTo=#{@year}&releaseMonthTo=12&_=1&sEcho=0&iColumns=4&sColumns=&iDisplayStart=1&iDisplayLength=100&sNames=%2C%2C%2C")
  @total_results = results['iTotalRecords']
  @total_results
end

Private Instance Methods

band_array(album) click to toggle source
# File lib/metal-archives.rb, line 75
def band_array(album)
  album[0].split('"')
end
json_results(url) click to toggle source
# File lib/metal-archives.rb, line 69
def json_results(url)
  response = Net::HTTP.get_response(URI.parse(url))
  data = response.body
  JSON.parse(data)
end