wordnik 0.3.8 → 0.3.9
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/lib/wordnik.rb +13 -4
- data/lib/wordnik/operation_parameter.rb +1 -3
- data/lib/wordnik/request.rb +27 -12
- data/lib/wordnik/version.rb +1 -1
- data/spec/operation_parameter_spec.rb +7 -6
- data/spec/request_spec.rb +19 -0
- data/spec/spec_helper.rb +4 -0
- data/spec/wordnik_spec.rb +38 -3
- metadata +2 -2
data/Gemfile.lock
CHANGED
data/lib/wordnik.rb
CHANGED
@@ -8,9 +8,7 @@ require 'wordnik/configuration'
|
|
8
8
|
require 'wordnik/version'
|
9
9
|
|
10
10
|
module Wordnik
|
11
|
-
|
12
|
-
API_VERSION = "4.01.61"
|
13
|
-
|
11
|
+
|
14
12
|
class << self
|
15
13
|
|
16
14
|
# A Wordnik configuration object. Must act like a hash and return sensible
|
@@ -61,12 +59,17 @@ module Wordnik
|
|
61
59
|
|
62
60
|
def authenticate
|
63
61
|
return if Wordnik.authenticated?
|
62
|
+
|
63
|
+
if Wordnik.configuration.username.blank? || Wordnik.configuration.password.blank?
|
64
|
+
raise ConfigurationError, "Username and password are required to authenticate."
|
65
|
+
end
|
66
|
+
|
64
67
|
response_body = Wordnik.account.get_authenticate(Wordnik.configuration.username, :password => Wordnik.configuration.password)
|
65
68
|
if response_body.is_a?(Hash) && response_body['userId'].present? && response_body['token'].present?
|
66
69
|
Wordnik.configuration.user_id = response_body['userId']
|
67
70
|
Wordnik.configuration.auth_token = response_body['token']
|
68
71
|
else
|
69
|
-
raise
|
72
|
+
raise ApiServerError, response_body.to_s
|
70
73
|
end
|
71
74
|
end
|
72
75
|
|
@@ -89,4 +92,10 @@ module Wordnik
|
|
89
92
|
|
90
93
|
end
|
91
94
|
|
95
|
+
end
|
96
|
+
|
97
|
+
class ConfigurationError < StandardError
|
98
|
+
end
|
99
|
+
|
100
|
+
class ApiServerError < StandardError
|
92
101
|
end
|
@@ -6,9 +6,7 @@ module Wordnik
|
|
6
6
|
include ActiveModel::Conversion
|
7
7
|
extend ActiveModel::Naming
|
8
8
|
|
9
|
-
attr_accessor :name, :description, :required, :param_type, :default_value, :allowable_values
|
10
|
-
|
11
|
-
validates_presence_of :name, :description, :required, :param_type, :default_value, :allowable_values
|
9
|
+
attr_accessor :name, :description, :required, :param_type, :default_value, :allowable_values, :param_access, :internal_description, :wrapper_name
|
12
10
|
|
13
11
|
def initialize(attributes = {})
|
14
12
|
attributes.each do |name, value|
|
data/lib/wordnik/request.rb
CHANGED
@@ -10,23 +10,26 @@ module Wordnik
|
|
10
10
|
include ActiveModel::Conversion
|
11
11
|
extend ActiveModel::Naming
|
12
12
|
|
13
|
-
attr_accessor :host, :
|
13
|
+
attr_accessor :host, :path, :format, :params, :body, :http_method, :headers
|
14
14
|
|
15
15
|
validates_presence_of :host, :path, :format, :http_method
|
16
16
|
|
17
|
+
# All requests must have an HTTP method and a path
|
18
|
+
# Optionals parameters are :params, :headers, :body, :format, :host
|
19
|
+
#
|
17
20
|
def initialize(http_method, path, attributes={})
|
18
21
|
attributes[:format] ||= "json"
|
19
22
|
attributes[:host] ||= Wordnik.configuration.base_uri
|
20
23
|
attributes[:params] ||= {}
|
21
24
|
|
22
|
-
# Set default headers
|
25
|
+
# Set default headers
|
23
26
|
default_headers = {
|
24
27
|
'User-Agent' => "Wordnik Ruby Gem #{Wordnik::VERSION}",
|
25
28
|
'Content-Type' => "application/#{attributes[:format].downcase}",
|
26
29
|
:api_key => Wordnik.configuration.api_key
|
27
30
|
}
|
28
31
|
|
29
|
-
# If a nil/blank api_key was passed in, remove it from the headers, even if the override is nil/blank
|
32
|
+
# If a nil/blank api_key was passed in, remove it from the headers, even if the override value is nil/blank
|
30
33
|
if attributes[:headers].present? && attributes[:headers].has_key?(:api_key)
|
31
34
|
default_headers.delete(:api_key)
|
32
35
|
end
|
@@ -38,7 +41,13 @@ module Wordnik
|
|
38
41
|
attributes[:headers].delete(:api_key) if attributes[:headers].present?
|
39
42
|
end
|
40
43
|
|
44
|
+
# Merge argument headers into defaults
|
41
45
|
attributes[:headers] = default_headers.merge(attributes[:headers] || {})
|
46
|
+
|
47
|
+
# Stick in the auth token if there is one
|
48
|
+
if Wordnik.authenticated?
|
49
|
+
attributes[:headers].merge!({:auth_token => Wordnik.configuration.auth_token})
|
50
|
+
end
|
42
51
|
|
43
52
|
self.http_method = http_method.to_sym
|
44
53
|
self.path = path
|
@@ -50,8 +59,7 @@ module Wordnik
|
|
50
59
|
# Construct a base URL
|
51
60
|
def url
|
52
61
|
u = Addressable::URI.new
|
53
|
-
u.host = self.host.sub(/\/$/, '')
|
54
|
-
u.port = self.port if self.port.present?
|
62
|
+
u.host = self.host.sub(/\/$/, '') # Remove trailing slash
|
55
63
|
u.path = self.interpreted_path
|
56
64
|
u.scheme = "http" # For some reason this must be set _after_ host, otherwise Addressable gets upset
|
57
65
|
u.to_s
|
@@ -76,10 +84,17 @@ module Wordnik
|
|
76
84
|
URI.encode(p)
|
77
85
|
end
|
78
86
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
87
|
+
# Massage the request body into a state of readiness
|
88
|
+
# If body is a hash, camelize all keys then convert to a json string
|
89
|
+
#
|
90
|
+
def body=(value)
|
91
|
+
if value.is_a?(Hash)
|
92
|
+
value = value.inject({}) do |memo, (k,v)|
|
93
|
+
memo[k.to_s.camelize(:lower).to_sym] = v
|
94
|
+
memo
|
95
|
+
end
|
96
|
+
end
|
97
|
+
@body = value
|
83
98
|
end
|
84
99
|
|
85
100
|
# Iterate over all params,
|
@@ -132,21 +147,21 @@ module Wordnik
|
|
132
147
|
when :post
|
133
148
|
Typhoeus::Request.post(
|
134
149
|
self.url_with_query_string,
|
135
|
-
:body => self.
|
150
|
+
:body => self.body.to_json,
|
136
151
|
:headers => self.headers.stringify_keys
|
137
152
|
)
|
138
153
|
|
139
154
|
when :put
|
140
155
|
Typhoeus::Request.put(
|
141
156
|
self.url_with_query_string,
|
142
|
-
:body => self.
|
157
|
+
:body => self.body.to_json,
|
143
158
|
:headers => self.headers.stringify_keys
|
144
159
|
)
|
145
160
|
|
146
161
|
when :delete
|
147
162
|
Typhoeus::Request.delete(
|
148
163
|
self.url_with_query_string,
|
149
|
-
:body => self.
|
164
|
+
:body => self.body.to_json,
|
150
165
|
:headers => self.headers.stringify_keys
|
151
166
|
)
|
152
167
|
end
|
data/lib/wordnik/version.rb
CHANGED
@@ -13,12 +13,13 @@ describe Wordnik::OperationParameter do
|
|
13
13
|
describe "initialization" do
|
14
14
|
|
15
15
|
it "successfully initializes" do
|
16
|
-
@operation_parameter.respond_to
|
17
|
-
@operation_parameter.respond_to
|
18
|
-
@operation_parameter.respond_to
|
19
|
-
@operation_parameter.respond_to
|
20
|
-
@operation_parameter.respond_to
|
21
|
-
@operation_parameter.respond_to
|
16
|
+
@operation_parameter.should respond_to(:name)
|
17
|
+
@operation_parameter.should respond_to(:description)
|
18
|
+
@operation_parameter.should respond_to(:required)
|
19
|
+
@operation_parameter.should respond_to(:param_type)
|
20
|
+
@operation_parameter.should respond_to(:default_value)
|
21
|
+
@operation_parameter.should respond_to(:allowable_values)
|
22
|
+
@operation_parameter.should respond_to(:param_access)
|
22
23
|
end
|
23
24
|
|
24
25
|
end
|
data/spec/request_spec.rb
CHANGED
@@ -19,6 +19,11 @@ describe Wordnik::Request do
|
|
19
19
|
it "gets default host from Wordnik.configuration" do
|
20
20
|
@request.host.should == Wordnik.configuration.base_uri
|
21
21
|
end
|
22
|
+
|
23
|
+
it "allows params to be nil" do
|
24
|
+
@request = Wordnik::Request.new(@default_http_method, @default_path, :params => nil)
|
25
|
+
@request.query_string.should == ""
|
26
|
+
end
|
22
27
|
|
23
28
|
end
|
24
29
|
|
@@ -59,6 +64,20 @@ describe Wordnik::Request do
|
|
59
64
|
end
|
60
65
|
|
61
66
|
end
|
67
|
+
|
68
|
+
describe "body" do
|
69
|
+
|
70
|
+
it "camelCases parameters" do
|
71
|
+
@request = Wordnik::Request.new(@default_http_method, @default_path, @default_params.merge({
|
72
|
+
:body => {
|
73
|
+
:bad_dog => 'bud',
|
74
|
+
:goodDog => "dud"
|
75
|
+
}
|
76
|
+
}))
|
77
|
+
@request.body.keys.should == [:badDog, :goodDog]
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
62
81
|
|
63
82
|
describe "path" do
|
64
83
|
|
data/spec/spec_helper.rb
CHANGED
data/spec/wordnik_spec.rb
CHANGED
@@ -5,6 +5,9 @@ describe Wordnik do
|
|
5
5
|
before(:each) do
|
6
6
|
configure_wordnik
|
7
7
|
end
|
8
|
+
|
9
|
+
after(:each) do
|
10
|
+
end
|
8
11
|
|
9
12
|
context "initialization" do
|
10
13
|
|
@@ -49,10 +52,21 @@ describe Wordnik do
|
|
49
52
|
config.password = 'wrong!'
|
50
53
|
config.base_uri = "beta.wordnik.com/v4"
|
51
54
|
end
|
55
|
+
lambda { Wordnik.authenticate }.should raise_error(ApiServerError)
|
52
56
|
Wordnik.authenticated?.should == false
|
53
57
|
end
|
54
58
|
|
55
|
-
it "fails if username and/or password are absent"
|
59
|
+
it "fails if username and/or password are absent" do
|
60
|
+
Wordnik.de_authenticate
|
61
|
+
Wordnik.configure do |config|
|
62
|
+
config.api_key = CREDENTIALS[:api_key]
|
63
|
+
config.username = nil
|
64
|
+
config.password = nil
|
65
|
+
config.base_uri = "beta.wordnik.com/v4"
|
66
|
+
end
|
67
|
+
lambda { Wordnik.authenticate }.should raise_error(ConfigurationError, /username and password are required/i)
|
68
|
+
Wordnik.authenticated?.should == false
|
69
|
+
end
|
56
70
|
|
57
71
|
end
|
58
72
|
|
@@ -90,15 +104,36 @@ describe Wordnik do
|
|
90
104
|
@request.query_string.should == "?limit=10&skip=2"
|
91
105
|
end
|
92
106
|
|
107
|
+
it "puts key-value arguments in the request body instead of the query params"
|
108
|
+
|
93
109
|
end
|
94
110
|
|
95
111
|
context "wordlists" do
|
96
112
|
|
113
|
+
before do
|
114
|
+
configure_wordnik
|
115
|
+
Wordnik.authenticate
|
116
|
+
end
|
117
|
+
|
97
118
|
it "creates a wordlist" do
|
98
|
-
|
119
|
+
body = {
|
120
|
+
:name=> "Wordnik Ruby Test List #{RAND}",
|
121
|
+
:description => 'This is created by the test suite.',
|
122
|
+
:type => 'PUBLIC',
|
123
|
+
:user_id => Wordnik.configuration.user_id
|
124
|
+
}
|
125
|
+
request = Wordnik::Request.new(:post, "wordLists", :body => body)
|
126
|
+
request.response.body.should be_a_kind_of(Hash)
|
127
|
+
request.response.body.should have_key('permalink')
|
99
128
|
end
|
100
129
|
|
101
|
-
it "
|
130
|
+
it "finds the new wordlist" do
|
131
|
+
lists = Wordnik.account.get_word_lists
|
132
|
+
permalinks = lists.map { |list| list['permalink'] }
|
133
|
+
permalinks.should include("wordnik-ruby-test-list-#{RAND}")
|
134
|
+
end
|
135
|
+
|
136
|
+
it "finds the new wordlist and adds words to it"
|
102
137
|
|
103
138
|
it "updates words"
|
104
139
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: wordnik
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.3.
|
5
|
+
version: 0.3.9
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Zeke Sikelianos
|
@@ -11,7 +11,7 @@ autorequire:
|
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
13
|
|
14
|
-
date: 2011-03-
|
14
|
+
date: 2011-03-18 00:00:00 -07:00
|
15
15
|
default_executable:
|
16
16
|
dependencies:
|
17
17
|
- !ruby/object:Gem::Dependency
|