PageRenderTime 58ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/mcollective/rpc/request.rb

https://github.com/richardc/marionette-collective
Ruby | 62 lines | 46 code | 9 blank | 7 comment | 2 complexity | 6661d76a2ac596c5f7086b7c6b941cd0 MD5 | raw file
  1. module MCollective
  2. module RPC
  3. # Simple class to manage compliant requests for MCollective::RPC agents
  4. class Request
  5. attr_accessor :time, :action, :data, :sender, :agent, :uniqid, :caller, :ddl
  6. def initialize(msg, ddl)
  7. @time = msg[:msgtime]
  8. @action = msg[:body][:action]
  9. @data = msg[:body][:data]
  10. @sender = msg[:senderid]
  11. @agent = msg[:body][:agent]
  12. @uniqid = msg[:requestid]
  13. @caller = msg[:callerid] || "unknown"
  14. @ddl = ddl
  15. end
  16. # If data is a hash, quick helper to get access to it's include? method
  17. # else returns false
  18. def include?(key)
  19. return false unless @data.is_a?(Hash)
  20. return @data.include?(key)
  21. end
  22. # If no :process_results is specified always respond else respond
  23. # based on the supplied property
  24. def should_respond?
  25. return @data[:process_results] if @data.include?(:process_results)
  26. return true
  27. end
  28. # If data is a hash, gives easy access to its members, else returns nil
  29. def [](key)
  30. return nil unless @data.is_a?(Hash)
  31. return @data[key]
  32. end
  33. def fetch(key, default)
  34. return nil unless @data.is_a?(Hash)
  35. return @data.fetch(key, default)
  36. end
  37. def to_hash
  38. return {:agent => @agent,
  39. :action => @action,
  40. :data => @data}
  41. end
  42. # Validate the request against the DDL
  43. def validate!
  44. @ddl.validate_rpc_request(@action, @data)
  45. end
  46. def to_json
  47. to_hash.merge!({:sender => @sender,
  48. :callerid => @callerid,
  49. :uniqid => @uniqid}).to_json
  50. end
  51. end
  52. end
  53. end