Class: Htcp::Message::Data

Inherits:
Base
  • Object
show all
Defined in:
lib/htcp/message/data.rb

Instance Method Summary collapse

Methods inherited from Base

#encode_int16, #encode_int32, #encode_int8, #encode_string

Constructor Details

#initialize(params) ⇒ Data

Returns a new instance of Data.



4
5
6
# File 'lib/htcp/message/data.rb', line 4

def initialize(params)
  @params = params
end

Instance Method Details

#to_sObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/htcp/message/data.rb', line 8

def to_s
  # Process data fields
  rr = @params[:response] ? 1 : 0
  response_code = @params[:response] || 0
  f1 = (rr.zero?) ? @params[:rd] : @params[:mo]

  # Compose request
  request = ''

  request << encode_int8(response_code << 4 | @params[:opcode])
  request << encode_int8((f1 << 1) | rr)
  request << encode_int32(@params[:trans_id])
  request << @params[:op_data].to_s

  # Encode request with string
  encode_int16(request.length + 2) + request
end