Class: Lib::BOOTP::Packet::OpCode

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/lib/bootp/packet/op_code.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op = 1) ⇒ OpCode

Returns a new instance of OpCode.

Raises:

  • (ArgumentError)


19
20
21
22
# File 'lib/lib/bootp/packet/op_code.rb', line 19

def initialize(op=1)
  @op = op.is_a?(Hash) ? op.transform_keys(&:to_sym)[:code].to_i : op
  raise ArgumentError, "OP Code out of range : #{@op}" unless [1,2].include? @op.to_i
end

Class Method Details

.unpack(op) ⇒ Object



52
53
54
# File 'lib/lib/bootp/packet/op_code.rb', line 52

def self.unpack(op)
  self.new op.unpack('C').first
end

Instance Method Details

#<=>(other) ⇒ Object



43
44
45
46
# File 'lib/lib/bootp/packet/op_code.rb', line 43

def <=>(other)
  return self.to_i <=> other if other.is_a?(Integer)
  self.to_s <=> other.to_s.upcase
end

#packObject



48
49
50
# File 'lib/lib/bootp/packet/op_code.rb', line 48

def pack
  [@op.to_i].pack('C')
end

#to_hObject



36
37
38
39
40
41
# File 'lib/lib/bootp/packet/op_code.rb', line 36

def to_h
  {
    code: @op.to_s,
    name: self.to_s
  }
end

#to_json(*params) ⇒ Object



32
33
34
# File 'lib/lib/bootp/packet/op_code.rb', line 32

def to_json(*params)
  self.to_h.to_json
end

#to_sObject



24
25
26
27
28
29
30
# File 'lib/lib/bootp/packet/op_code.rb', line 24

def to_s
  if @op == 1
    'BOOTREQUEST'
  elsif @op == 2
    'BOOTREPLY'
  end
end