class Quotes500::HTMLParser
HTMLParser
loads a html source file from url, parsing data you need
Constants
- SELECTOR_AUTHOR
- SELECTOR_AUTHOR_IMG_URL
- SELECTOR_QUOTE
- SELECTOR_RANK
Attributes
count[R]
page[R]
quotes[R]
targetSelector[R]
Public Class Methods
new(url, pageNumber)
click to toggle source
# File lib/quotes500.rb, line 27 def initialize(url, pageNumber) @document = Nokogiri::HTML(open(url+"#{pageNumber}/")) @page = pageNumber @count = @document.css('div.blog-quote').count @targetSelector = @document.css('blog-posts') @quotes = [] end
Public Instance Methods
retrieveQuotes()
click to toggle source
Retrieve 10 quotes from one html source return an array containg hashes
# File lib/quotes500.rb, line 37 def retrieveQuotes if @targetSelector.respond_to?("css") @count.times do |i| author = @document.css(SELECTOR_AUTHOR)[i].text.strip if @page > 1 rank = @document.css(SELECTOR_RANK)[i].text.to_i else rank = @document.css(SELECTOR_RANK)[i].text.to_i end content = @document.css(SELECTOR_QUOTE)[i-0].text.strip url = @document.css(SELECTOR_AUTHOR_IMG_URL)[i-0]['src'].strip quote = {:author => author, :rank => rank, :content => content, :imageSrc => url} @quotes << quote end else puts "no" end end