Class: RubyRobot::Grpc::RobotService

Inherits:
RubyRobot::Service show all
Defined in:
lib/ruby_robot/grpc/robot_service.rb

Instance Method Summary collapse

Instance Method Details

#left(empty, _unused_call) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/ruby_robot/grpc/robot_service.rb', line 48

def left(empty, _unused_call)
  logger.info "#{__method__} called (robot.placed?==#{robot.placed?})"
  if robot.placed?
    robot.LEFT
    position_report
  else
    not_placed_error
  end
end

#loggerObject



14
15
16
17
18
# File 'lib/ruby_robot/grpc/robot_service.rb', line 14

def logger
  @logger ||= proc {
    Logger.new(STDOUT)
  }.call
end

#move(empty, _unused_call) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/ruby_robot/grpc/robot_service.rb', line 58

def move(empty, _unused_call)
  logger.info "#{__method__} called (robot.placed?==#{robot.placed?})"
  if robot.placed?
    robot.MOVE
    position_report
  else
    not_placed_error
  end
end

#not_placed_errorObject



30
31
32
33
34
# File 'lib/ruby_robot/grpc/robot_service.rb', line 30

def not_placed_error
  ::RubyRobot::RubyRobotResponse.new(
    error: ::RubyRobot::RubyRobotError.new(error: 400, message: "Robot is not currently placed")
  )
end

#place(robot_request, _unused_call) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/ruby_robot/grpc/robot_service.rb', line 68

def place(robot_request, _unused_call)
  input = { x: robot_request.x, y: robot_request.y, direction: robot_request.direction }
  logger.info "#{__method__} called with #{input.to_json}"

  robot.PLACE(robot_request.x, robot_request.y, robot_request.direction.to_s.downcase)
  position_report
end

#position_reportObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby_robot/grpc/robot_service.rb', line 36

def position_report
  rr = robot.REPORT(false)

  ::RubyRobot::RubyRobotResponse.new(
    current_state: ::RubyRobot::RubyRobotRequest.new(
      x:         rr[:x],
      y:         rr[:y],
      direction: rr[:direction].to_s.upcase.to_sym
    )
  )
end

#remove(empty, _unused_call) ⇒ Object



76
77
78
79
80
# File 'lib/ruby_robot/grpc/robot_service.rb', line 76

def remove(empty, _unused_call)
  logger.info "#{__method__} called"
  @@robot = nil
  ::Google::Protobuf::Empty.new
end

#report(empty, _unused_call) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/ruby_robot/grpc/robot_service.rb', line 82

def report(empty, _unused_call)
  logger.info "#{__method__} called (robot.placed?==#{robot.placed?})"
  if robot.placed?
    position_report
  else
    not_placed_error
  end
end

#right(empty, _unused_call) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/ruby_robot/grpc/robot_service.rb', line 91

def right(empty, _unused_call)
  logger.info "#{__method__} called (robot.placed?==#{robot.placed?})"
  if robot.placed?
    robot.RIGHT
    position_report
  else
    not_placed_error
  end
end

#robotObject



20
21
22
23
24
25
26
27
28
# File 'lib/ruby_robot/grpc/robot_service.rb', line 20

def robot
  @@robot ||= proc {
    rr = ::RubyRobot::Shell.new
    def rr.placed?
      !@robot.nil?
    end
    rr
  }.call
end