Class: OmfCommon::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/omf_common/message.rb,
lib/omf_common/message/xml/message.rb,
lib/omf_common/message/json/json_message.rb

Direct Known Subclasses

Json::Message, XML::Message

Defined Under Namespace

Classes: Json, XML

Constant Summary collapse

OMF_NAMESPACE =
"http://schema.mytestbed.net/omf/#{OmfCommon::PROTOCOL_VERSION}/protocol"
OMF_CORE_READ =
[:operation, :ts, :src, :mid, :replyto, :cid, :itype, :rtype, :guard, :res_id]
OMF_CORE_WRITE =
[:replyto, :itype, :guard]
@@providers =
{
  xml: {
    require: 'omf_common/message/xml/message',
    constructor: 'OmfCommon::Message::XML::Message'
  },
  json: {
    require: 'omf_common/message/json/json_message',
    constructor: 'OmfCommon::Message::Json::Message'
  }
}
@@message_class =
nil
@@authenticate_messages =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.authenticate?Boolean

Return true if all messages will be authenticated, return false otherwise

Returns:

  • (Boolean)


47
48
49
# File 'lib/omf_common/message.rb', line 47

def self.authenticate?
  @@authenticate_messages
end

.create(type, properties, body = {}) ⇒ Object



36
37
38
# File 'lib/omf_common/message.rb', line 36

def self.create(type, properties, body = {})
  @@message_class.create(type, properties || {}, body)
end

.create_inform_message(itype = nil, properties = {}, body = {}) ⇒ Object



40
41
42
43
# File 'lib/omf_common/message.rb', line 40

def self.create_inform_message(itype = nil, properties = {}, body = {})
  body[:itype] = itype if itype
  create(:inform, properties, body)
end

.init(opts = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/omf_common/message.rb', line 60

def self.init(opts = {})
  if @@message_class
    raise "Message provider already iniitalised"
  end
  unless provider = opts[:provider]
    provider = @@providers[opts[:type]]
  end
  unless provider
    raise "Missing Message provider declaration. Either define 'type' or 'provider'"
  end

  require provider[:require] if provider[:require]

  if class_name = provider[:constructor]
    @@message_class = class_name.split('::').inject(Object) {|c,n| c.const_get(n) }
  else
    raise "Missing provider class info - :constructor"
  end
  @@authenticate_messages = opts[:authenticate] if opts[:authenticate]
end

.parse(str, content_type = nil, &block) ⇒ Object

Parse message from ‘str’ and pass it to ‘block’. If authnetication is on, the message will only be handed to ‘block’ if the source of the message can be authenticated.

Raises:

  • (ArgumentError)


55
56
57
58
# File 'lib/omf_common/message.rb', line 55

def self.parse(str, content_type = nil, &block)
  raise ArgumentError, 'Need message handling block' unless block
  @@message_class.parse(str, content_type, &block)
end

Instance Method Details

#[](name, ns = nil) ⇒ Object

To access properties

Parameters:

  • name (String)

    of the property

  • ns (Hash) (defaults to: nil)

    namespace of property



99
100
101
# File 'lib/omf_common/message.rb', line 99

def [](name, ns = nil)
  _get_property(name.to_sym, ns)
end

#[]=(name, ns = nil, value) ⇒ Object

To set properties

Parameters:

  • name (String)

    of the property

  • ns (Hash) (defaults to: nil)

    namespace of property



107
108
109
110
111
112
113
114
115
# File 'lib/omf_common/message.rb', line 107

def []=(name, ns = nil, value)
  # TODO why itype cannot be set?
  #raise if name.to_sym == :itype
  if ns
    @props_ns ||= {}
    @props_ns.merge(ns)
  end
  _set_property(name.to_sym, value, ns)
end

#create_inform_reply_message(itype = nil, properties = {}, body = {}) ⇒ Object



160
161
162
163
# File 'lib/omf_common/message.rb', line 160

def create_inform_reply_message(itype = nil, properties = {}, body = {})
  body[:cid] = self.mid
  self.class.create_inform_message(itype, properties, body)
end

#default_props_nsObject

Construct default namespace of the props from resource type



199
200
201
202
# File 'lib/omf_common/message.rb', line 199

def default_props_ns
  resource_type = _get_core(:rtype)
  resource_type ? { resource_type.to_s => "#{OMF_NAMESPACE}/#{resource_type}" } : {}
end

#each_bound_request_property(&block) ⇒ Object

Loop over all the bound (sent with a value) properties of a request message.

Raises:

  • (NotImplementedError)


131
132
133
# File 'lib/omf_common/message.rb', line 131

def each_bound_request_property(&block)
  raise NotImplementedError
end

#each_property(&block) ⇒ Object

Raises:

  • (NotImplementedError)


117
118
119
# File 'lib/omf_common/message.rb', line 117

def each_property(&block)
  raise NotImplementedError
end

#each_unbound_request_property(&block) ⇒ Object

Loop over all the unbound (sent without a value) properties of a request message.

Raises:

  • (NotImplementedError)


124
125
126
# File 'lib/omf_common/message.rb', line 124

def each_unbound_request_property(&block)
  raise NotImplementedError
end

#error?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/omf_common/message.rb', line 156

def error?
  (itype || '') =~ /(error|ERROR|FAILED)/
end

#guard?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


143
144
145
# File 'lib/omf_common/message.rb', line 143

def guard?
  raise NotImplementedError
end

#has_properties?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/omf_common/message.rb', line 139

def has_properties?
  not properties.empty?
end

#itype(format = nil) ⇒ Object

Fetch inform type

When no format provided, return the value as it is.

Parameters:

  • format (Symbol) (defaults to: nil)

    to render itype, valid formats: :ruby, :frcp



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/omf_common/message.rb', line 171

def itype(format = nil)
  if format && !_get_core(:itype).nil?
    case format.to_sym
    when :ruby
      _get_core(:itype).to_s.downcase.gsub(/\./, '_')
    when :frcp
      _get_core(:itype).to_s.upcase.gsub(/_/, '.')
    else
      raise ArgumentError, "Unknown format '#{format}'. Please use ':ruby, :frcp' instead."
    end
  else
    _get_core(:itype)
  end
end

#marshall(include_cert = false) ⇒ Object

Raises:

  • (NotImplementedError)


190
191
192
# File 'lib/omf_common/message.rb', line 190

def marshall(include_cert = false)
  raise NotImplementedError
end

#propertiesObject

Raises:

  • (NotImplementedError)


135
136
137
# File 'lib/omf_common/message.rb', line 135

def properties
  raise NotImplementedError
end

#props_nsObject

Get all property namespace defs



205
206
207
208
# File 'lib/omf_common/message.rb', line 205

def props_ns
  @props_ns ||= {}
  default_props_ns.merge(@props_ns)
end

#resourceObject



147
148
149
150
# File 'lib/omf_common/message.rb', line 147

def resource
  name = _get_property(:res_id)
  OmfCommon.comm.create_topic(name)
end

#success?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/omf_common/message.rb', line 152

def success?
  ! error?
end

#to_sObject

Raises:

  • (NotImplementedError)


186
187
188
# File 'lib/omf_common/message.rb', line 186

def to_s
  raise NotImplementedError
end

#valid?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


194
195
196
# File 'lib/omf_common/message.rb', line 194

def valid?
  raise NotImplementedError
end