class DictionaryScraper

Attributes

user_input[RW]

Public Class Methods

new() click to toggle source
# File lib/words_and_idioms/dictionary_scraper.rb, line 9
def initialize
  begin
    @@word = @@correct_spelling || UserInput.get_word
    word_url = "http://www.dictionary.com/browse/#{@@word}"
    html = open(word_url)
    @@doc = Nokogiri::HTML(html)
  rescue OpenURI::HTTPError
    check_spelling
  else
    print_definition
  end
end

Public Instance Methods

check_spelling() click to toggle source
# File lib/words_and_idioms/dictionary_scraper.rb, line 33
def check_spelling
  begin
    check_url = "http://www.macmillandictionary.com/spellcheck/british/?q=#{@@word}"
    check_html = open(check_url)
    check_doc = Nokogiri::HTML(check_html)
    @@correct_spelling = check_doc.css("#search-results")[1].children.children[1].children.children.text
  rescue NoMethodError
    puts "Hmmm... That's a tough one. I can't find that one anywhere. Maybe you can add it to the open dictionary!".red
    puts " "
  else
    puts "Hmmm... I can't find that one in the dictionary. Did you mean #{@@correct_spelling.upcase}?".yellow
    puts "Y/N?:"
      if gets.chomp.downcase == "y"
        DictionaryScraper.new
      else
        puts " "
        puts "Well, then. Perhaps we can try again.".green
        puts " "
      end
  end
end
print_definition() click to toggle source