Class: MetalArchives::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/metal-archives.rb

Constant Summary collapse

SITE_URL =
'http://metal-archives.com'
NO_BAND_REGEXP =
/span.+<\/span>/
BAND_NAME_AND_COUNTRY_REGEXP =
/(.+)\s{1}\(([a-zA-Z]{2})\)/
ALBUM_URL_AND_NAME_REGEXP =
/"(.+)">(.+)<\/a>/
RELEASE_DATE_REGEXP =
/<!--\s(.{10})\s-->/

Instance Method Summary collapse

Constructor Details

#initialize(year = Time.now.year) ⇒ Agent

An agent accesses the website and holds the HTML source.



13
14
15
16
# File 'lib/metal-archives.rb', line 13

def initialize(year=Time.now.year)
  @year = year
  @total_results = 0
end

Instance Method Details

#album_name(album) ⇒ Object



53
54
55
# File 'lib/metal-archives.rb', line 53

def album_name(album)
  album[1].match(ALBUM_URL_AND_NAME_REGEXP)[2]
end

#album_url(album) ⇒ Object



49
50
51
# File 'lib/metal-archives.rb', line 49

def album_url(album)
  album[1].match(ALBUM_URL_AND_NAME_REGEXP)[1]
end

#band_name(album) ⇒ Object



41
42
43
# 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) ⇒ Object



37
38
39
# File 'lib/metal-archives.rb', line 37

def band_url(album)
  band_array(album)[1]
end

#country(album) ⇒ Object



45
46
47
# File 'lib/metal-archives.rb', line 45

def country(album)
  band_array(album)[3].match(BAND_NAME_AND_COUNTRY_REGEXP)[2]
end

#paginated_albumsObject

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



27
28
29
30
31
32
33
34
35
# 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) ⇒ Object



61
62
63
64
65
# 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) ⇒ Object



57
58
59
# File 'lib/metal-archives.rb', line 57

def release_type(album)
  album[2]
end

#total_albumsObject

Find the total results to search through and memoize it.



19
20
21
22
23
24
# 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