Class: SonicPi

Inherits:
Object
  • Object
show all
Defined in:
lib/sonic_pi.rb

Constant Summary collapse

PORT_LOG_REGEX =
Regexp.compile(/Listen port:\s+(?<port>\d+)/)
RUN_COMMAND =
"/run-code"
STOP_COMMAND =
"/stop-all-jobs"
SERVER =
'localhost'
GUI_ID =
'SONIC_PI_CLI'

Instance Method Summary collapse

Constructor Details

#initialize(port = nil) ⇒ SonicPi

Returns a new instance of SonicPi.



9
10
11
# File 'lib/sonic_pi.rb', line 9

def initialize(port=nil)
  @port = port || find_port
end

Instance Method Details

#run(command) ⇒ Object



18
19
20
# File 'lib/sonic_pi.rb', line 18

def run(command)
  send_command(RUN_COMMAND, command)
end

#stopObject



22
23
24
# File 'lib/sonic_pi.rb', line 22

def stop
  send_command(STOP_COMMAND)
end

#test_connection!Object



26
27
28
29
30
31
32
33
34
# File 'lib/sonic_pi.rb', line 26

def test_connection!
  begin
    socket = UDPSocket.new
    socket.bind(nil, @port)
    abort("ERROR: Sonic Pi is not listening on #{@port} - is it running?")
  rescue
    # everything is good
  end
end