lib/pandorified/session.rb in pandorified-0.7.1 vs lib/pandorified/session.rb in pandorified-0.8.0

- old
+ new

@@ -1,9 +1,11 @@ require 'pandorified/result' -require 'pandorified/exceptions' module Pandorified + # Raised when Pandorabots returns an API result with a non-zero status. + class PandorabotsError < StandardError; end + class Session # A new session for conversing with a bot. # # @note If you choose not to specify a {custid}, one will be chosen automatically and remembered throughout the session. # @@ -13,21 +15,31 @@ @botid = botid @custid = custid end # Send a message to this session's bot and receive a response. - # If Pandorabots responds with an error, {Pandorified::Exceptions::PandorabotsError} is raised. # + # @note See {Pandorified::Result} for ways to handle the response, or use {#talk!} which raises an exception on errors. + # # @param [String] input Text to say to the bot. # # @return [String] The bot's response text. def talk(input) result = Pandorified::Result.new(botid: @botid, custid: @custid, input: input) - if result.status.zero? - @custid ||= result.custid - result.that - else - raise Pandorified::Exceptions::PandorabotsError, "Pandorabots returned status #{result.status}: #{result.message}" - end + @custid ||= result.custid if result.success? + result + end + + # Send a message to this session's bot and receive a response (if successful). + # If Pandorabots responds with an error, {Pandorified::Exceptions::PandorabotsError} is raised. + # + # @note See {#talk} if you want to check the {Pandorabots::Result} object for success instead of an exception being raised on error. + # + # @param [String] input Text to say to the bot. + # + # @return [String] The bot's response text. + def talk!(input) + result = self.talk(input) + raise Pandorified::PandorabotsError, "Pandorabots returned status #{result.status}: #{result.message}" if result.error? end end end