Class: IORequest::Authorizer

Inherits:
Object
  • Object
show all
Defined in:
lib/io_request/authorizer.rb

Overview

Class to authorize client connection.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|io_r, io_w| ... } ⇒ Authorizer

Returns a new instance of Authorizer.

Yield Parameters:

  • io_r (IO)

    input stream.

  • io_w (IO)

    output stream.

Yield Returns:

  • (Object, nil)

    if ‘nil` is returned, authorization will be considered as failed one. Otherwise data will be saved into `data`.



10
11
12
13
# File 'lib/io_request/authorizer.rb', line 10

def initialize(&block)
  @block = block
  @data = nil
end

Instance Attribute Details

#dataObject (readonly)

Returns literally any non-nil data from block.

Returns:

  • (Object)

    literally any non-nil data from block.



16
17
18
# File 'lib/io_request/authorizer.rb', line 16

def data
  @data
end

Class Method Details

.by_secret_key(key) ⇒ Object

Secret key authorization.



35
36
37
38
39
40
41
# 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

.emptyObject

No authorization.



30
31
32
# File 'lib/io_request/authorizer.rb', line 30

def Authorizer.empty
  Authorizer.new { |_io_r, _io_w| true }
end

Instance Method Details

#authorize(io_r, io_w) ⇒ Boolean

Returns authorization status.

Returns:

  • (Boolean)

    authorization status.



19
20
21
22
23
24
25
26
# 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