class OmfCommon::Message

Constants

OMF_CORE_READ
OMF_CORE_WRITE
OMF_NAMESPACE

Attributes

issuer[R]

Public Class Methods

authenticate?() click to toggle source

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

# File lib/omf_common/message.rb, line 48
def self.authenticate?
  @@authenticate_messages
end
create(type, properties, body = {}) click to toggle source
# File lib/omf_common/message.rb, line 37
def self.create(type, properties, body = {})
  @@message_class.create(type, properties || {}, body)
end
create_inform_message(itype = nil, properties = {}, body = {}) click to toggle source
# File lib/omf_common/message.rb, line 41
def self.create_inform_message(itype = nil, properties = {}, body = {})
  body[:itype] = itype if itype
  create(:inform, properties, body)
end
init(opts = {}) click to toggle source
# File lib/omf_common/message.rb, line 68
def self.init(opts = {})
  unless @@message_class
    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
    aopts = opts[:authenticate] || {}
    @@authenticate_messages = opts[:authenticate] && !(aopts[:authenticate] == false)
    if pdp_opts = (opts[:authenticate] || {})[:pdp]
      require pdp_opts.delete(:require) if pdp_opts[:require]
      unless pdp_constructor = pdp_opts.delete(:constructor)
        raise "Missing PDP provider declaration."
      end

      pdp_class = pdp_constructor.split('::').inject(Object) {|c,n| c.const_get(n) }
      @@authorisation_hook = pdp_class.new(pdp_opts)
    end
  end
end
parse(str, content_type = nil, &block) click to toggle source

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

# File lib/omf_common/message.rb, line 56
def self.parse(str, content_type = nil, &block)
  raise ArgumentError, 'Need message handling block' unless block
  @@message_class.parse(str, content_type) do |msg|
    if @@authorisation_hook
      # Hook will return message if it's authorized. Handing in
      # dispatch block in case hook needs more time for authorization.
      msg = @@authorisation_hook.authorize(msg, &block)
    end
    block.call(msg) if msg
  end
end

Public Instance Methods

[](name, ns = nil) click to toggle source

To access properties

@param [String] name of the property @param [Hash] ns namespace of property

# File lib/omf_common/message.rb, line 118
def [](name, ns = nil)
  _get_property(name.to_sym, ns)
end
[]=(name, ns = nil, value) click to toggle source

To set properties

@param [String] name of the property @param [Hash] ns namespace of property

# File lib/omf_common/message.rb, line 126
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 = {}) click to toggle source
# File lib/omf_common/message.rb, line 183
def create_inform_reply_message(itype = nil, properties = {}, body = {})
  body[:cid] = self.mid
  self.class.create_inform_message(itype, properties, body)
end
default_props_ns() click to toggle source

Construct default namespace of the props from resource type

# File lib/omf_common/message.rb, line 222
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) click to toggle source

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

# File lib/omf_common/message.rb, line 150
def each_bound_request_property(&block)
  raise NotImplementedError
end
each_property(&block) click to toggle source
# File lib/omf_common/message.rb, line 136
def each_property(&block)
  raise NotImplementedError
end
each_unbound_request_property(&block) click to toggle source

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

# File lib/omf_common/message.rb, line 143
def each_unbound_request_property(&block)
  raise NotImplementedError
end
error?() click to toggle source
# File lib/omf_common/message.rb, line 179
def error?
  (itype || '') =~ /(error|ERROR|FAILED)/
end
guard?() click to toggle source
# File lib/omf_common/message.rb, line 162
def guard?
  raise NotImplementedError
end
has_properties?() click to toggle source
# File lib/omf_common/message.rb, line 158
def has_properties?
  not properties.empty?
end
itype(format = nil) click to toggle source

Fetch inform type

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

@param [Symbol] format to render itype, valid formats: :ruby, :frcp

# File lib/omf_common/message.rb, line 194
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) click to toggle source
# File lib/omf_common/message.rb, line 213
def marshall(include_cert = false)
  raise NotImplementedError
end
properties() click to toggle source
# File lib/omf_common/message.rb, line 154
def properties
  raise NotImplementedError
end
props_ns() click to toggle source

Get all property namespace defs

# File lib/omf_common/message.rb, line 228
def props_ns
  @props_ns ||= {}
  default_props_ns.merge(@props_ns).stringify_keys
end
resource() click to toggle source
# File lib/omf_common/message.rb, line 166
def resource
  name = _get_property(:res_id)
  OmfCommon.comm.create_topic(name)
end
resource_address() click to toggle source
# File lib/omf_common/message.rb, line 171
def resource_address
  _get_property(:res_id)
end
success?() click to toggle source
# File lib/omf_common/message.rb, line 175
def success?
  ! error?
end
to_s() click to toggle source
# File lib/omf_common/message.rb, line 209
def to_s
  raise NotImplementedError
end
valid?() click to toggle source
# File lib/omf_common/message.rb, line 217
def valid?
  raise NotImplementedError
end

Private Instance Methods

_get_core(key) click to toggle source
# File lib/omf_common/message.rb, line 247
def _get_core(key)
  raise NotImplementedError
end
_get_property(name, ns = nil) click to toggle source
# File lib/omf_common/message.rb, line 235
def  _get_property(name, ns = nil)
  raise NotImplementedError
end
_set_core(key, value) click to toggle source
# File lib/omf_common/message.rb, line 243
def _set_core(key, value)
  raise NotImplementedError
end
_set_property(name, value, ns = nil) click to toggle source
# File lib/omf_common/message.rb, line 239
def  _set_property(name, value, ns = nil)
  raise NotImplementedError
end