class IORequest::Authorizer

Class to authorize client connection.

Attributes

data[R]

@return [Object] literally any non-nil data from block.

Public Class Methods

by_secret_key(key) click to toggle source

Secret key authorization.

# File lib/io_request/authorizer.rb, line 35
def Authorizer.by_secret_key(key)
  Authorizer.new do |io_r, io_w|
    io_w.write(key)
    other = io_r.read(key.size)
    key == other ? other : nil
  end
end
empty() click to toggle source

No authorization.

# File lib/io_request/authorizer.rb, line 30
def Authorizer.empty
  Authorizer.new { |_io_r, _io_w| true }
end
new(&block) click to toggle source

@yieldparam io_r [IO] input stream. @yieldparam io_w [IO] output stream. @yieldreturn [Object, nil] if `nil` is returned, authorization will be

considered as failed one. Otherwise data will be saved into `data`.
# File lib/io_request/authorizer.rb, line 10
def initialize(&block)
  @block = block
  @data = nil
end

Public Instance Methods

authorize(io_r, io_w) click to toggle source

@return [Boolean] authorization status.

# File lib/io_request/authorizer.rb, line 19
def authorize(io_r, io_w)
  @data = nil
  @data = @block.call(io_r, io_w)
  !@data.nil?
rescue StandardError => e
  IORequest.logger.error(e.full_message)
  false
end