Class: Wordnik

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/wordnik.rb

Constant Summary

PARTS_OF_SPEECH =
[:noun, :verb, :adjective, :adverb, :idiom, :article, :abbreviation,
:preposition, :prefix, :interjection, :suffix, :verb-transitive", :verb-intransitive"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil) ⇒ Wordnik



13
14
15
16
17
18
# File 'lib/wordnik.rb', line 13

def initialize(api_key = nil)
  @api_key = (api_key || File.read('.api-key').strip || '').dup
  unless @api_key.blank?
    self.class.default_params :api_key => @api_key
  end
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key



9
10
11
# File 'lib/wordnik.rb', line 9

def api_key
  @api_key
end

Instance Method Details

#autocomplete(word_fragment, count = 100) ⇒ Object



46
47
48
# File 'lib/wordnik.rb', line 46

def autocomplete(word_fragment, count = 100)
  do_request("suggest.json/#{word_fragment.downcase}", :count => count)
end

#define(word, *args) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/wordnik.rb', line 30

def define(word, *args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  unless options.key?(:count)
    options[:count] = args.any? ? args.first.to_i : 100
  end
  do_request("word.json/#{word.downcase}/definitions", options)
end

#examples(word) ⇒ Object



42
43
44
# File 'lib/wordnik.rb', line 42

def examples(word)
  do_request("word.json/#{word.downcase}/examples")
end

#frequency(word) ⇒ Object



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

def frequency(word)
  do_request("word.json/#{word.downcase}/frequency")
end

#lookup(word) ⇒ Object



26
27
28
# File 'lib/wordnik.rb', line 26

def lookup(word)
  do_request("word.json/#{word.downcase}")
end

#punctuation(word) ⇒ Object



62
63
64
# File 'lib/wordnik.rb', line 62

def punctuation(word)
  do_request("word.json/#{word.downcase}/punctuationFactor")
end

#random(has_definition = true, limit = 1) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/wordnik.rb', line 54

def random(has_definition = true, limit = 1)
  if limit == 1
    do_request("words.json/randomWord", :hasDictionaryDef => has_definition)
  else
    do_request("words.json/randomWords", {:hasDictionaryDef => has_definition, :limit => limit})
  end
end

#word_of_the_dayObject



50
51
52
# File 'lib/wordnik.rb', line 50

def word_of_the_day
  do_request("wordoftheday.json")
end