lita-wordnik 0.0.3 → 0.1.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.
- checksums.yaml +4 -4
- data/README.md +23 -0
- data/lib/lita/handlers/wordnik.rb +61 -11
- data/lita-wordnik.gemspec +2 -2
- data/spec/lita/handlers/wordnik_spec.rb +100 -46
- metadata +21 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce8bb942f982996203a905a3212e4ee5d162c39c
|
4
|
+
data.tar.gz: 20134edd81a00fde12234f006d47bb7568972a4f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7df229ffa08cc6fd6eca44c445896614d20f68d466f9bd31c5ec323e4e2807e97086122f806ac890cef6a0ea043a8cf4c9e6e2f83241f8dd8cd17d409b98c8f9
|
7
|
+
data.tar.gz: dee4b9ebd3ce0a0567480d153d18dcbd2f73aa112dfb6d8512da9ceaffddf652a6badf3c0a7f58d1f33401645f95a3ed6f77621bc0fe7ef4a7486bb84fa1bb79
|
data/README.md
CHANGED
@@ -35,6 +35,29 @@ To get the definition for a word:
|
|
35
35
|
```
|
36
36
|
Lita: define WORD
|
37
37
|
```
|
38
|
+
To get synonyms for a word:
|
39
|
+
|
40
|
+
```
|
41
|
+
Lita: synonyms WORD
|
42
|
+
```
|
43
|
+
|
44
|
+
or
|
45
|
+
|
46
|
+
```
|
47
|
+
Lita: words like WORD
|
48
|
+
```
|
49
|
+
|
50
|
+
To get antonyms for a word:
|
51
|
+
|
52
|
+
```
|
53
|
+
Lita: antonyms WORD
|
54
|
+
```
|
55
|
+
|
56
|
+
or
|
57
|
+
|
58
|
+
```
|
59
|
+
Lita: words unlike WORD
|
60
|
+
```
|
38
61
|
|
39
62
|
## License
|
40
63
|
|
@@ -3,7 +3,7 @@ require "lita"
|
|
3
3
|
module Lita
|
4
4
|
module Handlers
|
5
5
|
class Wordnik < Handler
|
6
|
-
|
6
|
+
NO_RESULTS = "Wordnik doesn't have any results for that."
|
7
7
|
|
8
8
|
def self.default_config(config)
|
9
9
|
config.api_key = nil
|
@@ -12,18 +12,37 @@ module Lita
|
|
12
12
|
route(/define\s+(.+)/i, :define, command: true, help: {
|
13
13
|
"define WORD" => "Get the definition for WORD."
|
14
14
|
})
|
15
|
+
route(/^syn(?:onyms)?\s+(.*)$/i, :synonyms, command: true, help: {
|
16
|
+
"synonyms WORD" => "Get synonyms for WORD."
|
17
|
+
})
|
18
|
+
route(/^words\s*like\s+(.*)$/i, :synonyms, command: true, help: {
|
19
|
+
"words like WORD" => "Get synonyms for WORD."
|
20
|
+
})
|
21
|
+
route(/^ant(?:onyms)?\s+(.*)$/i, :antonyms, command: true, help: {
|
22
|
+
"antonyms WORD" => "Get antonyms for WORD."
|
23
|
+
})
|
24
|
+
route(/^words\s*unlike\s+(.*)$/i, :antonyms, command: true, help: {
|
25
|
+
"words unlike WORD" => "Get antonyms for WORD."
|
26
|
+
})
|
15
27
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
28
|
+
class << self
|
29
|
+
def define_wordnik_method(name, getter_name)
|
30
|
+
define_method(name) do |response|
|
31
|
+
return unless validate(response)
|
32
|
+
word = encode_word(response.matches[0][0])
|
33
|
+
result = send(getter_name, word)
|
34
|
+
response.reply(result)
|
35
|
+
end
|
36
|
+
end
|
21
37
|
end
|
22
38
|
|
39
|
+
define_wordnik_method :define, :definition_for
|
40
|
+
define_wordnik_method :synonyms, :synonyms_for
|
41
|
+
define_wordnik_method :antonyms, :antonyms_for
|
42
|
+
|
23
43
|
private
|
24
44
|
|
25
|
-
def
|
26
|
-
word = encode_word(word)
|
45
|
+
def definition_for(word)
|
27
46
|
url = "http://api.wordnik.com/v4/word.json/#{word}/definitions"
|
28
47
|
Lita.logger.debug("Making Wordnik API request to #{url}.")
|
29
48
|
process_response http.get(
|
@@ -36,6 +55,30 @@ module Lita
|
|
36
55
|
)
|
37
56
|
end
|
38
57
|
|
58
|
+
def synonyms_for(word)
|
59
|
+
url = "http://api.wordnik.com/v4/word.json/#{word}/relatedWords"
|
60
|
+
Lita.logger.debug("Making Wordnik API request to #{url}.")
|
61
|
+
process_response http.get(
|
62
|
+
url,
|
63
|
+
api_key: Lita.config.handlers.wordnik.api_key,
|
64
|
+
useCanonical: true,
|
65
|
+
relationshipTypes: "synonym",
|
66
|
+
limitPerRelationshipType: 5
|
67
|
+
)
|
68
|
+
end
|
69
|
+
|
70
|
+
def antonyms_for(word)
|
71
|
+
url = "http://api.wordnik.com/v4/word.json/#{word}/relatedWords"
|
72
|
+
Lita.logger.debug("Making Wordnik API request to #{url}.")
|
73
|
+
process_response http.get(
|
74
|
+
url,
|
75
|
+
api_key: Lita.config.handlers.wordnik.api_key,
|
76
|
+
useCanonical: true,
|
77
|
+
relationshipTypes: "antonym",
|
78
|
+
limitPerRelationshipType: 5
|
79
|
+
)
|
80
|
+
end
|
81
|
+
|
39
82
|
def encode_word(word)
|
40
83
|
begin
|
41
84
|
URI.parse(word)
|
@@ -50,14 +93,21 @@ module Lita
|
|
50
93
|
when 400
|
51
94
|
"Wordnik didn't understand that word."
|
52
95
|
when 404
|
53
|
-
|
96
|
+
NO_RESULTS
|
54
97
|
when 200
|
55
98
|
data = MultiJson.load(response.body).first
|
56
99
|
|
57
100
|
if data
|
58
|
-
|
101
|
+
case data["relationshipType"]
|
102
|
+
when "synonym"
|
103
|
+
"(synonyms): #{data["words"].join(', ')}"
|
104
|
+
when "antonym"
|
105
|
+
"(antonyms): #{data["words"].join(', ')}"
|
106
|
+
else
|
107
|
+
"#{data["word"]} (#{data["partOfSpeech"]}): #{data["text"]}"
|
108
|
+
end
|
59
109
|
else
|
60
|
-
|
110
|
+
NO_RESULTS
|
61
111
|
end
|
62
112
|
else
|
63
113
|
Lita.logger.error("Wordnik request failed: #{response.inspect}")
|
data/lita-wordnik.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |spec|
|
2
2
|
spec.name = "lita-wordnik"
|
3
|
-
spec.version = "0.0
|
3
|
+
spec.version = "0.1.0"
|
4
4
|
spec.authors = ["Jimmy Cuadra"]
|
5
5
|
spec.email = ["jimmy@jimmycuadra.com"]
|
6
6
|
spec.description = %q{A Lita handler for dictionary functionality backed by Wordnik.}
|
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
|
19
19
|
spec.add_development_dependency "bundler", "~> 1.3"
|
20
20
|
spec.add_development_dependency "rake"
|
21
|
-
spec.add_development_dependency "rspec", "
|
21
|
+
spec.add_development_dependency "rspec", ">= 3.0.0"
|
22
22
|
spec.add_development_dependency "simplecov"
|
23
23
|
spec.add_development_dependency "coveralls"
|
24
24
|
end
|
@@ -1,8 +1,28 @@
|
|
1
1
|
require "spec_helper"
|
2
2
|
|
3
3
|
describe Lita::Handlers::Wordnik, lita_handler: true do
|
4
|
-
let(:body)
|
5
|
-
|
4
|
+
let(:response) { double("Faraday::Response", status: 200, body: body) }
|
5
|
+
|
6
|
+
it { routes_command("define computer").to(:define) }
|
7
|
+
it { routes_command("synonyms cold").to(:synonyms) }
|
8
|
+
it { routes_command("syn cold").to(:synonyms) }
|
9
|
+
it { routes_command("words like cold").to(:synonyms) }
|
10
|
+
it { routes_command("antonyms cold").to(:antonyms) }
|
11
|
+
it { routes_command("ant cold").to(:antonyms) }
|
12
|
+
it { routes_command("words unlike cold").to(:antonyms) }
|
13
|
+
|
14
|
+
it "sets the API key to nil by default" do
|
15
|
+
expect(Lita.config.handlers.wordnik.api_key).to be_nil
|
16
|
+
end
|
17
|
+
|
18
|
+
context "with Faraday stubbing" do
|
19
|
+
before do
|
20
|
+
allow_any_instance_of( Faraday::Connection).to receive(:get).and_return(response)
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#define" do
|
24
|
+
let(:body) do
|
25
|
+
<<-BODY.chomp
|
6
26
|
[
|
7
27
|
{
|
8
28
|
"word": "computer",
|
@@ -13,67 +33,101 @@ assembles, stores, correlates, or otherwise processes information.",
|
|
13
33
|
}
|
14
34
|
]
|
15
35
|
BODY
|
16
|
-
|
36
|
+
end
|
17
37
|
|
18
|
-
|
38
|
+
it "replies that the API key is required" do
|
39
|
+
send_command("define computer")
|
40
|
+
expect(replies.last).to include("API key required")
|
41
|
+
end
|
19
42
|
|
20
|
-
|
21
|
-
|
22
|
-
Faraday::Connection
|
23
|
-
).to receive(:get).and_return(response)
|
24
|
-
end
|
43
|
+
context "when the API key is set" do
|
44
|
+
before { Lita.config.handlers.wordnik.api_key = "abc123" }
|
25
45
|
|
26
|
-
|
46
|
+
it "replies with the definition" do
|
47
|
+
send_command("define computer")
|
48
|
+
expect(replies.last).to include("computer (noun): A device that")
|
49
|
+
end
|
27
50
|
|
28
|
-
|
29
|
-
|
30
|
-
|
51
|
+
it "URL encodes words" do
|
52
|
+
word = "a phrase with a % sign in it"
|
53
|
+
expect(URI).to receive(:encode).with(word)
|
54
|
+
send_command("define #{word}")
|
55
|
+
end
|
31
56
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
57
|
+
it "replies that Wordnik didn't understand the word on 400 status" do
|
58
|
+
allow(response).to receive(:status).and_return(400)
|
59
|
+
send_command("define computer")
|
60
|
+
expect(replies.last).to include("didn't understand")
|
61
|
+
end
|
37
62
|
|
38
|
-
|
39
|
-
|
63
|
+
it "replies that Wordnik has no definitions on 404 status" do
|
64
|
+
allow(response).to receive(:status).and_return(404)
|
65
|
+
send_command("define computer")
|
66
|
+
expect(replies.last).to include("doesn't have any results")
|
67
|
+
end
|
40
68
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
69
|
+
it "logs an error and replies that the request completely failed" do
|
70
|
+
allow(response).to receive(:status).and_return(500)
|
71
|
+
send_command("define computer")
|
72
|
+
expect(replies.last).to include("request failed")
|
73
|
+
end
|
45
74
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
75
|
+
context "when the response has no definitions" do
|
76
|
+
let(:body) { "[]" }
|
77
|
+
|
78
|
+
it "replies that Wordnik has no definitions" do
|
79
|
+
send_command("define whiskey stone")
|
80
|
+
expect(replies.last).to include("doesn't have any results")
|
81
|
+
end
|
82
|
+
end
|
50
83
|
end
|
84
|
+
end
|
51
85
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
86
|
+
describe "#synonyms" do
|
87
|
+
let(:body) do
|
88
|
+
<<-BODY.chomp
|
89
|
+
[
|
90
|
+
{
|
91
|
+
"words": [
|
92
|
+
"stoical",
|
93
|
+
"frigid",
|
94
|
+
"unconcerned",
|
95
|
+
"bleak",
|
96
|
+
"indifferent"
|
97
|
+
],
|
98
|
+
"relationshipType": "synonym"
|
99
|
+
}
|
100
|
+
]
|
101
|
+
BODY
|
56
102
|
end
|
57
103
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
104
|
+
before { Lita.config.handlers.wordnik.api_key = "abc123" }
|
105
|
+
|
106
|
+
it "replies with synonyms" do
|
107
|
+
send_command("synonyms cold")
|
108
|
+
expect(replies.last).to include("(synonyms): stoical, frigid, unconcerned, bleak, indifferent")
|
62
109
|
end
|
110
|
+
end
|
63
111
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
112
|
+
describe "#antonyms" do
|
113
|
+
let(:body) do
|
114
|
+
<<-BODY.chomp
|
115
|
+
[
|
116
|
+
{
|
117
|
+
"words": [
|
118
|
+
"hot"
|
119
|
+
],
|
120
|
+
"relationshipType": "antonym"
|
121
|
+
}
|
122
|
+
]
|
123
|
+
BODY
|
68
124
|
end
|
69
125
|
|
70
|
-
|
71
|
-
let(:body) { "[]" }
|
126
|
+
before { Lita.config.handlers.wordnik.api_key = "abc123" }
|
72
127
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
end
|
128
|
+
it "replies with antonyms" do
|
129
|
+
send_command("antonyms cold")
|
130
|
+
expect(replies.last).to include("(antonyms): hot")
|
77
131
|
end
|
78
132
|
end
|
79
133
|
end
|
metadata
CHANGED
@@ -1,97 +1,97 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lita-wordnik
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jimmy Cuadra
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-06-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: lita
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '2.1'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '2.1'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '1.3'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.3'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 3.0.0
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 3.0.0
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: simplecov
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: coveralls
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- -
|
87
|
+
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- -
|
94
|
+
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
description: A Lita handler for dictionary functionality backed by Wordnik.
|
@@ -101,8 +101,8 @@ executables: []
|
|
101
101
|
extensions: []
|
102
102
|
extra_rdoc_files: []
|
103
103
|
files:
|
104
|
-
- .gitignore
|
105
|
-
- .travis.yml
|
104
|
+
- ".gitignore"
|
105
|
+
- ".travis.yml"
|
106
106
|
- Gemfile
|
107
107
|
- LICENSE
|
108
108
|
- README.md
|
@@ -123,17 +123,17 @@ require_paths:
|
|
123
123
|
- lib
|
124
124
|
required_ruby_version: !ruby/object:Gem::Requirement
|
125
125
|
requirements:
|
126
|
-
- -
|
126
|
+
- - ">="
|
127
127
|
- !ruby/object:Gem::Version
|
128
128
|
version: '0'
|
129
129
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
130
|
requirements:
|
131
|
-
- -
|
131
|
+
- - ">="
|
132
132
|
- !ruby/object:Gem::Version
|
133
133
|
version: '0'
|
134
134
|
requirements: []
|
135
135
|
rubyforge_project:
|
136
|
-
rubygems_version: 2.
|
136
|
+
rubygems_version: 2.2.2
|
137
137
|
signing_key:
|
138
138
|
specification_version: 4
|
139
139
|
summary: A Lita handler for dictionary functionality backed by Wordnik.
|