Class: Utils::IRB::IRBServer

Inherits:
Object
  • Object
show all
Includes:
XDG
Defined in:
lib/utils/irb/irb_server.rb

Overview

A class that provides server functionality for interactive Ruby (IRB) sessions.

This class manages an IRB server instance that can receive and process code snippets for evaluation. It handles communication through Unix domain sockets, allowing external clients to send code for execution and receive the results back.

Examples:

server = Utils::IRB::IRBServer.new(url: 'unix:///tmp/irb.sock')
server.start
#
client = Utils::IRB::IRBServer.new(url: 'unix:///tmp/irb.sock')
client.store_snippet('puts "Hello World"')
result = server.eval_snippet('2 + 2')

Constant Summary

Constants included from XDG

XDG::XDG_CACHE_HOME, XDG::XDG_CONFIG_HOME, XDG::XDG_DATA_HOME, XDG::XDG_STATE_HOME

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, log_out: nil) ⇒ IRBServer

The initialize method sets up a new IRBServer instance with the specified URL.

This method configures the IRB server by initializing a logger for error reporting and storing the provided URL for server communication.



29
30
31
32
# File 'lib/utils/irb/irb_server.rb', line 29

def initialize(url:, log_out: nil)
  @url     = url
  @log_out = log_out
end

Instance Attribute Details

#snippetObject (readonly)

The snippet reader method provides access to the snippet instance variable.



42
43
44
# File 'lib/utils/irb/irb_server.rb', line 42

def snippet
  @snippet
end

#urlString (readonly)

The url reader method provides access to the URL instance variable.



37
38
39
# File 'lib/utils/irb/irb_server.rb', line 37

def url
  @url
end

Instance Method Details

#eval_lines(lines, prefix = ' # => ') ⇒ String

The eval_lines method sends a series of code lines to the IRB server for evaluation and returns the formatted results.

This method transmits multiple lines of code to the IRB server for execution, with each line being evaluated in sequence. It includes a prefix for each result line and returns the combined output from the evaluation.



146
147
148
149
150
151
# File 'lib/utils/irb/irb_server.rb', line 146

def eval_lines(lines, prefix = ' # => ')
  message = build_client.transmit_with_response(
    { action: 'eval_lines', prefix:, lines: }
  )
  message.result
end

#eval_snippet(code) ⇒ String

The eval_snippet method sends a code snippet to the IRB server for evaluation and returns the result.

This method transmits the provided code snippet to the IRB server for execution, waits for the server’s response, and extracts the evaluation result from the response.



128
129
130
131
132
133
# File 'lib/utils/irb/irb_server.rb', line 128

def eval_snippet(code)
  message = build_client.transmit_with_response(
    { action: 'eval', snippet: code }
  )
  message.result
end

#execute_snippet(code) ⇒ Utils::IRB::IRBServer

The execute_snippet method sends a code snippet to the IRB server for execution and waits for the response.

This method transmits an execute command along with the provided code snippet to the IRB server, allowing the server to evaluate the code and return the result.



113
114
115
116
# File 'lib/utils/irb/irb_server.rb', line 113

def execute_snippet(code)
  build_client.transmit({ action: 'execute', snippet: code })
  self
end

#startUtils::IRB::IRBServer

The start method initializes and begins operation of the IRB server.

This method sets up the server by building the underlying socket connection, logging the start event, and configuring a background receiver to handle incoming messages. It processes different message actions such as storing code snippets or evaluating code, and responds appropriately to each message type.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/utils/irb/irb_server.rb', line 53

def start
  @server = build_server
  @logger.info "Starting #{self.class.name} server on #{@url}."
  @server.receive_in_background do |message|
    case message.action
    when 'store'
      @snippet = message.snippet
      @logger.info "Stored #{message.to_json}."
    when 'execute'
      time_eval { eval(message.snippet) }
      @logger.info "Execution of #{message.to_json} took %.2fs" % @eval_duration
    when 'eval'
      result = time_eval { eval(message.snippet) }
      @logger.info "Evaluation of #{message.to_json} took %.2fs" % @eval_duration
      message.respond(result: result.to_s, type: message.action)
    when 'eval_lines'
      b = binding
      result = message.lines.map do |line|
        l = line.chomp
        r = b.eval(l)
        [ l, message.prefix, r ].join
      end.join(?\n)
      message.respond(result: result, type: message.action)
    when 'stop'
      @logger.info "Stopping #{self.class.name} server on #{@url}."
      Thread.current.exit
    else
      @logger.warn("Message for action #{message.action.inspect} not supported.")
    end
  rescue => e
    @logger.error("#{self.class.name} caught #{e.class}: #{e} for #{message.to_json}.")
  end
  self
end

#stop_servernil

The stop_server method sends a stop command to the IRB server.

This method communicates with the IRB server to request a graceful shutdown of the server process by transmitting a stop action.



159
160
161
162
# File 'lib/utils/irb/irb_server.rb', line 159

def stop_server
  build_client.transmit({ action: 'stop' })
  nil
end

#store_snippet(code) ⇒ Utils::IRB::IRBServer

The store_snippet method transmits a code snippet to the client for storage.

This method prepares a transmission request containing the specified code snippet and sends it to the client using the build_client mechanism. It is designed to facilitate the storage of code snippets within the system’s communication protocol.



98
99
100
101
# File 'lib/utils/irb/irb_server.rb', line 98

def store_snippet(code)
  build_client.transmit({ action: 'store', snippet: code })
  self
end