pandorified 0.7.1 → 0.8.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/Rakefile +5 -0
- data/lib/pandorified/result.rb +25 -0
- data/lib/pandorified/session.rb +20 -8
- data/lib/pandorified/version.rb +1 -1
- metadata +1 -2
- data/lib/pandorified/exceptions.rb +0 -7
data/Rakefile
CHANGED
data/lib/pandorified/result.rb
CHANGED
@@ -9,28 +9,53 @@ module Pandorified
|
|
9
9
|
@xml = Nokogiri::XML(RestClient.post(API_URL, params))
|
10
10
|
end
|
11
11
|
|
12
|
+
# Check the status of this result. See {#success?} and {#error?}.
|
13
|
+
#
|
14
|
+
# @return [Number] A status number as returned by Pandorabots.
|
12
15
|
def status
|
13
16
|
@status ||= @xml.xpath('/result/@status').first.value.to_i
|
14
17
|
end
|
15
18
|
|
19
|
+
# @return [String] The botid of the bot this result is for.
|
16
20
|
def botid
|
17
21
|
@botid ||= @xml.xpath('/result/@botid').first.value
|
18
22
|
end
|
19
23
|
|
24
|
+
# @return [String] The custid for this session.
|
20
25
|
def custid
|
21
26
|
@custid ||= @xml.xpath('/result/@custid').first.value
|
22
27
|
end
|
23
28
|
|
29
|
+
# @return [String] The orginal input that triggered this response.
|
24
30
|
def input
|
25
31
|
@input ||= @xml.xpath('/result/input').first.text
|
26
32
|
end
|
27
33
|
|
34
|
+
# @return [String] The bot's response to the input.
|
28
35
|
def that
|
29
36
|
@that ||= @xml.xpath('/result/that').first.text
|
30
37
|
end
|
31
38
|
|
39
|
+
# @return [String] The error message as returned by Pandorabots, if an error occured.
|
32
40
|
def message
|
33
41
|
@message ||= @xml.xpath('/result/message').first.text
|
34
42
|
end
|
43
|
+
|
44
|
+
# @return `true` if this result was successful (no error was returned by Pandorabots), `false` otherwise.
|
45
|
+
def success?
|
46
|
+
self.status.zero?
|
47
|
+
end
|
48
|
+
|
49
|
+
alias_method :ok?, :success?
|
50
|
+
alias_method :successful?, :success?
|
51
|
+
|
52
|
+
# @note After checking if there is an error, you can read the error message with {#message}.
|
53
|
+
#
|
54
|
+
# @return `true` if Pandorabots returned an error.
|
55
|
+
def error?
|
56
|
+
!self.success?
|
57
|
+
end
|
58
|
+
|
59
|
+
alias_method :to_s, :that
|
35
60
|
end
|
36
61
|
end
|
data/lib/pandorified/session.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
require 'pandorified/result'
|
2
|
-
require 'pandorified/exceptions'
|
3
2
|
|
4
3
|
module Pandorified
|
4
|
+
# Raised when Pandorabots returns an API result with a non-zero status.
|
5
|
+
class PandorabotsError < StandardError; end
|
6
|
+
|
5
7
|
class Session
|
6
8
|
# A new session for conversing with a bot.
|
7
9
|
#
|
@@ -15,19 +17,29 @@ module Pandorified
|
|
15
17
|
end
|
16
18
|
|
17
19
|
# Send a message to this session's bot and receive a response.
|
18
|
-
#
|
20
|
+
#
|
21
|
+
# @note See {Pandorified::Result} for ways to handle the response, or use {#talk!} which raises an exception on errors.
|
19
22
|
#
|
20
23
|
# @param [String] input Text to say to the bot.
|
21
24
|
#
|
22
25
|
# @return [String] The bot's response text.
|
23
26
|
def talk(input)
|
24
27
|
result = Pandorified::Result.new(botid: @botid, custid: @custid, input: input)
|
25
|
-
if result.
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
28
|
+
@custid ||= result.custid if result.success?
|
29
|
+
result
|
30
|
+
end
|
31
|
+
|
32
|
+
# Send a message to this session's bot and receive a response (if successful).
|
33
|
+
# If Pandorabots responds with an error, {Pandorified::Exceptions::PandorabotsError} is raised.
|
34
|
+
#
|
35
|
+
# @note See {#talk} if you want to check the {Pandorabots::Result} object for success instead of an exception being raised on error.
|
36
|
+
#
|
37
|
+
# @param [String] input Text to say to the bot.
|
38
|
+
#
|
39
|
+
# @return [String] The bot's response text.
|
40
|
+
def talk!(input)
|
41
|
+
result = self.talk(input)
|
42
|
+
raise Pandorified::PandorabotsError, "Pandorabots returned status #{result.status}: #{result.message}" if result.error?
|
31
43
|
end
|
32
44
|
end
|
33
45
|
end
|
data/lib/pandorified/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pandorified
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.8.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -56,7 +56,6 @@ files:
|
|
56
56
|
- README.md
|
57
57
|
- Rakefile
|
58
58
|
- lib/pandorified.rb
|
59
|
-
- lib/pandorified/exceptions.rb
|
60
59
|
- lib/pandorified/result.rb
|
61
60
|
- lib/pandorified/session.rb
|
62
61
|
- lib/pandorified/version.rb
|