Class: DictionaryRB::Urban
- Inherits:
-
Object
- Object
- DictionaryRB::Urban
- Defined in:
- lib/dictionary-rb/urban.rb
Overview
Parses the page for a word at / Urban Dictionary and extracts the #meanings, #examples and #similar_words for it. Lot of take care has been taken to prevent it from hitting the ENDPOINT so as to make it quickly generate the other results, once a URL is parsed.
Constant Summary collapse
- PREFIX =
Endpoint for Urban Dictionary
"http://www.urbandictionary.com/define.php?term="
Instance Attribute Summary collapse
-
#word ⇒ Object
readonly
The associated word.
Instance Method Summary collapse
-
#examples ⇒ Array
Fetches and gives the examples for the word.
-
#initialize(word) ⇒ Urban
constructor
A new instance of Urban.
-
#meaning ⇒ String
Fetches and gives the first meaning of the word.
-
#meanings ⇒ Array
Fetches and gives meanings for the word from Urban Dictionary.
-
#similar_words ⇒ Array
(also: #synonyms)
Fetches and gives synonyms for the word.
- #to_s ⇒ Object
Constructor Details
Instance Attribute Details
#word ⇒ Object (readonly)
The associated word
10 11 12 |
# File 'lib/dictionary-rb/urban.rb', line 10 def word @word end |
Instance Method Details
#examples ⇒ Array
Fetches and gives the examples for the word.
55 56 57 58 59 60 |
# File 'lib/dictionary-rb/urban.rb', line 55 def examples @doc ||= Nokogiri::HTML(open(PREFIX + CGI::escape(@word))) #nodes = @doc.css('div#outer.container div.row.three_columns div.span6 div#content div.box div.inner div.example') nodes = @doc.css('.example') nodes.map(&:text).map(&:strip).reject(&:empty?) end |
#meaning ⇒ String
Fetches and gives the first meaning of the word.
28 29 30 |
# File 'lib/dictionary-rb/urban.rb', line 28 def meaning meanings.first end |
#meanings ⇒ Array
Fetches and gives meanings for the word from Urban Dictionary
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/dictionary-rb/urban.rb', line 39 def meanings url = PREFIX + CGI::escape(@word) @doc ||= Nokogiri::HTML(open(url)) #nodes = @doc.css('div#outer.container div.row.three_columns div.span6 div#content div.box div.inner div.meaning') nodes = @doc.css('.meaning') results = nodes.map(&:text).map(&:strip).reject(&:empty?) @meaning = results.first results end |
#similar_words ⇒ Array Also known as: synonyms
Fetches and gives synonyms for the word.
68 69 70 71 72 |
# File 'lib/dictionary-rb/urban.rb', line 68 def similar_words @doc ||= Nokogiri::HTML(open(PREFIX + CGI::escape(@word))) nodes = @doc.css('.tags a.tag') nodes.map(&:text).reject(&:empty?) end |
#to_s ⇒ Object
76 77 78 |
# File 'lib/dictionary-rb/urban.rb', line 76 def to_s sprintf("Urban Dictionary (word: %s, meaning: %s)", @word, @meaning) end |