Class: CZTop::ZAP::Response

Inherits:
Object
  • Object
show all
Includes:
StatusCodes
Defined in:
lib/cztop/zap.rb

Overview

Represents a ZAP response.

Defined Under Namespace

Modules: StatusCodes Classes: InternalError, TemporaryError

Constant Summary

Constants included from StatusCodes

StatusCodes::ALL, StatusCodes::AUTHENTICATION_FAILURE, StatusCodes::INTERNAL_ERROR, StatusCodes::SUCCESS, StatusCodes::TEMPORARY_ERROR

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status_code) ⇒ Response

Initializes a new response.

Parameters:

  • status_code (String, #to_s)

    ZAP status code

Raises:

  • (ArgumentError)


198
199
200
201
202
# 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

Instance Attribute Details

#meta_dataString?

Returns the meta data, if authentication was successful.

Returns:

  • (String)

    the meta data for the authenticated user

  • (nil)

    if authentication was unsuccessful



220
221
222
223
# File 'lib/cztop/zap.rb', line 220

def 
  return nil unless success?
  @meta_data
end

#request_idString

Returns the original request ID.

Returns:

  • (String)

    the original request ID



180
181
182
# File 'lib/cztop/zap.rb', line 180

def request_id
  @request_id
end

#status_codeString

Returns status code.

Returns:

  • (String)

    status code

See Also:



184
185
186
# File 'lib/cztop/zap.rb', line 184

def status_code
  @status_code
end

#status_textString

Returns status explanation.

Returns:

  • (String)

    status explanation



187
188
189
# File 'lib/cztop/zap.rb', line 187

def status_text
  @status_text
end

#user_idString?

Returns the user ID, if authentication was successful.

Returns:

  • (String)

    the user ID of the authenticated user

  • (nil)

    if authentication was unsuccessful



212
213
214
215
# File 'lib/cztop/zap.rb', line 212

def user_id
  return nil unless success?
  @user_id
end

#versionString

Returns ZAP version.

Returns:

  • (String)

    ZAP version



177
178
179
# File 'lib/cztop/zap.rb', line 177

def version
  @version
end

Class Method Details

.from_message(msg) ⇒ Response

Crafts a new CZTop::ZAP::Response from a message.

Parameters:

  • msg (CZTop::message)

    the message

Returns:

Raises:

  • (VersionMismatch)

    if the message contains an unsupported version

  • (TemporaryError)

    if the status code indicates a temporary error

  • (InternalError)

    if the status code indicates an internal error, or the status code is invalid



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# 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.
   =  # 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. = 
  end
end

Instance Method Details

#success?Boolean

Returns whether the authentication was successful.

Returns:

  • (Boolean)

    whether the authentication was successful



205
206
207
# File 'lib/cztop/zap.rb', line 205

def success?
  @status_code == SUCCESS
end

#to_msgCZTop::Message

Creates a sendable message from this CZTop::ZAP::Response.

Returns:



227
228
229
230
231
# 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