lib/arborist/manager/tree_api.rb in arborist-0.0.1.pre20160829140603 vs lib/arborist/manager/tree_api.rb in arborist-0.0.1.pre20161005112841

- old
+ new

@@ -17,11 +17,10 @@ ### Create the TreeAPI handler that will read requests from the specified +pollable+ ### and call into the +manager+ to respond to them. def initialize( pollable, manager ) - self.log.debug "Setting up a %p" % [ self.class ] @pollitem = pollable @enabled = true @manager = manager end @@ -39,12 +38,10 @@ end ### Handle the specified +raw_request+ and return a response. def handle_request( raw_request ) - self.log.debug "Handling request: %p" % [ raw_request ] - raise "Manager is shutting down" unless self.enabled? header, body = self.parse_request( raw_request ) return self.dispatch_request( header, body ) @@ -66,17 +63,15 @@ ### Attempt to dispatch a request given its +header+ and +body+, and return the ### serialized response. def dispatch_request( header, body ) - self.log.debug "Dispatching request %p -> %p" % [ header, body ] handler = self.lookup_request_action( header ) or raise Arborist::RequestError, "No such action '%s'" % [ header['action'] ] response = handler.call( header, body ) - self.log.debug "Returning response: %p" % [ response ] return response end ### Given a request +header+, return a #call-able object that can handle the response. @@ -94,22 +89,20 @@ ### Build an error response message for the specified +category+ and +reason+. def error_response( category, reason ) msg = [ { category: category, reason: reason, success: false, version: 1 } ] - self.log.debug "Returning error response: %p" % [ msg ] return MessagePack.pack( msg ) end ### Build a successful response with the specified +body+. def successful_response( body ) msg = [ { success: true, version: 1 }, body ] - self.log.debug "Returning successful response: %p" % [ msg ] return MessagePack.pack( msg ) end ### Validate and return a parsed msgpack +raw_request+. @@ -118,12 +111,10 @@ MessagePack.unpack( raw_request ) rescue => err raise Arborist::RequestError, err.message end - self.log.debug "Parsed request: %p" % [ tuple ] - raise Arborist::RequestError, 'not a tuple' unless tuple.is_a?( Array ) raise Arborist::RequestError, 'incorrect length' if tuple.length.zero? || tuple.length > 2 header, body = *tuple raise Arborist::RequestError, "header is not a Map" unless @@ -156,12 +147,18 @@ ### Return a response to the `subscribe` action. def handle_subscribe_request( header, body ) self.log.debug "SUBSCRIBE: %p" % [ header ] event_type = header[ 'event_type' ] node_identifier = header[ 'identifier' ] - subscription = @manager.create_subscription( node_identifier, event_type, body ) + body = [ body ] unless body.is_a?( Array ) + positive = body.shift + negative = body.shift || {} + + subscription = @manager. + create_subscription( node_identifier, event_type, positive, negative ) + return successful_response([ subscription.id ]) end ### Return a response to the `unsubscribe` action. @@ -222,9 +219,13 @@ ### Update nodes using the data from the update request's +body+. def handle_update_request( header, body ) self.log.debug "UPDATE: %p" % [ header ] + + unless body.respond_to?( :each ) + return error_response( 'client', 'Malformed update: body does not respond to #each' ) + end body.each do |identifier, properties| @manager.update_node( identifier, properties ) end