Class: BandCamp::Page
- Inherits:
-
Object
- Object
- BandCamp::Page
- Defined in:
- lib/band_camp/page.rb
Instance Method Summary collapse
- #album_name ⇒ Object
- #band_name ⇒ Object
- #download ⇒ Object
- #extend_relative_url(relative_url) ⇒ Object
- #harmony_page ⇒ Object
- #hpricot ⇒ Object
-
#initialize(url, options = {}) ⇒ Page
constructor
A new instance of Page.
- #is_album? ⇒ Boolean
- #page_html ⇒ Object
- #parsed_url ⇒ Object
- #path_for_download ⇒ Object
- #songs ⇒ Object
- #urls_of_albums_on_this_page ⇒ Object
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 = @url = url end |
Instance Method Details
#album_name ⇒ Object
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_name ⇒ Object
48 49 50 |
# File 'lib/band_camp/page.rb', line 48 def band_name @band_name ||= harmony_page.execute_js("BandData.name") end |
#download ⇒ Object
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_page ⇒ Object
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 |
#hpricot ⇒ Object
80 81 82 |
# File 'lib/band_camp/page.rb', line 80 def hpricot @hpricot ||= Hpricot.parse(page_html) end |
#is_album? ⇒ 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_html ⇒ Object
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_url ⇒ Object
21 22 23 |
# File 'lib/band_camp/page.rb', line 21 def parsed_url @parsed_url ||= URI.parse(@url) end |
#path_for_download ⇒ Object
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 |
#songs ⇒ Object
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_page ⇒ Object
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 |