Class: BillboardChartScraper

Inherits:
Object
  • Object
show all
Defined in:
lib/billboard_chart/billboard_chart_scraper.rb

Overview

encoding: utf-8

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ BillboardChartScraper

Returns a new instance of BillboardChartScraper.



9
10
11
12
# File 'lib/billboard_chart/billboard_chart_scraper.rb', line 9

def initialize(url)
  @scraped_data = Nokogiri::HTML(open(url))
  top_ten
end

Instance Attribute Details

#scraped_dataObject

Returns the value of attribute scraped_data.



7
8
9
# File 'lib/billboard_chart/billboard_chart_scraper.rb', line 7

def scraped_data
  @scraped_data
end

Instance Method Details

#get_artistsObject

Scrape Methods ============= #


16
17
18
# File 'lib/billboard_chart/billboard_chart_scraper.rb', line 16

def get_artists
  @scraped_data.css("p.chart_info a").collect {|artist| artist.text}
end

#get_singlesObject



20
21
22
# File 'lib/billboard_chart/billboard_chart_scraper.rb', line 20

def get_singles
  @scraped_data.css("h1").collect {|single| single.text}[1..10]
end

#merge_artists_and_singlesObject



24
25
26
# File 'lib/billboard_chart/billboard_chart_scraper.rb', line 24

def merge_artists_and_singles
  Hash[get_artists.zip(get_singles)]
end

#top_tenObject

Display Methods ============= #


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/billboard_chart/billboard_chart_scraper.rb', line 30

def top_ten
  puts "\n"
  puts "✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭".yellow
  puts "\n"
  puts "      ♫  Top 10 Billboard Hot 100 Singles Chart: ♫    "
  puts "                  (#{DateTime.now.strftime('%m/%d/%Y')})             ".red
  puts "\n"
  i = 0
  while i < 10
    merge_artists_and_singles.each do |artist, single|
      puts "#{i+1}. #{artist} - #{single}"
      i += 1
    end
      puts "\n"
      puts "✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭ ✭".yellow
      puts "\n"
  end
end