Table Of Contents

Previous topic

Reference

This Page

Sample Document

Ruby API

Ruby API is similar to Python API. I this section show samples in Ruby. If you need detail information see Python API reference.

Decorators

berryMQ provides module methods Follower.following and Follower.auto_twitter. They works like Class.public and Class.private and so on. It emurates decorator mechanism in Python.

Use this like following:

require 'berrymq'

class Logger
  include BerryMQ::Follower

  following("*:log")
  def receive_log(message)
    ...
  end
end

Class.public keeps effect until other module method will be called, But this functions effect only the next method definition.

Warning

Ruby implementation uses module including mechanism to class. So you can’t set module function receiver like Python.

class BerryMQ::Follower

If you want to use the method as receiver, set this class as metaclass.

This class provide special decorators. following and auto_twitter. Use like this:

# Ruby
class Logger
  include BerryMQ::Follower

  following("*:log")
  def receive_log(message)
     ...
  end
end
classmethod Follower.following(identifier[, guard_condition])

This is a decorator function. This decorator can use for only instanse methods of the classes which use Follower metaclass.

Parameters:
  • identifier (str) – receive message filter identifier.
  • guard_condition (callable object with one paramater(message object)) – if this guard function return False, message receive will be skipped
classmethod Follower.auto_twitter(identifier[, entry[, exit]])

This is a decorator function. The decorated function is called, automatically sends message.

@auto_twitter("function")
def sample_function():
    ... do something

If this function is called, function:entry message will be sent before running function, and then function:exit message will be sent after running function.

Use entry, exit parameter for controlling message.

entry exit action
False False send both.
False True exit message only
True False entry message only
True True send both.
Parameters:
  • identifier (str) – Send message :ref:identifier
  • entry (True or False) – Send message before calling function?(default: False)
  • exit (True or False) – Send message after calling function?(default: False)

Functions

You can use BerryMQ.twitter function like this:

require 'berrymq'

def on_button_pressed
   BerryMQ.twitter("on_button_pressed:log")
   .
   .
end
BerryMQ.twitter(identifier[, ...])

Send message. It is the most important function in berryMQ. This funciton can recieve any args and kwargs. These values are delivered via Message object.

Note

In future release, if last paramter is Hash, it will pass as kwargs.

Classes

class BerryMQ::Message

This object is created in berryMQ automatically. User doesn’t create this object directly.

All of following attributes are readonly(defined as property).

attribute [R] name

This is a front part of Identifier.

attribute [R] action

This is a back part of Identifier.

attribute [R] id

This is a string form of Identifier

attribute [R] args

If you pass any parameters at twitter(), this attribute stores them.

.
twitter("do_something:log", time.ctime())
.

class Logger
   following("*:log)
   def receive_log(message)
      puts message.args[0]  # print time.ctime() value
   end
end