Class: CodinRep::EmployeeCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/codin_rep/employee_command.rb

Direct Known Subclasses

AddEmployee, DelEmployee

Defined Under Namespace

Classes: InvalidInteger, UnknownEmployeeOperation

Constant Summary collapse

CODE_MAX_SIZE =
12

Constants inherited from Command

Command::COMMAND_PREFIX

Instance Method Summary collapse

Methods inherited from Command

#generate_command_payload, #generate_header, #get_max_attempts, #get_timeout_time, #should_close_connection?

Constructor Details

#initialize(*args) ⇒ EmployeeCommand

Returns a new instance of EmployeeCommand.



47
48
49
50
# File 'lib/codin_rep/employee_command.rb', line 47

def initialize(*args)
  super(*args)
  @sent_registration = false
end

Instance Method Details

#check_response_headerObject



75
76
77
78
79
80
# File 'lib/codin_rep/employee_command.rb', line 75

def check_response_header
  expected_header = self.class::EXPECTED_HEADER
  unless @response.match(/^#{expected_header}/)
    raise UnknownHeader.new @response[0..expected_header.size], 'set employee', expected_header
  end
end

#executeObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/codin_rep/employee_command.rb', line 52

def execute
  @command_data = generate_header
  @command_data += generate_command_payload
  @response = communicate!
  check_response_header
  @sent_registration = true
  @command_data = self.class::REGISTRATION_COMPLETED_HEADER
  @response = communicate!
  # The REP requires some time to process the employee commands, so there's
  # this artificial sleep here to do it.
  sleep 0.1
  return get_data_from_response_payload
rescue
  @communication.close
  raise
ensure
  @communication.close if should_close_connection?
end

#get_data_from_response_payloadObject



86
87
88
# File 'lib/codin_rep/employee_command.rb', line 86

def get_data_from_response_payload
  true
end

#get_expected_response_sizeObject



71
72
73
# File 'lib/codin_rep/employee_command.rb', line 71

def get_expected_response_size
  @sent_registration ? 0 : self.class::EXPECTED_HEADER.size
end

#get_response_payloadObject



82
83
84
# File 'lib/codin_rep/employee_command.rb', line 82

def get_response_payload
  @sent_employee_data = true
end