Class: Icss::Protocol

Inherits:
Object
  • Object
show all
Includes:
Validations, Receiver, Receiver::ActsAsHash, Receiver::ActsAsLoadable
Defined in:
lib/icss/protocol.rb,
lib/icss/sample_message_call.rb

Overview

Describes an Avro Protocol Declaration

Avro protocols describe RPC interfaces. The Protocol class will receive an Avro JSON

A Protocol has the following attributes:

  • protocol, a string, the name of the protocol (required). name is provided as an alias for protocol.

  • namespace, a string that qualifies the name (optional).

  • doc, a string describing this protocol (optional).

  • types, an optional list of definitions of named types (records, enums, fixed and errors). An error definition is just like a record definition except it uses “error” instead of “record”. Note that forward references to named types are not permitted.

  • messages, an optional JSON object whose keys are message names and whose values are objects whose attributes are described below. No two messages may have the same name.

The name and namespace qualification rules defined for schema objects apply to protocols as well: see the documentation for Icss::Type.

For example, one may define a simple HelloWorld protocol with:

{
  "namespace":    "com.acme",
  "protocol":     "HelloWorld",
  "doc":          "Protocol Greetings",
  "types": [
    { "name":     "Greeting",
      "type":     "record",
      "fields":   [ {"name": "message", "type": "string"} ]},
    { "name":     "Curse",
      "type":     "error",
      "fields":   [ {"name": "message", "type": "string"} ]}
  ],
  "messages": {
    "hello": {
      "doc":      "Say hello.",
      "request":  [{"name": "greeting", "type": "Greeting" }],
      "response": "Greeting",
      "errors":   ["Curse"]
    }
  }
}

Instance Method Summary collapse

Methods included from Validations

#validate_name, #validate_namespace

Instance Method Details

#find_message(nm) ⇒ Object



94
95
96
97
98
# File 'lib/icss/protocol.rb', line 94

def find_message nm
  return if messages.blank?
  nm = nm.to_s.gsub("/", ".").split(".").last
  messages[nm]
end

#fullnameObject

String: namespace.name



86
87
88
# File 'lib/icss/protocol.rb', line 86

def fullname
  [namespace, name].compact.join(".")
end

#message_samples_hashObject

a hash for dumping to file: @example: from the whole thing, would dump only this:

namespace: util.time
protocol: chronic
messages:
  parse:
    samples:
      - url:            "?now=5%3A06%3A07%202010-08-08&time_str=Yesterday"
        response:       { "time": "2010-08-07 05:06:07 UTC", "epoch_seconds": 1281225967 }


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/icss/sample_message_call.rb', line 117

def message_samples_hash
  hsh = { "namespace" => namespace, "protocol" => protocol, "messages" => {} }
  messages.each do |msg_name, msg|
    hsh["messages"][msg_name] = { "samples" => [] }
    msg.samples.each do |sample_req|
      sample_hsh = {
        "name"     => sample_req.name,
        "doc"      => sample_req.doc,
      }
      if sample_req.response.present?
      then sample_hsh['response'] =  sample_req.response
      else sample_hsh['error']    =  sample_req.error
      end
      if sample_req.url.present?
      then sample_hsh['url']     = sample_req.url.to_s
      else sample_hsh['request'] = sample_req.request
      end
      hsh["messages"][msg_name]["samples"] << sample_hsh.compact_blank!
    end
  end
  return hsh
end

#pathObject



90
91
92
# File 'lib/icss/protocol.rb', line 90

def path
  fullname.gsub('.', '/')
end

#receive_protocol(nm) ⇒ Object



100
101
102
103
104
# File 'lib/icss/protocol.rb', line 100

def receive_protocol nm
  namespace_and_name = nm.to_s.gsub("/", ".").split(".")
  self.protocol  = namespace_and_name.pop
  self.namespace = namespace_and_name.join('.')
end

#receive_targets(hsh) ⇒ Object



106
107
108
109
110
111
# File 'lib/icss/protocol.rb', line 106

def receive_targets hsh
  self.targets = hsh.inject({}) do |target_obj_hsh, (target_name, target_info_list)|
    target_obj_hsh[target_name] = TargetListFactory.receive(target_info_list, target_name) # returns an arry of targets
    target_obj_hsh
  end
end

#targets_to_hashObject



128
129
130
131
132
133
# File 'lib/icss/protocol.rb', line 128

def targets_to_hash
  return unless targets
  targets.inject({}) do |hsh,(k,targs)|
    hsh[k] = targs.map{|t| t.respond_to?(:to_hash) ? t.to_hash : t } ; hsh
  end
end

#to_hashObject



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/icss/protocol.rb', line 113

def to_hash()
  {
    :namespace   => @namespace, # use accessor so unset namespace isn't given
    :protocol    => protocol,
    :doc         => doc,
    :under_consideration => under_consideration,
    :update_frequency    => update_frequency,
    :types       => (types       && types.map(&:to_hash)),
    :messages    => messages.inject({}){|h,(k,v)| h[k] = v.to_hash; h },
    :data_assets => data_assets.map(&:to_hash),
    :code_assets => code_assets.map(&:to_hash),
    :targets     => targets_to_hash,
  }.reject{|k,v| v.nil? }.tree_merge! self.message_samples_hash
end

#to_json(*args) ⇒ Object

This will cause funny errors when it is an element of something that’s to_json’ed



136
# File 'lib/icss/protocol.rb', line 136

def to_json(*args) to_hash.to_json(*args) ; end