Module: Docscribe::Server::Protocol

Defined in:
lib/docscribe/server.rb

Overview

JSON-line protocol helpers.

Class Method Summary collapse

Class Method Details

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

Note:

module_function: defines #build_request (visibility: private)

Build a JSON-RPC request hash.

Parameters:

  • method (String)

    method name

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

    request parameters

Returns:

  • (Hash<Symbol, Object>)


222
223
224
225
226
227
228
229
# File 'lib/docscribe/server.rb', line 222

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)


238
239
240
241
242
# File 'lib/docscribe/server.rb', line 238

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 (Hash<Object, Object>)

Returns:

  • (String)


249
250
251
# File 'lib/docscribe/server.rb', line 249

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