Class: DictionaryScraper

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

Constant Summary collapse

@@word =
""
@@definition =
""

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDictionaryScraper

Returns a new instance of DictionaryScraper.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/words_and_idioms/dictionary_scraper.rb', line 11

def initialize
  begin
    @@word = UserInput.get_word
    word_url = "http://www.dictionary.com/browse/#{@@word}"
    html = open(word_url)
    doc = Nokogiri::HTML(html)

  rescue OpenURI::HTTPError
    DictionaryScraper.check_spelling

  else
    definition_xml = doc.css(".def-content")[0].text
    definition_init_parse = definition_xml.split("\r\n")
    @@definition = definition_init_parse[1].delete("\n").strip
    puts " "
    puts " "
    puts "According to dictionary.com #{@@word} means:".green
    puts "#{@@definition}"
    puts " "
    puts " "
  end
end

Instance Attribute Details

#user_inputObject

Returns the value of attribute user_input.



6
7
8
# File 'lib/words_and_idioms/dictionary_scraper.rb', line 6

def user_input
  @user_input
end

Class Method Details

.check_spellingObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/words_and_idioms/dictionary_scraper.rb', line 34

def self.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 " "
      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 == "Y"
      @correct_word = @correct_spelling
      corrected_url = "http://www.dictionary.com/browse/#{@correct_word}"
      corrected_html = open(corrected_url)
      corrected_doc = Nokogiri::HTML(corrected_html)
      corrected_definition_xml = corrected_doc.css(".def-content")[0].text
      corrected_definition_init_parse = corrected_definition_xml.split("\r\n")
      @corrected_definition = corrected_definition_init_parse[1].delete("\n").strip
      puts "According to dictionary.com #{@correct_word} means:".green
      puts "#{@corrected_definition}"
    else
      puts " "
      puts " "
      puts "Well, then. Perhaps we can try again.".green
      puts " "
      puts " "
    end
  end
end