wordnik 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +3 -22
- data/Gemfile +4 -0
- data/Gemfile.lock +51 -0
- data/README.md +82 -0
- data/Rakefile +23 -47
- data/api_docs/account.json +1 -0
- data/api_docs/corpus.json +1 -0
- data/api_docs/document.json +1 -0
- data/api_docs/partners.json +1 -0
- data/api_docs/system.json +1 -0
- data/api_docs/tag.json +1 -0
- data/api_docs/user.json +1 -0
- data/api_docs/users.json +1 -0
- data/api_docs/word.json +1 -0
- data/api_docs/wordList.json +1 -0
- data/api_docs/wordLists.json +1 -0
- data/api_docs/wordoftheday.json +1 -0
- data/api_docs/words.json +1 -0
- data/lib/wordnik.rb +57 -84
- data/lib/wordnik/configuration.rb +24 -0
- data/lib/wordnik/endpoint.rb +33 -0
- data/lib/wordnik/monkey_patches.rb +26 -0
- data/lib/wordnik/operation.rb +45 -0
- data/lib/wordnik/operation_parameter.rb +39 -0
- data/lib/wordnik/request.rb +168 -0
- data/lib/wordnik/resource.rb +101 -0
- data/lib/wordnik/response.rb +74 -0
- data/lib/wordnik/version.rb +3 -0
- data/spec/endpoint_spec.rb +26 -0
- data/spec/operation_parameter_spec.rb +26 -0
- data/spec/operation_spec.rb +37 -0
- data/spec/request_spec.rb +157 -0
- data/spec/resource_spec.rb +31 -0
- data/spec/response_spec.rb +49 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +25 -0
- data/spec/wordnik_spec.rb +76 -0
- data/wordnik.gemspec +25 -51
- metadata +146 -43
- data/.document +0 -5
- data/CHANGELOG +0 -8
- data/LICENSE +0 -12
- data/README.rdoc +0 -23
- data/VERSION +0 -1
- data/test/helper.rb +0 -10
- data/test/test_wordnik.rb +0 -89
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Wordnik::Endpoint do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
VCR.use_cassette('words', :record => :new_episodes) do
|
7
|
+
@response = Typhoeus::Request.get("http://api.wordnik.com/v4/word.json")
|
8
|
+
end
|
9
|
+
|
10
|
+
@endpoint = Wordnik::Endpoint.new(JSON.parse(@response.body)['endPoints'].first)
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "initialization" do
|
14
|
+
|
15
|
+
it "successfully initializes" do
|
16
|
+
@endpoint.path.should == "/word.{format}/{word}"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "sets operations" do
|
20
|
+
@endpoint.operations.class.should == Array
|
21
|
+
@endpoint.operations.first.class.should == Wordnik::Operation
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Wordnik::OperationParameter do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
VCR.use_cassette('words', :record => :new_episodes) do
|
7
|
+
@response = Typhoeus::Request.get("http://api.wordnik.com/v4/word.json")
|
8
|
+
end
|
9
|
+
|
10
|
+
@operation_parameter = Wordnik::OperationParameter.new(JSON.parse(@response.body)['endPoints'].first['operations'].first['parameters'].first)
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "initialization" do
|
14
|
+
|
15
|
+
it "successfully initializes" do
|
16
|
+
@operation_parameter.respond_to?(:name).should == true
|
17
|
+
@operation_parameter.respond_to?(:description).should == true
|
18
|
+
@operation_parameter.respond_to?(:required).should == true
|
19
|
+
@operation_parameter.respond_to?(:param_type).should == true
|
20
|
+
@operation_parameter.respond_to?(:default_value).should == true
|
21
|
+
@operation_parameter.respond_to?(:allowable_values).should == true
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Wordnik::Operation do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
VCR.use_cassette('words', :record => :new_episodes) do
|
7
|
+
@response = Typhoeus::Request.get("http://api.wordnik.com/v4/word.json")
|
8
|
+
end
|
9
|
+
|
10
|
+
@operation = Wordnik::Operation.new(JSON.parse(@response.body)['endPoints'].first['operations'].first)
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "initialization" do
|
14
|
+
|
15
|
+
it "successfully initializes" do
|
16
|
+
@operation.summary.should =~ /returns the WordObject/i
|
17
|
+
end
|
18
|
+
|
19
|
+
it "sets parameters" do
|
20
|
+
@operation.parameters.class.should == Array
|
21
|
+
@operation.parameters.first.class.should == Wordnik::OperationParameter
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "instance methods" do
|
27
|
+
it "knows if its HTTP method is GET" do
|
28
|
+
@operation.http_method = "GET"
|
29
|
+
@operation.get?.should == true
|
30
|
+
@operation.http_method = "POST"
|
31
|
+
@operation.get?.should == false
|
32
|
+
@operation.http_method = "get"
|
33
|
+
@operation.get?.should == true
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,157 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Wordnik::Request do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@default_http_method = :get
|
7
|
+
@default_path = "words/fancy"
|
8
|
+
@default_params = {
|
9
|
+
:params => {:foo => "1", :bar => "2"}
|
10
|
+
}
|
11
|
+
@request = Wordnik::Request.new(@default_http_method, @default_path, @default_params)
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "initialization" do
|
15
|
+
it "sets default response format to json" do
|
16
|
+
@request.format.should == "json"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "sets get default host from Wordnik.configuration" do
|
20
|
+
@request.host.should == Wordnik.configuration.base_uri
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "attr_accessors" do
|
26
|
+
|
27
|
+
it "has working attributes" do
|
28
|
+
@request.host.should == Wordnik.configuration.base_uri
|
29
|
+
@request.path.should == "words/fancy"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "allows attributes to be overwritten" do
|
33
|
+
@request.http_method.should == :get
|
34
|
+
@request.http_method = "post"
|
35
|
+
@request.http_method.should == 'post'
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "url" do
|
41
|
+
|
42
|
+
it "constructs a base URL" do
|
43
|
+
@request.url.should == "http://beta.wordnik.com/v4/words.json/fancy"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "constructs a query string" do
|
47
|
+
@request.query_string.should == "?bar=2&foo=1"
|
48
|
+
end
|
49
|
+
|
50
|
+
it "constructs a full url" do
|
51
|
+
@request.url_with_query_string.should == "http://beta.wordnik.com/v4/words.json/fancy?bar=2&foo=1"
|
52
|
+
end
|
53
|
+
|
54
|
+
it "accounts for excessive slashes" do
|
55
|
+
@request = Wordnik::Request.new(:get, "andBurn", @default_params.merge({
|
56
|
+
:host => "slash.com/"
|
57
|
+
}))
|
58
|
+
@request.url.should == "http://slash.com/andBurn.json"
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "path" do
|
64
|
+
|
65
|
+
it "accounts for a total absence of format in the path string" do
|
66
|
+
@request = Wordnik::Request.new(:get, "/word/{word}/entries", @default_params.merge({
|
67
|
+
:format => "xml",
|
68
|
+
:params => {
|
69
|
+
:word => "cat"
|
70
|
+
}
|
71
|
+
}))
|
72
|
+
@request.url.should == "http://beta.wordnik.com/v4/word.xml/cat/entries"
|
73
|
+
end
|
74
|
+
|
75
|
+
it "does string substitution on path params" do
|
76
|
+
@request = Wordnik::Request.new(:get, "/word.{format}/{word}/entries", @default_params.merge({
|
77
|
+
:format => "xml",
|
78
|
+
:params => {
|
79
|
+
:word => "cat"
|
80
|
+
}
|
81
|
+
}))
|
82
|
+
@request.url.should == "http://beta.wordnik.com/v4/word.xml/cat/entries"
|
83
|
+
end
|
84
|
+
|
85
|
+
it "leaves path-bound params out of the query string" do
|
86
|
+
@request = Wordnik::Request.new(:get, "/word.{format}/{word}/entries", @default_params.merge({
|
87
|
+
:params => {
|
88
|
+
:word => "cat",
|
89
|
+
:limit => 20
|
90
|
+
}
|
91
|
+
}))
|
92
|
+
@request.query_string.should == "?limit=20"
|
93
|
+
end
|
94
|
+
|
95
|
+
it "returns a question-mark free (blank) query string if no query params are present" do
|
96
|
+
@request = Wordnik::Request.new(:get, "/word.{format}/{word}/entries", @default_params.merge({
|
97
|
+
:params => {
|
98
|
+
:word => "cat",
|
99
|
+
}
|
100
|
+
}))
|
101
|
+
@request.query_string.should == ""
|
102
|
+
end
|
103
|
+
|
104
|
+
it "removes blank params" do
|
105
|
+
@request = Wordnik::Request.new(:get, "words/fancy", @default_params.merge({
|
106
|
+
:params => {
|
107
|
+
:word => "dog",
|
108
|
+
:limit => "",
|
109
|
+
:foo => "criminy"
|
110
|
+
}
|
111
|
+
}))
|
112
|
+
@request.query_string.should == "?foo=criminy&word=dog"
|
113
|
+
end
|
114
|
+
|
115
|
+
it "obfuscates the API key when needed" do
|
116
|
+
@request = Wordnik::Request.new(:get, "words/fancy", @default_params.merge({
|
117
|
+
:params => {
|
118
|
+
:word => "dog",
|
119
|
+
:api_key => "123456"
|
120
|
+
}
|
121
|
+
}))
|
122
|
+
@request.query_string_params.should == {:word => "dog", :api_key => "123456"}
|
123
|
+
@request.query_string_params(true).should == {:word => "dog", :api_key => "YOUR_API_KEY"}
|
124
|
+
|
125
|
+
@request.query_string.should == "?api_key=123456&word=dog"
|
126
|
+
@request.query_string(:obfuscated => true).should == "?api_key=YOUR_API_KEY&word=dog"
|
127
|
+
|
128
|
+
@request.url_with_query_string.should =~ /123456/
|
129
|
+
@request.url_with_query_string(:obfuscated => true).should =~ /YOUR\_API\_KEY/
|
130
|
+
end
|
131
|
+
|
132
|
+
it "URI encodes the path" do
|
133
|
+
@request = Wordnik::Request.new(:get, "word.{format}/{word}/definitions", @default_params.merge({
|
134
|
+
:params => {
|
135
|
+
:word => "bill gates"
|
136
|
+
}
|
137
|
+
}))
|
138
|
+
@request.url.should =~ /word.json\/bill\%20gates\/definitions/
|
139
|
+
end
|
140
|
+
|
141
|
+
it "converts numeric params to strings" do
|
142
|
+
@request = Wordnik::Request.new(@default_http_method, @default_path, @default_params.merge({
|
143
|
+
:params => {
|
144
|
+
:limit => 100
|
145
|
+
}
|
146
|
+
}))
|
147
|
+
|
148
|
+
@request.interpreted_path.should_not be_nil
|
149
|
+
@request.query_string.should =~ /\?limit=100/
|
150
|
+
@request.url_with_query_string.should =~ /\?limit=100/
|
151
|
+
end
|
152
|
+
|
153
|
+
it "camelCases parameters"
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Wordnik::Resource do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
VCR.use_cassette('words', :record => :new_episodes) do
|
7
|
+
@response = Typhoeus::Request.get("http://api.wordnik.com/v4/word.json")
|
8
|
+
end
|
9
|
+
|
10
|
+
@default_params = {
|
11
|
+
:name => "word",
|
12
|
+
:raw_data => JSON.parse(@response.body)
|
13
|
+
}
|
14
|
+
|
15
|
+
@resource = Wordnik::Resource.new(@default_params)
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "initialization" do
|
19
|
+
|
20
|
+
it "successfully initializes" do
|
21
|
+
@resource.name.should == "word"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "sets endpoints" do
|
25
|
+
@resource.endpoints.size.should == 10
|
26
|
+
@resource.endpoints.first.class.should == Wordnik::Endpoint
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Wordnik::Response do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
|
7
|
+
VCR.use_cassette('default_response_request', :record => :new_episodes) do
|
8
|
+
@raw = Typhoeus::Request.get("http://api.wordnik.com/v4/word.json")
|
9
|
+
end
|
10
|
+
|
11
|
+
@response = Wordnik::Response.new(@raw)
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "initialization" do
|
15
|
+
it "sets body" do
|
16
|
+
@response.body.class.should == Hash
|
17
|
+
@response.body.has_key?('endPoints').should == true
|
18
|
+
end
|
19
|
+
|
20
|
+
it "sets code" do
|
21
|
+
@response.code.should == 200
|
22
|
+
end
|
23
|
+
|
24
|
+
it "converts header string into a hash" do
|
25
|
+
@response.headers.class.should == Hash
|
26
|
+
@response.headers['Wordnik-Api-Version'].to_s.should =~ /4\.0/
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "format" do
|
32
|
+
|
33
|
+
it "recognizes json" do
|
34
|
+
@response.format.should == :json
|
35
|
+
@response.json?.should == true
|
36
|
+
end
|
37
|
+
|
38
|
+
it "recognizes xml" do
|
39
|
+
VCR.use_cassette('xml_response_request', :record => :new_episodes) do
|
40
|
+
@raw = Typhoeus::Request.get("http://api.wordnik.com/v4/word.xml/help")
|
41
|
+
end
|
42
|
+
@response = Wordnik::Response.new(@raw)
|
43
|
+
@response.format.should == :xml
|
44
|
+
@response.xml?.should == true
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'wordnik'
|
4
|
+
require 'vcr'
|
5
|
+
require 'typhoeus'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
# some (optional) config here
|
10
|
+
end
|
11
|
+
|
12
|
+
VCR.config do |config|
|
13
|
+
config.cassette_library_dir = 'spec/vcr'
|
14
|
+
config.stub_with :webmock # or :fakeweb
|
15
|
+
end
|
16
|
+
|
17
|
+
unless ENV['KEY']
|
18
|
+
puts "\nOn Noes! You gotta include your API key when running the specs, like so:\n\nKEY=12345 rake spec\n\n"
|
19
|
+
exit
|
20
|
+
end
|
21
|
+
|
22
|
+
Wordnik.configure do |config|
|
23
|
+
config.api_key = ENV['KEY']
|
24
|
+
config.base_uri = "beta.wordnik.com/v4"
|
25
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Wordnik do
|
4
|
+
|
5
|
+
|
6
|
+
context "instantiation" do
|
7
|
+
|
8
|
+
context "resources" do
|
9
|
+
it "instantiates resources from cached JSON" do
|
10
|
+
Wordnik.resources.class.should == Hash
|
11
|
+
Wordnik.resources[:word].class.should == Wordnik::Resource
|
12
|
+
end
|
13
|
+
|
14
|
+
it "has as many resources as there are resource names" do
|
15
|
+
Wordnik.resources.size.should == Wordnik.resource_names.size
|
16
|
+
end
|
17
|
+
|
18
|
+
it "assigns resource keys that match the resource names" do
|
19
|
+
Wordnik.resources[:word].name.should == :word
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
context "dynamic method_missing magic" do
|
27
|
+
|
28
|
+
it "maps shorthand Wordnik.resource calls to their resources" do
|
29
|
+
Wordnik.word.class.should == Wordnik::Resource
|
30
|
+
Wordnik.word.name.should == :word
|
31
|
+
end
|
32
|
+
|
33
|
+
it "builds requests but doesn't run them if method name begins with 'build_'" do
|
34
|
+
@request = Wordnik.word.build_get('dynamo')
|
35
|
+
@request.class.should == Wordnik::Request
|
36
|
+
end
|
37
|
+
|
38
|
+
it "runs requests and returns their body if method name doesn't begin with 'build_'" do
|
39
|
+
@response_body = Wordnik.word.get('dynamo')
|
40
|
+
@response_body.class.should == Hash
|
41
|
+
@response_body.keys.sort.should == %w(canonicalForm word)
|
42
|
+
end
|
43
|
+
|
44
|
+
context "argument handling" do
|
45
|
+
|
46
|
+
before(:each) do
|
47
|
+
@request = Wordnik.word.build_get_examples('dynamo', :skip => 2, :limit => 10)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "injects required arguments into the path" do
|
51
|
+
@request.path.should == "/word/dynamo/examples"
|
52
|
+
end
|
53
|
+
|
54
|
+
it "passes optional key-value arguments to the query string" do
|
55
|
+
@request.query_string.should == "?limit=10&skip=2"
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
context "wordlists" do
|
61
|
+
|
62
|
+
it "creates a wordlist"
|
63
|
+
|
64
|
+
it "adds words"
|
65
|
+
|
66
|
+
it "updates words"
|
67
|
+
|
68
|
+
it "get all the words"
|
69
|
+
|
70
|
+
it "deletes a wordlist"
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|