Class: BandCamp::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/band_camp/page.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Page

Returns a new instance of Page.



8
9
10
11
# File 'lib/band_camp/page.rb', line 8

def initialize(url, options = {})
  @options = options
  @url = url
end

Instance Method Details

#album_nameObject



52
53
54
# File 'lib/band_camp/page.rb', line 52

def album_name
  @album_name ||= harmony_page.execute_js("TralbumData.current.title")
end

#band_nameObject



48
49
50
# File 'lib/band_camp/page.rb', line 48

def band_name
  @band_name ||= harmony_page.execute_js("BandData.name")
end

#downloadObject



60
61
62
63
64
65
66
# File 'lib/band_camp/page.rb', line 60

def download
  if is_album?
    download_songs_for_this_album
  else
    download_albums_on_this_page
  end
end

#extend_relative_url(relative_url) ⇒ Object



68
69
70
71
72
73
# File 'lib/band_camp/page.rb', line 68

def extend_relative_url(relative_url)
  url = URI.parse(relative_url)
  url.scheme = "http"
  url.host = parsed_url.host
  url.to_s
end

#harmony_pageObject



25
26
27
28
29
30
31
32
33
# File 'lib/band_camp/page.rb', line 25

def harmony_page
  unless @harmony_page
    # Assigning to a variable first to make the puts statements make sense
    html = page_html
    puts "Initializing headless browser" if @options[:debug]
    @harmony_page = Harmony::Page.new(html)
  end
  @harmony_page
end

#hpricotObject



80
81
82
# File 'lib/band_camp/page.rb', line 80

def hpricot
  @hpricot ||= Hpricot.parse(page_html)
end

#is_album?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/band_camp/page.rb', line 35

def is_album?
  harmony_page.execute_js("TralbumData.current")
  return true
rescue Johnson::Error
  return false
end

#page_htmlObject



13
14
15
16
17
18
19
# File 'lib/band_camp/page.rb', line 13

def page_html
  unless @page_html
    puts "Getting content of \"#{@url}\"" if @options[:debug]
    @page_html = Net::HTTP.get(parsed_url)
  end
  @page_html
end

#parsed_urlObject



21
22
23
# File 'lib/band_camp/page.rb', line 21

def parsed_url
  @parsed_url ||= URI.parse(@url)
end

#path_for_downloadObject



75
76
77
78
# File 'lib/band_camp/page.rb', line 75

def path_for_download
  File.join(BandCamp::file_safe_string(band_name),
            BandCamp::file_safe_string(album_name))
end

#songsObject



42
43
44
45
46
# File 'lib/band_camp/page.rb', line 42

def songs
  @songs ||= harmony_page.execute_js("TralbumData.trackinfo").map do |song_object|
    Song.new(song_object.title.to_s, song_object.title_link.to_s, song_object.file.to_s, @options)
  end
end

#urls_of_albums_on_this_pageObject



56
57
58
# File 'lib/band_camp/page.rb', line 56

def urls_of_albums_on_this_page
  hpricot.search(".ipCell h1 a").map { |a| extend_relative_url(a.attributes["href"]) }
end