Class: Skein::Handler
- Inherits:
-
Object
- Object
- Skein::Handler
- Defined in:
- lib/skein/handler.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Properties ===========================================================.
Class Method Summary collapse
-
.for(target) ⇒ Object
Class Methods ========================================================.
Instance Method Summary collapse
- #handle(message_json) ⇒ Object
-
#initialize(target, context = nil) ⇒ Handler
constructor
Instance Methods =====================================================.
Constructor Details
#initialize(target, context = nil) ⇒ Handler
Instance Methods =====================================================
19 20 21 22 |
# File 'lib/skein/handler.rb', line 19 def initialize(target, context = nil) @target = target @context = context end |
Instance Attribute Details
#context ⇒ Object (readonly)
Properties ===========================================================
4 5 6 |
# File 'lib/skein/handler.rb', line 4 def context @context end |
Class Method Details
.for(target) ⇒ Object
Class Methods ========================================================
8 9 10 11 12 13 14 15 |
# File 'lib/skein/handler.rb', line 8 def self.for(target) case (target.respond_to?(:async?) and target.async?) when true Skein::Handler::Async.new(target) else Skein::Handler::Threaded.new(target) end end |
Instance Method Details
#handle(message_json) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/skein/handler.rb', line 24 def handle() request = begin JSON.load() rescue Object => e @context and @context.exception!(e, ) return yield(JSON.dump( result: nil, error: '[%s] %s' % [ e.class, e ], id: nil )) end case (request) when Hash # Acceptable else return yield(JSON.dump( result: nil, error: 'Request does not conform to the JSON-RPC format.', id: nil )) end request['params'] = case (params = request['params']) when Array params when nil request.key?('params') ? [ nil ] : [ ] else [ request['params'] ] end unless (request['method'] and request['method'].is_a?(String) and request['method'].match(/\S/)) return yield(JSON.dump( result: nil, error: 'Request does not conform to the JSON-RPC format, missing valid method.', id: request['id'] )) end begin delegate(request['method'], *request['params']) do |result, error = nil| yield(JSON.dump( result: result, error: error, id: request['id'] )) end rescue Object => e @context and @context.exception!(e, ) yield(JSON.dump( result: nil, error: '[%s] %s' % [ e.class, e ], id: request['id'] )) end end |