Class: Pragmatic

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

Class Method Summary collapse

Class Method Details

.mediaObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pragmatic.rb', line 19

def self.media
  uri = URI 'http://pragprog.com/categories/all'
  doc = Nokogiri::HTML(open uri) 

  works = doc.css('.thumbnail')
  titles =  works.collect{|w| w.css('img').first.attributes['alt'].text.gsub('Cover Image For ','').gsub('...','') } 
  covers = works.collect{|w| w.css("img").first.attributes['src'].text}
  urls = works.collect{|w| w.css("a").first.attributes['href'].text}
  books = titles.zip(covers,urls).inject([]) do |books, book| 
    books << Book.new(*book) 
  end 

  JSON.parse(books.to_json) 
end