class RuboCop::Daemon::SocketReader

Constants

Request

Public Class Methods

new(socket, verbose) click to toggle source
# File lib/rubocop/daemon/socket_reader.rb, line 9
def initialize(socket, verbose)
  @socket = socket
  @verbose = verbose
end

Public Instance Methods

read!() click to toggle source
# File lib/rubocop/daemon/socket_reader.rb, line 14
def read!
  request = parse_request(@socket.read)

  Helper.redirect(
    stdin: StringIO.new(request.body),
    stdout: @socket,
    stderr: @socket,
  ) do
    create_command_instance(request).run
  end
end

Private Instance Methods

create_command_instance(request) click to toggle source
# File lib/rubocop/daemon/socket_reader.rb, line 43
def create_command_instance(request)
  klass = find_command_class(request.header.command)

  klass.new(
    request.header.args,
    token: request.header.token,
    cwd: request.header.cwd,
  )
end
find_command_class(command) click to toggle source
# File lib/rubocop/daemon/socket_reader.rb, line 53
def find_command_class(command)
  case command
  when 'stop' then ServerCommand::Stop
  when 'exec' then ServerCommand::Exec
  else
    raise UnknownServerCommandError, "#{command.inspect} is not a valid command"
  end
end
parse_header(header) click to toggle source
# File lib/rubocop/daemon/socket_reader.rb, line 38
def parse_header(header)
  token, cwd, command, *args = header.shellsplit
  Header.new(token, cwd, command, args)
end
parse_request(content) click to toggle source
# File lib/rubocop/daemon/socket_reader.rb, line 28
def parse_request(content)
  raw_header, *body = content.lines
  if @verbose
    puts raw_header.to_s
    puts "STDIN: #{body.size} lines" if body.any?
  end

  Request.new(parse_header(raw_header), body.join)
end