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



85
86
87
# File 'lib/mcollective/rpc/result.rb', line 85

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

#[](key) ⇒ Object



60
61
62
# File 'lib/mcollective/rpc/result.rb', line 60

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

#[]=(key, item) ⇒ Object



64
65
66
# File 'lib/mcollective/rpc/result.rb', line 64

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

#compatible_key(key) ⇒ Object



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

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
49
50
# 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)

    if output.is_a?(Symbol) && data.include?(output.to_s)
      data[output] = data.delete(output.to_s)
    end
  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

#eachObject



72
73
74
# File 'lib/mcollective/rpc/result.rb', line 72

def each
  @results.each_pair {|k,v| yield(k,v) }
end

#fetch(key, default) ⇒ Object



68
69
70
# File 'lib/mcollective/rpc/result.rb', line 68

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

#to_json(*a) ⇒ Object



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

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