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)


214
215
216
217
218
219
# 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

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



241
242
243
244
245
# File 'lib/cztop/zap.rb', line 241

def 
  return nil unless success?

  @meta_data
end

#request_idString

Returns the original request ID.

Returns:

  • (String)

    the original request ID



196
197
198
# File 'lib/cztop/zap.rb', line 196

def request_id
  @request_id
end

#status_codeString

Returns status code.

Returns:

  • (String)

    status code

See Also:



200
201
202
# File 'lib/cztop/zap.rb', line 200

def status_code
  @status_code
end

#status_textString

Returns status explanation.

Returns:

  • (String)

    status explanation



203
204
205
# File 'lib/cztop/zap.rb', line 203

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



231
232
233
234
235
# File 'lib/cztop/zap.rb', line 231

def user_id
  return nil unless success?

  @user_id
end

#versionString

Returns ZAP version.

Returns:

  • (String)

    ZAP version



193
194
195
# File 'lib/cztop/zap.rb', line 193

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



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# 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.
   =  # 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



223
224
225
# File 'lib/cztop/zap.rb', line 223

def success?
  @status_code == SUCCESS
end

#to_msgCZTop::Message

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

Returns:



250
251
252
253
254
# 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