Class: RandomWordByLength::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/random_word_by_length/scraper.rb

Class Method Summary collapse

Class Method Details

.get_easy_wordObject

5 or fewer letters



32
33
34
35
36
37
38
39
40
# File 'lib/random_word_by_length/scraper.rb', line 32

def self.get_easy_word
  easy_words = []
  self.get_words.each do |i|
    if i.length <= 17
    easy_words << i
    end
  end
  easy_words.sample.delete_prefix("/dictionary/")
end

.get_hard_wordObject

Greater than 10 letters



52
53
54
55
56
57
58
59
60
# File 'lib/random_word_by_length/scraper.rb', line 52

def self.get_hard_word
  hard_words = []
  self.get_words.each do |i|
    if i.length > 22
    hard_words << i
    end
  end
  hard_words.sample.delete_prefix("/dictionary/")
end

.get_medium_wordObject

5 or greater up to and including 10 letters



42
43
44
45
46
47
48
49
50
# File 'lib/random_word_by_length/scraper.rb', line 42

def self.get_medium_word
  medium_words = []
  self.get_words.each do |i|
    if i.length.between?(18, 22)
    medium_words << i
    end
  end
  medium_words.sample.delete_prefix("/dictionary/")
end

.get_word_attributes(word) ⇒ Object

I should return an array of attributes



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/random_word_by_length/scraper.rb', line 62

def self.get_word_attributes(word)
  url = "https://www.merriam-webster.com/dictionary/#{word.gsub(" ", "%20")}"
  doc = Nokogiri::HTML(open(url))
  #Conditional handles when a word is a variant spelling or a plural of a parent-word by redirecting to the parent word.
  if doc.search(".dtText").text == ""
    parent_word = doc.search("a.cxt.text-uppercase").text
    url = "https://www.merriam-webster.com/dictionary/#{parent_word.gsub(" ", "%20")}"
    doc = Nokogiri::HTML(open(url))
    definition = doc.search(".dtText").text
    kind = doc.search(".important-blue-link").attribute("href").value.delete_prefix("/dictionary/")
  #Conditional handles when the word is a geographical name (place).
  elsif doc.search(".important-blue-link").text == ""
    definition = doc.search(".dtText").text
    kind = doc.search("span.fl").text
  else
    definition = doc.search(".dtText").text
    kind = doc.search(".important-blue-link").attribute("href").value.delete_prefix("/dictionary/")
  end
  attributes = []
  attributes << definition
  attributes << kind
  attributes
end

.get_wordsObject

scrapes all the words in the subpage for the letter



21
22
23
24
25
26
27
28
29
30
# File 'lib/random_word_by_length/scraper.rb', line 21

def self.get_words
  url = self.url_letter_subpage
  doc = Nokogiri::HTML(open(url))
  words = doc.search(".browse-words .entries ul li a")
  words_array = []
  words.each do |i|
    words_array << i.attribute("href").value
  end
  words_array
end

.random_letterObject

a random word needs a random number



3
4
5
# File 'lib/random_word_by_length/scraper.rb', line 3

def self.random_letter
  ('a'..'z').to_a.sample
end

.url_letterObject

gets the main-letter page



7
8
9
10
# File 'lib/random_word_by_length/scraper.rb', line 7

def self.url_letter
  letter = self.random_letter
  url = "https://www.merriam-webster.com/browse/dictionary/#{letter}"
end

.url_letter_subpageObject

gets the sub-page for the letter



12
13
14
15
16
17
18
19
# File 'lib/random_word_by_length/scraper.rb', line 12

def self.url_letter_subpage
  url = self.url_letter
  doc = Nokogiri::HTML(open(url))
  range = doc.search("div.p-wrap.p-wrap-bot.clearfix").css(".counters").text
  range = range.delete("page 1 of ").to_i
  num = (rand(1..range))
  url = url + "/#{num}"
end