Class: Railscasts

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

Class Method Summary collapse

Class Method Details

.screencastsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/railscasts.rb', line 21

def self.screencasts
  url = 'http://railscasts.com/'
  doc = Nokogiri::HTML(open(URI url)) 
  num_pages = doc.css(".pagination a")[-2].text.to_i

  i = 0
  screencasts = [] 
  
  while i < num_pages do 
    i += 1 
    url = url.split('?')[0] + "?page=#{i}"
    p url 
    
    doc = Nokogiri::HTML(open(URI url)) 
    doc.css('.episode').each do |dom| 
      title = dom.css('.main h2 a').text
      image = dom.css('.screenshot img').first.attributes['src'].text
      number = dom.css('.number').text.split("Episode #")[-1]
      published_at = dom.css('.published_at').text
      comments_count = dom.css('.comments a').text.split(" ")[0]
      screencasts << Screencast.new(title,image,number,published_at,comments_count) 
    end 
  end 

  return screencasts.to_json
end