Module: Orbjson::Common

Included in:
CGI_JSON_RPC, WEBrick_JSON_RPC
Defined in:
lib/orbjson.rb

Overview

Assorted method used by both CGI and WEBrick Orbjson classes

Instance Method Summary collapse

Instance Method Details

#format_repsonse(result, error, id) ⇒ Object

Takes the result of some method, plus the id of the originating JSON-RP call, and creates the JSON-RPC reponse



147
148
149
150
151
152
# File 'lib/orbjson.rb', line 147

def format_repsonse( result, error, id )
  id.class == String ? quote = '"' : quote = ''
  '{ "result" : ' + result.to_json + ', "error":"' + 
   error.to_s + '", "id": ' +  quote +  id.to_s + 
   quote + ' }'
end

#process(json) ⇒ Object

Takes a JSON-RPC message, figures out what it means in Ruby, invokes the request method on the correspnding service, and returns the results



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/orbjson.rb', line 157

def process( json )
  json   = "#{json}"
  null=nil;  
  jsonobj = {}.from_json( JSON::Lexer.new(json ) ) 
  klass, method = jsonobj[ 'method'].split( '.' ) 
  $roy_logger.debug( "klass, method  = #{klass}, #{method}" )
  receiver = System.get_object( klass.capitalize )
  begin
    if ( jsonobj[ 'params'] && jsonobj[ 'params'] != 'nil' )
      if jsonobj[ 'params'].size < 1
        result = receiver.send( method  )                  
      else
        result = receiver.send( method , *jsonobj[ 'params']   )
      end
    else
      result = receiver.send( method  )        
    end
    error = null
  rescue
    result = null
    error = $!.to_s
  end
  format_repsonse( result, error, jsonobj['id'] || 'default' )
end