Class: Metanym

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

Constant Summary collapse

VERSION =
"0.1.0"
URL =
"http://thesaurus.com/browse/"

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Metanym

Returns a new instance of Metanym.



10
11
12
# File 'lib/metanym.rb', line 10

def initialize(query)
  @url = URI.encode(URL + query)
end

Instance Method Details

#antonymsObject



18
19
20
# File 'lib/metanym.rb', line 18

def antonyms
  @antonyms ||= items_at 'Antonyms'
end

#definitionObject



22
23
24
25
26
# File 'lib/metanym.rb', line 22

def definition
  @definition ||= doc.at_xpath('//td[.="Definition:"]/following::td').text
rescue NoMethodError
  nil
end

#docObject



34
35
36
# File 'lib/metanym.rb', line 34

def doc
  @doc ||= Nokogiri.HTML source
end

#items_at(head) ⇒ Object



28
29
30
31
32
# File 'lib/metanym.rb', line 28

def items_at(head)
  doc.at_xpath("//td[.='#{head}:']/following-sibling::td").text.strip.split(/[\n,]\s*/)
rescue NoMethodError
  []
end

#sourceObject



38
39
40
# File 'lib/metanym.rb', line 38

def source
  Excon.get(@url, :expects => 200).body
end

#synonymsObject



14
15
16
# File 'lib/metanym.rb', line 14

def synonyms
  @synonyms ||= items_at 'Synonyms'
end