Class: WordLookUp::DictionaryScraper
- Inherits:
-
Object
- Object
- WordLookUp::DictionaryScraper
- Defined in:
- lib/word_look_up/dictionary_scraper.rb
Instance Attribute Summary collapse
-
#words_and_definitions ⇒ Object
Returns the value of attribute words_and_definitions.
-
#words_list ⇒ Object
Returns the value of attribute words_list.
Instance Method Summary collapse
- #find_form(page) ⇒ Object
- #get_definitions ⇒ Object
- #get_definitions_for(word) ⇒ Object
- #get_definitions_threaded ⇒ Object
- #get_home_page ⇒ Object
-
#initialize(words_to_look_up) ⇒ DictionaryScraper
constructor
A new instance of DictionaryScraper.
- #invalid_word_results(results_page) ⇒ Object
- #process_results(results_page) ⇒ Object
- #search_for(word, form) ⇒ Object
- #valid_word_results(results_page) ⇒ Object
Constructor Details
#initialize(words_to_look_up) ⇒ DictionaryScraper
Returns a new instance of DictionaryScraper.
7 8 9 10 11 |
# File 'lib/word_look_up/dictionary_scraper.rb', line 7 def initialize(words_to_look_up) @words_list = words_to_look_up @web_scraper = Mechanize.new @words_and_definitions = [] end |
Instance Attribute Details
#words_and_definitions ⇒ Object
Returns the value of attribute words_and_definitions.
6 7 8 |
# File 'lib/word_look_up/dictionary_scraper.rb', line 6 def words_and_definitions @words_and_definitions end |
#words_list ⇒ Object
Returns the value of attribute words_list.
6 7 8 |
# File 'lib/word_look_up/dictionary_scraper.rb', line 6 def words_list @words_list end |
Instance Method Details
#find_form(page) ⇒ Object
43 44 45 |
# File 'lib/word_look_up/dictionary_scraper.rb', line 43 def find_form(page) page.forms.first end |
#get_definitions ⇒ Object
13 14 15 16 17 |
# File 'lib/word_look_up/dictionary_scraper.rb', line 13 def get_definitions @words_list.each do |word| get_definitions_for(word) end end |
#get_definitions_for(word) ⇒ Object
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/word_look_up/dictionary_scraper.rb', line 28 def get_definitions_for(word) home_page = get_home_page form = find_form(home_page) begin results_page = search_for(word, form) @words_and_definitions << WordLookUp::WordAndDefinition.new(word, process_results(results_page)) rescue Mechanize::ResponseCodeError => e @words_and_definitions << WordLookUp::WordAndDefinition.new(word, [false, ["no suggestions were found for #{word}"]]) end end |
#get_definitions_threaded ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/word_look_up/dictionary_scraper.rb', line 19 def get_definitions_threaded threads = @words_list.map do |word_to_look_up| Thread.new(word_to_look_up) do |word| get_definitions_for(word) end end threads.each { |thr| thr.join } end |
#get_home_page ⇒ Object
39 40 41 |
# File 'lib/word_look_up/dictionary_scraper.rb', line 39 def get_home_page @web_scraper.get('http://dictionary.cambridge.org') end |
#invalid_word_results(results_page) ⇒ Object
70 71 72 73 74 75 76 77 78 |
# File 'lib/word_look_up/dictionary_scraper.rb', line 70 def invalid_word_results(results_page) did_you_means = [] results_page.search('#cdo-spellcheck').each do |suggestions| suggestions.search('.cdo-link').each do |suggestion| did_you_means << suggestion.text.strip end end did_you_means end |
#process_results(results_page) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/word_look_up/dictionary_scraper.rb', line 52 def process_results(results_page) if results_page.search('.def-block').length > 0 [true, valid_word_results(results_page)] else [false, invalid_word_results(results_page)] end end |
#search_for(word, form) ⇒ Object
47 48 49 50 |
# File 'lib/word_look_up/dictionary_scraper.rb', line 47 def search_for(word, form) form['q'] = word form.submit end |
#valid_word_results(results_page) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/word_look_up/dictionary_scraper.rb', line 60 def valid_word_results(results_page) definitions = [] results_page.search('.def-block').each do |h2| h2.search('.def').each do |definition| definitions << definition.text.strip end end definitions end |