lib/pandorified/result.rb in pandorified-0.9.3 vs lib/pandorified/result.rb in pandorified-0.9.4
- old
+ new
@@ -1,52 +1,60 @@
+# frozen_string_literal: true
+
require 'rest_client'
require 'nokogiri'
module Pandorified
API_URL = 'https://www.pandorabots.com/pandora/talk-xml'
+ # The result of sending a message to a bot, including the response message if
+ # successful.
class Result
def initialize(params)
@xml = Nokogiri::XML(RestClient.post(API_URL, params))
end
# @return [String] The bot's response to the input.
def that
@that ||= @xml.xpath('/result/that').first.text.strip
end
- alias_method :to_s, :that
+ alias to_s that
# Check the status of this result. See {#success?} and {#error?}.
#
# @return [Number] A status number as returned by Pandorabots.
def status
@status ||= @xml.xpath('/result/@status').first.value.to_i
end
- # @return `true` if this result was successful (no error was returned by Pandorabots), `false` otherwise.
+ # @return `true` if this result was successful (no error was returned by
+ # Pandorabots), `false` otherwise.
def success?
- self.status.zero?
+ status.zero?
end
- alias_method :ok?, :success?
- alias_method :successful?, :success?
+ alias ok? success?
+ alias successful? success?
- # @note After checking if there is an error, you can read the error message with {#message}.
+ # @note After checking if there is an error, you can read the error message
+ # with {#message}.
#
# @return `true` if Pandorabots returned an error.
def error?
- !self.success?
+ !success?
end
- # @return [String] The error message as returned by Pandorabots, if an error occured.
+ # @return [String] The error message as returned by Pandorabots, if an
+ # error occured.
def message
- return nil if self.success?
+ return nil if success?
+
@message ||= @xml.xpath('/result/message').first.text
end
- alias_method :error, :message
- alias_method :error_message, :message
+ alias error message
+ alias error_message message
# @return [String] The botid of the bot this result is for.
def botid
@botid ||= @xml.xpath('/result/@botid').first.value
end