Class: DictionaryScraper

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

Constant Summary collapse

@@word =
""
@@definition =
""
@@correct_spelling =
nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDictionaryScraper

Returns a new instance of DictionaryScraper.



9
10
11
12
13
14
15
16
17
18
19
20
# 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

Instance Attribute Details

#user_inputObject

Returns the value of attribute user_input.



3
4
5
# File 'lib/words_and_idioms/dictionary_scraper.rb', line 3

def user_input
  @user_input
end

Instance Method Details

#check_spellingObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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


22
23
24
25
26
27
28
29
30
31
# File 'lib/words_and_idioms/dictionary_scraper.rb', line 22

def print_definition
  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 "According to dictionary.com #{@@word} means:".green
  puts "#{@@definition}"
  puts " "
  @@correct_spelling = nil
end