class CZTop::ZAP::Response

Represents a ZAP response.

Attributes

meta_data[W]

@return [String] meta data in ZMTP 3.0 format

request_id[RW]

@return [String] the original request ID

status_code[RW]

@return [String] status code @see StatusCodes

status_text[RW]

@return [String] status explanation

user_id[W]

@return [String] the user ID

version[RW]

@return [String] ZAP version

Public Class Methods

from_message(msg) click to toggle source

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 160
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
new(status_code) click to toggle source

Initializes a new response.

@param status_code [String, to_s] ZAP status code

# File lib/cztop/zap.rb, line 214
def initialize(status_code)
  @status_code = status_code.to_s
  raise ArgumentError unless ALL.include?(@status_code)

  @version     = VERSION
end

Public Instance Methods

meta_data() click to toggle source

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 241
def meta_data
  return nil unless success?

  @meta_data
end
success?() click to toggle source

@return [Boolean] whether the authentication was successful

# File lib/cztop/zap.rb, line 223
def success?
  @status_code == SUCCESS
end
to_msg() click to toggle source

Creates a sendable message from this {Response}. @return [CZTop::Message} this request packed into a message

# File lib/cztop/zap.rb, line 250
def to_msg
  fields = [@version, @request_id, @status_code,
            @status_text, @user_id, @meta_data].map(&:to_s)
  CZTop::Message.new(fields)
end
user_id() click to toggle source

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 231
def user_id
  return nil unless success?

  @user_id
end