wordnik 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +8 -2
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/wordnik.rb +88 -3
- data/test/test_wordnik.rb +84 -2
- data/wordnik.gemspec +12 -9
- metadata +27 -16
- data/lib/wordnik/word.rb +0 -35
- data/lib/wordnik/wordnik.rb +0 -61
data/CHANGELOG
CHANGED
data/Rakefile
CHANGED
@@ -11,7 +11,7 @@ begin
|
|
11
11
|
gem.homepage = "http://github.com/ealdent/wordnik"
|
12
12
|
gem.authors = ["Jason Adams"]
|
13
13
|
gem.add_dependency "httparty", ">= 0.4.5"
|
14
|
-
gem.add_development_dependency "
|
14
|
+
gem.add_development_dependency "shoulda", ">= 0"
|
15
15
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
16
|
end
|
17
17
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/wordnik.rb
CHANGED
@@ -1,4 +1,89 @@
|
|
1
1
|
require 'httparty'
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
|
3
|
+
class Wordnik
|
4
|
+
include HTTParty
|
5
|
+
|
6
|
+
attr_reader :api_key
|
7
|
+
|
8
|
+
base_uri 'http://api.wordnik.com/api'
|
9
|
+
|
10
|
+
def initialize(api_key = nil)
|
11
|
+
@api_key = (api_key || File.read('.api-key').strip || '').dup
|
12
|
+
unless @api_key.blank?
|
13
|
+
self.class.default_params :api_key => @api_key
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def api_key=(api_key)
|
18
|
+
@api_key = api_key.to_s.dup
|
19
|
+
self.class.default_params :api_key => @api_key
|
20
|
+
@api_key
|
21
|
+
end
|
22
|
+
|
23
|
+
def lookup(word)
|
24
|
+
do_request("word.json/#{word.downcase}")
|
25
|
+
end
|
26
|
+
|
27
|
+
def define(word, *args)
|
28
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
29
|
+
unless options.key?(:count)
|
30
|
+
options[:count] = args.any? ? args.first.to_i : 100
|
31
|
+
end
|
32
|
+
do_request("word.json/#{word.downcase}/definitions", options)
|
33
|
+
end
|
34
|
+
|
35
|
+
def frequency(word)
|
36
|
+
do_request("word.json/#{word.downcase}/frequency")
|
37
|
+
end
|
38
|
+
|
39
|
+
def examples(word)
|
40
|
+
do_request("word.json/#{word.downcase}/examples")
|
41
|
+
end
|
42
|
+
|
43
|
+
def autocomplete(word_fragment, count = 100)
|
44
|
+
do_request("suggest.json/#{word_fragment.downcase}", :count => count)
|
45
|
+
end
|
46
|
+
|
47
|
+
def word_of_the_day
|
48
|
+
do_request("wordoftheday.json")
|
49
|
+
end
|
50
|
+
|
51
|
+
def random(has_definition = true)
|
52
|
+
do_request("words.json/randomWord", :hasDictionaryDef => has_definition)
|
53
|
+
end
|
54
|
+
|
55
|
+
def punctuation(word)
|
56
|
+
do_request("word.json/#{word.downcase}/punctuationFactor")
|
57
|
+
end
|
58
|
+
|
59
|
+
protected
|
60
|
+
|
61
|
+
def do_request(request, options = {})
|
62
|
+
handle_result(self.class.get("/#{request}", sanitize_options(options)))
|
63
|
+
end
|
64
|
+
|
65
|
+
def handle_result(result)
|
66
|
+
if result.is_a?(String)
|
67
|
+
raise 'Error in result.'
|
68
|
+
elsif result.is_a?(Hash) && result.key?('type') && result['type'] == 'error'
|
69
|
+
raise "Error in result: #{result['message']}"
|
70
|
+
else
|
71
|
+
result
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def sanitize_options(opts)
|
76
|
+
options = {}
|
77
|
+
|
78
|
+
options[:count] = opts[:count].to_i if opts.key?(:count)
|
79
|
+
options[:hasDictionaryDef] = !!opts[:hasDictionaryDef] if opts.key?(:hasDictionaryDef)
|
80
|
+
|
81
|
+
if opts.key?(:partOfSpeech)
|
82
|
+
options[:partOfSpeech] = opts[:partOfSpeech].reject do |pos|
|
83
|
+
![:noun, :verb, :adjective, :adverb, :idiom, :article, :abbreviation, :preposition, :prefix, :interjection, :suffix].include?(pos.to_sym)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
options
|
88
|
+
end
|
89
|
+
end
|
data/test/test_wordnik.rb
CHANGED
@@ -1,7 +1,89 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
|
+
# In order to run any tests, you must copy your API key into a file in the
|
4
|
+
# same directory that you run your tests from, or else set the environment
|
5
|
+
# variable WORDNIK_API_KEY to the api key you wish to use.
|
3
6
|
class TestWordnik < Test::Unit::TestCase
|
4
|
-
|
5
|
-
|
7
|
+
context "initializing a Wordnik object" do
|
8
|
+
should "instantiate given an API key" do
|
9
|
+
assert_equal Wordnik.new('TESTTESTETESTTESTSETESTTESTTETST').nil?, false
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context "a single Wordnik instance" do
|
14
|
+
setup do
|
15
|
+
@api_key = (File.exists?('.api-key') ? File.read('.api-key') : ENV['WORDNIK_API_KEY']).strip
|
16
|
+
raise "No API key available." unless @api_key
|
17
|
+
|
18
|
+
@wordnik = Wordnik.new(@api_key)
|
19
|
+
@test_word = 'test'
|
20
|
+
@test_word_fragment = 'invas'
|
21
|
+
end
|
22
|
+
|
23
|
+
should "make its api-key accessible" do
|
24
|
+
assert_equal @wordnik.api_key, @api_key
|
25
|
+
end
|
26
|
+
|
27
|
+
should "lookup the id for a word" do
|
28
|
+
word = @wordnik.lookup(@test_word)
|
29
|
+
|
30
|
+
assert_equal word.is_a?(Hash), true
|
31
|
+
assert_equal word.empty?, false
|
32
|
+
assert_equal word.member?('id'), true
|
33
|
+
assert_equal word['id'].to_i > 0, true
|
34
|
+
end
|
35
|
+
|
36
|
+
should "lookup definitions for a word" do
|
37
|
+
definitions = @wordnik.define(@test_word)
|
38
|
+
|
39
|
+
assert_equal definitions.is_a?(Array), true
|
40
|
+
assert_equal definitions.empty?, false
|
41
|
+
assert_equal definitions.first.is_a?(Hash), true
|
42
|
+
assert_equal definitions.first.member?('id'), true
|
43
|
+
end
|
44
|
+
|
45
|
+
should "find frequency counts for a word" do
|
46
|
+
frequency = @wordnik.frequency(@test_word)
|
47
|
+
|
48
|
+
assert_equal frequency.is_a?(Hash), true
|
49
|
+
assert_equal frequency.member?('frequency'), true
|
50
|
+
end
|
51
|
+
|
52
|
+
should "find examples for a word" do
|
53
|
+
examples = @wordnik.examples(@test_word)
|
54
|
+
|
55
|
+
assert_equal examples.is_a?(Array), true
|
56
|
+
assert_equal examples.empty?, false
|
57
|
+
end
|
58
|
+
|
59
|
+
should "autocomplete a word fragment" do
|
60
|
+
suggestions = @wordnik.autocomplete(@test_word_fragment)
|
61
|
+
|
62
|
+
assert_equal suggestions.is_a?(Hash), true
|
63
|
+
assert_equal suggestions['match'].is_a?(Array), true
|
64
|
+
assert_equal suggestions['match'].empty?, false
|
65
|
+
end
|
66
|
+
|
67
|
+
should "get the word of the day" do
|
68
|
+
word = @wordnik.word_of_the_day
|
69
|
+
|
70
|
+
assert_equal word.is_a?(Hash), true
|
71
|
+
assert_equal word.member?('wordstring'), true
|
72
|
+
end
|
73
|
+
|
74
|
+
should "get a random word" do
|
75
|
+
word = @wordnik.random
|
76
|
+
|
77
|
+
assert_equal word.is_a?(Hash), true
|
78
|
+
assert_equal word.member?('wordstring'), true
|
79
|
+
end
|
80
|
+
|
81
|
+
should "get punctuation info for a word" do
|
82
|
+
punctuation = @wordnik.punctuation(@test_word)
|
83
|
+
|
84
|
+
assert_equal punctuation.is_a?(Hash), true
|
85
|
+
assert_equal punctuation.key?('wordId'), true
|
86
|
+
assert_equal punctuation.key?('periodCount'), true
|
87
|
+
end
|
6
88
|
end
|
7
89
|
end
|
data/wordnik.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{wordnik}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jason Adams"]
|
12
|
-
s.date = %q{
|
12
|
+
s.date = %q{2010-03-12}
|
13
13
|
s.description = %q{Ruby interface to the Wordnik API. Details at http://docs.wordnik.com/api/methods.}
|
14
14
|
s.email = %q{jasonmadams@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -19,20 +19,20 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
".gitignore",
|
22
|
+
"CHANGELOG",
|
22
23
|
"LICENSE",
|
23
24
|
"README.rdoc",
|
24
25
|
"Rakefile",
|
25
26
|
"VERSION",
|
26
27
|
"lib/wordnik.rb",
|
27
|
-
"lib/wordnik/word.rb",
|
28
|
-
"lib/wordnik/wordnik.rb",
|
29
28
|
"test/helper.rb",
|
30
|
-
"test/test_wordnik.rb"
|
29
|
+
"test/test_wordnik.rb",
|
30
|
+
"wordnik.gemspec"
|
31
31
|
]
|
32
32
|
s.homepage = %q{http://github.com/ealdent/wordnik}
|
33
33
|
s.rdoc_options = ["--charset=UTF-8"]
|
34
34
|
s.require_paths = ["lib"]
|
35
|
-
s.rubygems_version = %q{1.3.
|
35
|
+
s.rubygems_version = %q{1.3.6}
|
36
36
|
s.summary = %q{Ruby interface to the Wordnik API}
|
37
37
|
s.test_files = [
|
38
38
|
"test/helper.rb",
|
@@ -44,12 +44,15 @@ Gem::Specification.new do |s|
|
|
44
44
|
s.specification_version = 3
|
45
45
|
|
46
46
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
|
-
s.
|
47
|
+
s.add_runtime_dependency(%q<httparty>, [">= 0.4.5"])
|
48
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
48
49
|
else
|
49
|
-
s.add_dependency(%q<
|
50
|
+
s.add_dependency(%q<httparty>, [">= 0.4.5"])
|
51
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
50
52
|
end
|
51
53
|
else
|
52
|
-
s.add_dependency(%q<
|
54
|
+
s.add_dependency(%q<httparty>, [">= 0.4.5"])
|
55
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
53
56
|
end
|
54
57
|
end
|
55
58
|
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wordnik
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Jason Adams
|
@@ -9,29 +14,35 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date:
|
17
|
+
date: 2010-03-12 00:00:00 -05:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: httparty
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
24
|
requirements:
|
21
25
|
- - ">="
|
22
26
|
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 4
|
30
|
+
- 5
|
23
31
|
version: 0.4.5
|
24
|
-
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
25
34
|
- !ruby/object:Gem::Dependency
|
26
|
-
name:
|
27
|
-
|
28
|
-
|
29
|
-
version_requirements: !ruby/object:Gem::Requirement
|
35
|
+
name: shoulda
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
38
|
requirements:
|
31
39
|
- - ">="
|
32
40
|
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
33
43
|
version: "0"
|
34
|
-
|
44
|
+
type: :development
|
45
|
+
version_requirements: *id002
|
35
46
|
description: Ruby interface to the Wordnik API. Details at http://docs.wordnik.com/api/methods.
|
36
47
|
email: jasonmadams@gmail.com
|
37
48
|
executables: []
|
@@ -50,8 +61,6 @@ files:
|
|
50
61
|
- Rakefile
|
51
62
|
- VERSION
|
52
63
|
- lib/wordnik.rb
|
53
|
-
- lib/wordnik/word.rb
|
54
|
-
- lib/wordnik/wordnik.rb
|
55
64
|
- test/helper.rb
|
56
65
|
- test/test_wordnik.rb
|
57
66
|
- wordnik.gemspec
|
@@ -68,18 +77,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
68
77
|
requirements:
|
69
78
|
- - ">="
|
70
79
|
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
71
82
|
version: "0"
|
72
|
-
version:
|
73
83
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
84
|
requirements:
|
75
85
|
- - ">="
|
76
86
|
- !ruby/object:Gem::Version
|
87
|
+
segments:
|
88
|
+
- 0
|
77
89
|
version: "0"
|
78
|
-
version:
|
79
90
|
requirements: []
|
80
91
|
|
81
92
|
rubyforge_project:
|
82
|
-
rubygems_version: 1.3.
|
93
|
+
rubygems_version: 1.3.6
|
83
94
|
signing_key:
|
84
95
|
specification_version: 3
|
85
96
|
summary: Ruby interface to the Wordnik API
|
data/lib/wordnik/word.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
module Wordnik
|
2
|
-
class Word
|
3
|
-
attr_reader :word
|
4
|
-
|
5
|
-
def initialize(word, eager = false)
|
6
|
-
@word = word.dup.freeze
|
7
|
-
load_all if eager
|
8
|
-
end
|
9
|
-
|
10
|
-
def wordnik_id
|
11
|
-
@wordnik_id ||= Wordnik.instance.lookup(word)['id']
|
12
|
-
end
|
13
|
-
|
14
|
-
def definitions
|
15
|
-
@definitions ||= Wordnik.instance.define(word)
|
16
|
-
end
|
17
|
-
|
18
|
-
def frequencies
|
19
|
-
@frequencies ||= Wordnik.instance.frequency(word)
|
20
|
-
end
|
21
|
-
|
22
|
-
def examples
|
23
|
-
@examples ||= Wordnik.instance.examples(word)
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
def load_all
|
29
|
-
wordnik_id
|
30
|
-
definitions
|
31
|
-
frequencies
|
32
|
-
examples
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
data/lib/wordnik/wordnik.rb
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
module Wordnik
|
2
|
-
class Wordnik
|
3
|
-
include Singleton
|
4
|
-
include HTTParty
|
5
|
-
|
6
|
-
attr_reader :api_key
|
7
|
-
|
8
|
-
base_uri 'http://api.wordnik.com/api'
|
9
|
-
|
10
|
-
def initialize(api_key = nil)
|
11
|
-
@api_key = (api_key || File.read('.api-key').strip || '').dup
|
12
|
-
self.class.default_params :api_key => @api_key
|
13
|
-
end
|
14
|
-
|
15
|
-
def api_key=(api_key)
|
16
|
-
@api_key = api_key.dup
|
17
|
-
end
|
18
|
-
|
19
|
-
def lookup(word)
|
20
|
-
do_request("word.json/#{word.downcase}")
|
21
|
-
end
|
22
|
-
|
23
|
-
def define(word, count = 100)
|
24
|
-
do_request("word.json/#{word.downcase}/definitions", :count => count)
|
25
|
-
end
|
26
|
-
|
27
|
-
def frequency(word)
|
28
|
-
do_request("word.json/#{word.downcase}/frequency")
|
29
|
-
end
|
30
|
-
|
31
|
-
def examples(word)
|
32
|
-
do_request("word.json/#{word.downcase}/examples")
|
33
|
-
end
|
34
|
-
|
35
|
-
def autocomplete(word_fragment, count = 100)
|
36
|
-
do_request("suggest.json/#{word_fragment.downcase}", :count => count)
|
37
|
-
end
|
38
|
-
|
39
|
-
def word_of_the_day
|
40
|
-
do_request("wordoftheday.json")
|
41
|
-
end
|
42
|
-
|
43
|
-
def random(has_definition = true)
|
44
|
-
do_request("words.json/randomWord", :hasDictionaryDef => has_definition)
|
45
|
-
end
|
46
|
-
|
47
|
-
protected
|
48
|
-
|
49
|
-
def do_request(request, options = {})
|
50
|
-
handle_result(self.class.get("/#{request}", options))
|
51
|
-
end
|
52
|
-
|
53
|
-
def handle_result(result)
|
54
|
-
if result.is_a?(String)
|
55
|
-
raise 'Error in result.'
|
56
|
-
else
|
57
|
-
result
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|