Class: RPC::Encoders::Json::Client

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/rpc/lib/rpc/encoders/json.rb

Instance Method Summary collapse

Methods included from Errors

#error, #exception

Instance Method Details

#batch(requests) ⇒ Object

Provide list of requests and notifications to run on the server.

Examples:

["list", ["/"], ["clear", "logs", nil]]


70
71
72
73
74
# File 'lib/rpc/lib/rpc/encoders/json.rb', line 70

def batch(requests)
  data = requests.map { |request| Request.new(*request).data }
  RPC.log "CLIENT ENCODE BATCH #{data.inspect}"
  data.to_json
end

#decode(binary) ⇒ Object

TODO: support batch



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rpc/lib/rpc/encoders/json.rb', line 77

def decode(binary)
  if binary.nil?
    raise TypeError.new("#{self.class}#decode takes binary data as an argument, not nil!")
  end

  object = JSON.parse(binary)
  RPC.log "CLIENT DECODE #{object.inspect}"
  object
rescue JSON::ParserError => error
  self.exception(error, -32600, "Invalid Request.")
end

#encode(method, *args) ⇒ Object



52
53
54
55
56
# File 'lib/rpc/lib/rpc/encoders/json.rb', line 52

def encode(method, *args)
  data = Request.new(method, args).data
  RPC.log "CLIENT ENCODE #{data.inspect}"
  data.to_json
end

#notification(method, *args) ⇒ Object

Notifications are calls which don’t require response. They look just the same, but they don’t have any id.



60
61
62
63
64
# File 'lib/rpc/lib/rpc/encoders/json.rb', line 60

def notification(method, *args)
  data = Request.new(method, args, nil).data
  RPC.log "CLIENT ENCODE NOTIFICATION #{data.inspect}"
  data.to_json
end