Class: SimpsonsWorld::Scrape

Inherits:
Object
  • Object
show all
Defined in:
lib/simpsons_world/scrape.rb

Class Method Summary collapse

Class Method Details

.allObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/simpsons_world/scrape.rb', line 9

def self.all
  doc = Nokogiri::HTML(open BASE_URL << EPISODES_URL)
  seasons = doc.css(".chapters-wrapper")
  seasons.each_with_index do |season, i|
    number = season.attr 'data-season-number'
    episodes = season.css("ul.items > li").map.with_index(1) { |episode, j|
      [ j,
        {
          title:       episode.css('.category-thumb-expanded .thumbnail-text').text,
          description: clean_description(episode.css('.category-thumb-expanded .thumbnail-extra-text').text),
          url:         episode.css('a')[0]['href']
        }
      ]
    }.to_h
    SimpsonsWorld::Season.new(number, episodes)
  end
end

.clean_description(str) ⇒ Object



27
28
29
# File 'lib/simpsons_world/scrape.rb', line 27

def self.clean_description str
  str.gsub(/SEASON PREMIERE\.|Presented by FXX\./i, '').strip
end