class BrainyQuote::Quote

Attributes

author[RW]
text[RW]
topic_name[R]

Public Class Methods

new(topic_name) click to toggle source
# File lib/brainyquote/quote.rb, line 5
def initialize(topic_name)
  @topic_name = topic_name
end
quote_about(topic_name) click to toggle source
# File lib/brainyquote/quote.rb, line 43
def quote_about(topic_name)
  quote_object = self.new(topic_name)
  quote_data = quote_object.data_from_topic

  quote_object.text = quote_data.first
  quote_object.author = quote_data.last
  quote_object
end
topics() click to toggle source
# File lib/brainyquote/quote.rb, line 52
def topics
  topics = []
  url = 'http://www.brainyquote.com/quotes/topics.html'
  doc = Nokogiri::HTML(open(url))

  doc.css('table .bqLn a').each do |link|
    topics << link.text
  end

  topics
end

Public Instance Methods

data_from_topic() click to toggle source
# File lib/brainyquote/quote.rb, line 9
def data_from_topic
  text_and_author = []
  doc = Nokogiri::HTML(open(random_pagination_url))

  #Retrieve text/author pairs.
  doc.css('.bqQt').each do |data|
    text_and_author << data.text.split("\n").reject!(&:empty?).take(2)
  end

  #Randomly selectand return a scraped quote/author.
  index = (rand(text_and_author.length) - 1)
  text_and_author[index]
end

Private Instance Methods

random_pagination_url() click to toggle source
# File lib/brainyquote/quote.rb, line 24
def random_pagination_url
  #Prepare basic topic_url
  page_name = @topic_name.split(/\W/).join.downcase
  url = "http://www.brainyquote.com/quotes/topics/topic_#{page_name}.html"

  doc = Nokogiri::HTML(open(url))

  #Find number of available pages for topics and pick one at random
  max = doc.css('ul.pagination').first.css('li:nth-last-child(2)').text.to_i
  page = rand(max)

  #Render url for selected page
  "http://www.brainyquote.com/quotes/topics/topic_#{page_name}#{page}.html"
end