class DictionaryRB::Urban
Parses the page for a word at {www.urbandictionary.com/ 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 {PREFIX ENDPOINT} so as to make it quickly generate the other results, once a URL is parsed.
Constants
- PREFIX
Endpoint for
Urban
Dictionary
Attributes
The associated word
Public Class Methods
@param word [String] The word for Urban
Dictionary
@example
word = DictionaryRB::Urban.new('Krunal')
# File lib/dictionary-rb/urban.rb, line 17 def initialize(word) @word = word if word.is_a? String @word = word.word if word.is_a? Word end
Public Instance Methods
Fetches and gives the examples for the word. @example
word.examples #=> ["I hate that guy, he is a krunal", "Hot chick - God i want ur Krunalness\rKrunal - I know...",]
@return [Array] containing the examples
# 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
Fetches and gives the first meaning of the word. @example
word.meaning #=> "A fuck, nothing more, just a fuck"
@see meanings
@return [String] containing meaning for the word
# File lib/dictionary-rb/urban.rb, line 28 def meaning meanings.first end
Fetches and gives meanings for the word from Urban
Dictionary
@example
word.meanings #=> ["A fuck, nothing more, just a fuck", "Describes someone as being the sexiest beast alive. Anyone who is blessed with the name Krunal should get a medal.",..]
@see meaning
@return [Array] containing the meanings for the word.
# 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
Fetches and gives synonyms for the word. @example
word.synonyms #=> ["agam", "indian", "kerpal",.. ]
@see synonyms
@return [Array] containing synonyms for the word
# 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
# File lib/dictionary-rb/urban.rb, line 76 def to_s sprintf("Urban Dictionary (word: %s, meaning: %s)", @word, @meaning) end