class Shiolink

SHIOLINK interface

Public Class Methods

new(load, unload, request) click to toggle source

initialize with callbacks @param [Proc] load SHIORI load(dirpath) @param [Proc] unload SHIORI unload() @param [Proc] request SHIORI request(request_str)

# File lib/shiolink.rb, line 7
def initialize(load, unload, request)
  @load = load
  @unload = unload
  @request = request
  @session = nil
end

Public Instance Methods

add_line(line) click to toggle source

add line @param [String] line line that ends with ā€œnā€ @return [String] SHIOLINK protocol response string or nil

# File lib/shiolink.rb, line 17
def add_line(line)
  unless @session
    case line[0 ... 3]
    when '*L:'
      path = line[3 ... line.size].chomp + line.match(/[\/\\]/).to_s
      @load.call(path)
      nil
    when '*S:'
      @session = ''
      line
    when '*U:'
      @unload.call
      nil
    end
  else
    @session += line
    if line.chomp.size != 0
      nil
    else
      response = @request.call(@session)
      @session = nil
      response
    end
  end
end
start(input = $stdin, output = $stdout) click to toggle source

start watching IO @param [IO] input readable stream @param [IO] output writable stream

# File lib/shiolink.rb, line 46
def start(input = $stdin, output = $stdout)
  input.each_line do |line|
    if response = add_line(line)
      output.print(response)
      #output.flush
    end
  end
end