Module: JSON_RPC::Parser

Defined in:
lib/json_rpc/parser.rb

Class Method Summary collapse

Class Method Details

.array_from_json(json) ⇒ Object

Convert raw JSON string into typed object(s).



19
20
21
22
23
24
25
26
# File 'lib/json_rpc/parser.rb', line 19

def self.array_from_json(json)
  raw = ActiveSupport::JSON.decode(json)
  case raw
  when Hash  then object_from_hash(raw)
  when Array then raw.map { |h| object_from_hash(h) }
  else            raw # let validator scream later
  end
end

.object_from_hash(h) ⇒ Object

Convert one JSON-RPC hash into a typed object.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/json_rpc/parser.rb', line 6

def self.object_from_hash(h)
  # Order matters: responses have id *plus* result/error,
  # so check for those keys first.
  if h.key?("result") || h.key?("error")
    Response.from_h(h)
  elsif h.key?("id")
    Request.from_h(h)          # request (id may be nil)
  else
    Notification.from_h(h)     # no id ⇒ notification
  end
end