class CZTop::ZAP::Response
Represents a ZAP
response.
Attributes
@return [String] meta data in ZMTP 3.0 format
@return [String] the original request ID
@return [String] status code @see StatusCodes
@return [String] status explanation
@return [String] the user ID
@return [String] ZAP
version
Public Class Methods
Crafts a new {Response} from a message.
@param msg [CZTop::message] the message @return [Response] the response @raise [VersionMismatch] if the message contains an unsupported version @raise [TemporaryError] if the status code indicates a temporary error @raise [InternalError] if the status code indicates an internal error,
or the status code is invalid
# File lib/cztop/zap.rb, line 144 def self.from_message(msg) version, # The version frame, which SHALL contain the three octets "1.0". request_id, # The request id, which MAY contain an opaque binary blob. status_code, # The status code, which SHALL contain a string. status_text, # The status text, which MAY contain a string. user_id, # The user id, which SHALL contain a string. meta_data = # The meta data, which MAY contain a blob. msg.to_a raise VersionMismatch if version != VERSION case status_code when SUCCESS, AUTHENTICATION_FAILURE # valid codes, nothing to do when TEMPORARY_ERROR raise TemporaryError, status_text when INTERNAL_ERROR raise InternalError, status_text else raise InternalError, "invalid status code" end new(status_code).tap do |r| r.version = version r.request_id = request_id r.status_code = status_code r.status_text = status_text r.user_id = user_id r.meta_data = meta_data end end
Initializes a new response.
@param status_code
[String, to_s] ZAP
status code
# File lib/cztop/zap.rb, line 198 def initialize(status_code) @status_code = status_code.to_s raise ArgumentError unless ALL.include?(@status_code) @version = VERSION end
Public Instance Methods
Returns the meta data, if authentication was successful. @return [String] the meta data for the authenticated user @return [nil] if authentication was unsuccessful
# File lib/cztop/zap.rb, line 220 def meta_data return nil unless success? @meta_data end
@return [Boolean] whether the authentication was successful
# File lib/cztop/zap.rb, line 205 def success? @status_code == SUCCESS end
Creates a sendable message from this {Response}. @return [CZTop::Message} this request packed into a message
# File lib/cztop/zap.rb, line 227 def to_msg fields = [@version, @request_id, @status_code, @status_text, @user_id, @meta_data].map(&:to_s) CZTop::Message.new(fields) end
Returns the user ID, if authentication was successful. @return [String] the user ID of the authenticated user @return [nil] if authentication was unsuccessful
# File lib/cztop/zap.rb, line 212 def user_id return nil unless success? @user_id end