Class: DictionaryRB::Urban

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(word) ⇒ Urban

Returns a new instance of Urban.

Examples:

word = DictionaryRB::Urban.new('Krunal')

Parameters:

  • word (String)

    The word for Urban Dictionary



17
18
19
20
# 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

Instance Attribute Details

#wordObject (readonly)

The associated word



10
11
12
# File 'lib/dictionary-rb/urban.rb', line 10

def word
  @word
end

Instance Method Details

#examplesArray

Fetches and gives the examples for the word.

Examples:

word.examples
#=> ["I hate that guy, he is a krunal", "Hot chick - God i want ur Krunalness\rKrunal - I know...",]

Returns:

  • (Array)

    containing the examples



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

#meaningString

Fetches and gives the first meaning of the word.

Examples:

word.meaning
#=> "A fuck, nothing more, just a fuck"

Returns:

  • (String)

    containing meaning for the word

See Also:



28
29
30
# File 'lib/dictionary-rb/urban.rb', line 28

def meaning
  meanings.first
end

#meaningsArray

Fetches and gives meanings for the word from Urban Dictionary

Examples:

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.",..]

Returns:

  • (Array)

    containing the meanings for the word.

See Also:



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_wordsArray Also known as: synonyms

Fetches and gives synonyms for the word.

Examples:

word.synonyms
#=> ["agam", "indian", "kerpal",.. ]

Returns:

  • (Array)

    containing synonyms for the word

See Also:



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_sObject



76
77
78
# File 'lib/dictionary-rb/urban.rb', line 76

def to_s
  sprintf("Urban Dictionary (word: %s, meaning: %s)", @word, @meaning)
end