Class: MCollective::RPC::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mcollective/rpc/result.rb

Overview

Simple class to manage compliant results from MCollective::RPC agents

Currently it just fakes Hash behaviour to the result to remain backward compatible but it also knows which agent and action produced it so you can associate results to a DDL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent, action, result = {}) ⇒ Result

Returns a new instance of Result.



13
14
15
16
17
18
19
# File 'lib/mcollective/rpc/result.rb', line 13

def initialize(agent, action, result={})
  @agent = agent
  @action = action
  @results = result

  convert_data_based_on_ddl if ddl
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



9
10
11
# File 'lib/mcollective/rpc/result.rb', line 9

def action
  @action
end

#agentObject (readonly)

Returns the value of attribute agent.



9
10
11
# File 'lib/mcollective/rpc/result.rb', line 9

def agent
  @agent
end

#resultsObject (readonly)

Returns the value of attribute results.



9
10
11
# File 'lib/mcollective/rpc/result.rb', line 9

def results
  @results
end

Instance Method Details

#<=>(other) ⇒ Object



83
84
85
# File 'lib/mcollective/rpc/result.rb', line 83

def <=>(other)
  self[:sender] <=> other[:sender]
end

#[](key) ⇒ Object



58
59
60
# File 'lib/mcollective/rpc/result.rb', line 58

def [](key)
  @results[compatible_key(key)]
end

#[]=(key, item) ⇒ Object



62
63
64
# File 'lib/mcollective/rpc/result.rb', line 62

def []=(key, item)
  @results[key] = item
end

#compatible_key(key) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/mcollective/rpc/result.rb', line 50

def compatible_key(key)
  if key.is_a?(Symbol) && @results.include?(key.to_s)
    key.to_s
  else
    key
  end
end

#convert_data_based_on_ddlObject

Converts keys on the supplied data to those listed as outputs in the DDL. This is to facilitate JSON based transports without forcing everyone to rewrite DDLs and clients to convert symbols to strings, the data will be on symbol keys if the DDL has a symbol and not a string output defined



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mcollective/rpc/result.rb', line 38

def convert_data_based_on_ddl
  interface = ddl.action_interface(action)

  return if interface.fetch(:output, {}).empty?

  interface[:output].each do |output, _properties|
    next if data.include?(output)

    data[output] = data.delete(output.to_s) if output.is_a?(Symbol) && data.include?(output.to_s)
  end
end

#dataObject



27
28
29
30
31
# File 'lib/mcollective/rpc/result.rb', line 27

def data
  @results[:data] = @results.delete("data") if @results.include?("data")

  self[:data]
end

#ddlObject



21
22
23
24
25
# File 'lib/mcollective/rpc/result.rb', line 21

def ddl
  @_ddl ||= DDL.new(agent)
rescue
  nil
end

#each(&block) ⇒ Object



70
71
72
# File 'lib/mcollective/rpc/result.rb', line 70

def each(&block)
  @results.each_pair(&block)
end

#fetch(key, default) ⇒ Object



66
67
68
# File 'lib/mcollective/rpc/result.rb', line 66

def fetch(key, default)
  @results.fetch(compatible_key(key), default)
end

#to_json(*result) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/mcollective/rpc/result.rb', line 74

def to_json(*result)
  {:agent => @agent,
   :action => @action,
   :sender => self[:sender],
   :statuscode => self[:statuscode],
   :statusmsg => self[:statusmsg],
   :data => data}.to_json(*result)
end