Module: Docscribe::Server::Protocol

Defined in:
lib/docscribe/server/protocol.rb

Overview

JSON-line protocol helpers.

Class Method Summary collapse

Class Method Details

.build_request(method, params = {}) ⇒ Hash<Symbol, String, Hash<Symbol, T>>

Note:

module_function: defines #build_request (visibility: private)

Build a JSON-RPC request hash.

Parameters:

  • method (String)

    method name

  • params (Hash<Symbol, T>) (defaults to: {})

    request parameters

Returns:

  • (Hash<Symbol, String, Hash<Symbol, T>>)


18
19
20
21
22
23
24
25
# File 'lib/docscribe/server/protocol.rb', line 18

def build_request(method, params = {})
  {
    jsonrpc: '2.0',
    id: SecureRandom.hex(8),
    method: method,
    params: params
  }
end

.parse_response(line) ⇒ Hash<String, Object>??

Note:

module_function: defines #parse_response (visibility: private)

Parse a single JSON-line response.

Parameters:

  • line (String)

    raw JSON line

Returns:

  • (Hash<String, Object>?)
  • (nil)

    if JSON::ParserError

Raises:

  • (JSON::ParserError)


34
35
36
37
38
# File 'lib/docscribe/server/protocol.rb', line 34

def parse_response(line)
  JSON.parse(line)
rescue JSON::ParserError
  nil
end

.serialize(hash) ⇒ String

Note:

module_function: defines #serialize (visibility: private)

Serialize a hash to a JSON line.

Parameters:

  • hash (Object)

Returns:

  • (String)


45
46
47
# File 'lib/docscribe/server/protocol.rb', line 45

def serialize(hash)
  "#{JSON.generate(hash)}\n"
end