Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

- depend on shoulda not thoughtbot-shoulda

- better handling of instantiation without api key and setting of api key
- more tests
  • Loading branch information...
commit 714da1fbc7cb0ee5be4007b43f6e563fcf011f44 1 parent bbd7805
@ealdent authored
Showing with 44 additions and 10 deletions.
  1. +1 −1  Rakefile
  2. +5 −1 lib/wordnik/wordnik.rb
  3. +38 −8 test/test_wordnik.rb
View
2  Rakefile
@@ -11,7 +11,7 @@ begin
gem.homepage = "http://github.com/ealdent/wordnik"
gem.authors = ["Jason Adams"]
gem.add_dependency "httparty", ">= 0.4.5"
- gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
+ gem.add_development_dependency "shoulda", ">= 0"
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
View
6 lib/wordnik/wordnik.rb
@@ -8,11 +8,15 @@ class Wordnik
def initialize(api_key = nil)
@api_key = (api_key || File.read('.api-key').strip || '').dup
- self.class.default_params :api_key => @api_key
+ unless @api_key.blank?
+ self.class.default_params :api_key => @api_key
+ end
end
def api_key=(api_key)
@api_key = api_key.dup
+ self.class.default_params :api_key => @api_key
+ nil
end
def lookup(word)
View
46 test/test_wordnik.rb
@@ -4,21 +4,51 @@
# same directory that you run your tests from, or else set the environment
# variable WORDNIK_API_KEY to the api key you wish to use.
class TestWordnik < Test::Unit::TestCase
+ context "a single Wordnik instance" do
+ should "instantiate given an API key" do
+ assert_equal Wordnik::Wordnik.new('TESTTESTETESTTESTSETESTTESTTETST').nil?, false
+ end
+ end
+
context "the Wordnik singleton" do
setup do
- api_key = (File.exists?('.api-key') ? File.read : ENV['WORDNIK_API_KEY']).strip
- raise "No API key available." unless api_key
+ @api_key = (File.exists?('.api-key') ? File.read('.api-key') : ENV['WORDNIK_API_KEY']).strip
+ raise "No API key available." unless @api_key
- @wordnik = Wordnik::Wordnik.instance(api_key)
+ @wordnik = Wordnik::DefaultWordnik.instance
+ @wordnik.api_key = @api_key
+ @test_word = 'test'
end
- should "instantiate given an API key" do
- @wordnik.should true
+ should "make its api-key accessible" do
+ assert_equal @wordnik.api_key, @api_key
+ end
+
+ should "lookup the id for a word" do
+ word = @wordnik.lookup(@test_word)
+
+ assert_equal word.is_a?(Hash), true
+ assert_equal word.empty?, false
+ assert_equal word.member?('id'), true
+ assert_equal word['id'].to_i > 0, true
+ end
+
+ should "lookup definitions for a word" do
+ definitions = @wordnik.define(@test_word)
+
+ assert_equal definitions.is_a?(Array), true
+ assert_equal definitions.empty?, false
+ assert_equal definitions.first.is_a?(Hash), true
+ assert_equal definitions.first.member?('@id'), true
+ end
+
+ should "lookup no more definitions than specified" do
+ definitions = @wordnik.define(@test_word, 2)
+
+ assert_equal definitions.is_a?(Array), true
+ assert_equal definitions.size, 2
end
- should "lookup the id for word"
- should "lookup definitions for a word"
- should "lookup no more definitions than specified"
should "find frequency counts for a word"
should "find examples for a word"
should "autocomplete a word fragment"
Please sign in to comment.
Something went wrong with that request. Please try again.