Class: Shiolink

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

Overview

SHIOLINK interface

Instance Method Summary collapse

Constructor Details

#initialize(load, unload, request) ⇒ Shiolink

initialize with callbacks

Parameters:

  • load (Proc)

    SHIORI load(dirpath)

  • unload (Proc)

    SHIORI unload()

  • request (Proc)

    SHIORI request(request_str)


7
8
9
10
11
12
# File 'lib/shiolink.rb', line 7

def initialize(load, unload, request)
  @load = load
  @unload = unload
  @request = request
  @session = nil
end

Instance Method Details

#add_line(line) ⇒ String

add line

Parameters:

  • line (String)

    line that ends with ā€œnā€

Returns:

  • (String)

    SHIOLINK protocol response string or nil


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 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) ⇒ Object

start watching IO

Parameters:

  • input (IO) (defaults to: $stdin)

    readable stream

  • output (IO) (defaults to: $stdout)

    writable stream


46
47
48
49
50
51
52
53
# 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