Class: MCollective::RPC::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/mcollective/rpc/request.rb

Overview

Simple class to manage compliant requests for MCollective::RPC agents

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msg, ddl) ⇒ Request

Returns a new instance of Request.



7
8
9
10
11
12
13
14
15
16
# File 'lib/mcollective/rpc/request.rb', line 7

def initialize(msg, ddl)
  @time = msg[:msgtime]
  @action = msg[:body][:action]
  @data = msg[:body][:data]
  @sender = msg[:senderid]
  @agent = msg[:body][:agent]
  @uniqid = msg[:requestid]
  @caller = msg[:callerid] || "unknown"
  @ddl = ddl
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



5
6
7
# File 'lib/mcollective/rpc/request.rb', line 5

def action
  @action
end

#agentObject

Returns the value of attribute agent.



5
6
7
# File 'lib/mcollective/rpc/request.rb', line 5

def agent
  @agent
end

#callerObject

Returns the value of attribute caller.



5
6
7
# File 'lib/mcollective/rpc/request.rb', line 5

def caller
  @caller
end

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/mcollective/rpc/request.rb', line 5

def data
  @data
end

#ddlObject

Returns the value of attribute ddl.



5
6
7
# File 'lib/mcollective/rpc/request.rb', line 5

def ddl
  @ddl
end

#senderObject

Returns the value of attribute sender.



5
6
7
# File 'lib/mcollective/rpc/request.rb', line 5

def sender
  @sender
end

#timeObject

Returns the value of attribute time.



5
6
7
# File 'lib/mcollective/rpc/request.rb', line 5

def time
  @time
end

#uniqidObject

Returns the value of attribute uniqid.



5
6
7
# File 'lib/mcollective/rpc/request.rb', line 5

def uniqid
  @uniqid
end

Instance Method Details

#[](key) ⇒ Object

If data is a hash, gives easy access to its members, else returns nil



34
35
36
37
# File 'lib/mcollective/rpc/request.rb', line 34

def [](key)
  return nil unless @data.is_a?(Hash)
  return @data[key]
end

#fetch(key, default) ⇒ Object



39
40
41
42
# File 'lib/mcollective/rpc/request.rb', line 39

def fetch(key, default)
  return nil unless @data.is_a?(Hash)
  return @data.fetch(key, default)
end

#include?(key) ⇒ Boolean

If data is a hash, quick helper to get access to it’s include? method else returns false

Returns:

  • (Boolean)


20
21
22
23
# File 'lib/mcollective/rpc/request.rb', line 20

def include?(key)
  return false unless @data.is_a?(Hash)
  return @data.include?(key)
end

#should_respond?Boolean

If no :process_results is specified always respond else respond based on the supplied property

Returns:

  • (Boolean)


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

def should_respond?
  return @data[:process_results] if @data.include?(:process_results)

  return true
end

#to_hashObject



44
45
46
47
48
# File 'lib/mcollective/rpc/request.rb', line 44

def to_hash
  return {:agent => @agent,
          :action => @action,
          :data => @data}
end

#to_jsonObject



55
56
57
58
59
# File 'lib/mcollective/rpc/request.rb', line 55

def to_json
  to_hash.merge!({:sender   => @sender,
                  :callerid => @callerid,
                  :uniqid   => @uniqid}).to_json
end

#validate!Object

Validate the request against the DDL



51
52
53
# File 'lib/mcollective/rpc/request.rb', line 51

def validate!
  @ddl.validate_rpc_request(@action, @data)
end