https://github.com/mschae/intermediary
This gem is responsible for emitting and receiving updates to changes that occur within an app.
https://github.com/mschae/intermediary
Last synced: about 1 year ago
JSON representation
This gem is responsible for emitting and receiving updates to changes that occur within an app.
- Host: GitHub
- URL: https://github.com/mschae/intermediary
- Owner: mschae
- License: mit
- Created: 2014-08-04T15:49:42.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2014-09-30T13:58:07.000Z (over 11 years ago)
- Last Synced: 2025-01-23T05:44:45.126Z (over 1 year ago)
- Language: Ruby
- Homepage:
- Size: 173 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Intermediary
This gems aims to unify asynchronous communication between different apps in a
SOA.
It is built on top of the awesome bunny gem, which provides an interface to
RabbitMQ.
The emitter (the app that publishes updates) in this gem does not know who
consumes those updates. To this end a topic exchange is being used. To make
subscribing to updates as straightforward as possible the exchange key mimics
restful routes. Updates to resources are published in an exchange key that
reflects the resource's href (down to the host).
## Installation
Add this line to your application's Gemfile:
gem 'intermediary'
And then execute:
$ bundle
Or install it yourself as:
$ gem install intermediary
## Usage
As explained above, gem provides two elements to exchange updates between apps:
- The emitter
- The listener
### The Emitter
To use the emitter one simply inherits from `Intermediary::Emitters::Base` as
follows:
```ruby
class MyEmitter < Intermediary::Emitters::Base
def href
# Some fance href generation...
end
end
```
Per default the initializer takes one argument (the object). As everything else
it can be customized by overwriting it.
The href method always has to be supplied to indicate how to construct the
routing key.
Per default the payload emitted is the the object's `.attributes` and the
event.
### The Listener
The receiver is merely a user-defined class that implements the following
method: `self.act`. Which takes three arguments: `payload`, `routing_key` and
`properties`. The `payload` will be a hash.
Furthermore the class has to define `@queue_name` and `@routing_key`.
Example:
```ruby
class MyListener
@queue_name = "my-awesome-queue"
@routing_key = "app_example_com.model.#"
def self.act(payload, routing_key, properties)
puts payload.inspect
end
end
```
**Please note that multiple receivers with the same queue name will share
items!**
To register a listener, please call the following (e.g. in an initializer):
```ruby
Intermediary::Listeners.register MyListener
```
Additionally one can define a `self.on_error` method with takes one argument:
The error itself. It will get called when processing a job failed. Feel free
to pass it to your favorite error tracker.
**Pro tip**
If you want to share the same `on_error` handler amongst multiple listeners,
how about using a module, eh?
## Contributing
1. Fork it ( http://github.com//intermediary/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request