|
| 1 | +## Using the Routemaster Client feature |
| 2 | + |
| 3 | +[`routemaster-client`](https://github.com/deliveroo/routemaster-client) comes as a dependency of `roo_on_rails` with a basic implementation of lifecycle event publishers. |
| 4 | + |
| 5 | +This code example assumes that you are using the latest version of the [`roo_on_rails`](roo_on_rails) gem and that you have set the correct environment variables for Routemaster Client to work on your app, as explained in the main [`README.md`](roo_on_rails#routemaster-client) file. |
| 6 | + |
| 7 | +It also assumes that your app has an API for the resources you want to publish lifecycle events for, with matching routes and an `API_HOST` environment variable set. |
| 8 | + |
| 9 | +### Setup lifecycle events for your models |
| 10 | + |
| 11 | +We will most likely want to publish lifecycle events for several models, so to write slightly less code let's create a model concern first: |
| 12 | + |
| 13 | +```ruby |
| 14 | +# app/models/concerns/routemaster_lifecycle_events.rb |
| 15 | +require 'roo_on_rails/routemaster/lifecycle_events' |
| 16 | + |
| 17 | +module RoutemasterLifecycleEvents |
| 18 | + extend ActiveSupport::Concern |
| 19 | + include RooOnRails::Routemaster::LifecycleEvents |
| 20 | + |
| 21 | + included do |
| 22 | + publish_lifecycle_events |
| 23 | + end |
| 24 | +end |
| 25 | +``` |
| 26 | + |
| 27 | +Then let's include this concern to the relevant model(s): |
| 28 | + |
| 29 | +```ruby |
| 30 | +# app/models/order.rb |
| 31 | +class Order < ApplicationRecord |
| 32 | + include RoutemasterLifecycleEvents |
| 33 | + |
| 34 | + # ... |
| 35 | +end |
| 36 | +``` |
| 37 | + |
| 38 | +And another one for the example: |
| 39 | + |
| 40 | +```ruby |
| 41 | +# app/models/rider.rb |
| 42 | +class Rider < ApplicationRecord |
| 43 | + include RoutemasterLifecycleEvents |
| 44 | + |
| 45 | + # ... |
| 46 | +end |
| 47 | +``` |
| 48 | + |
| 49 | +### Create publishers for lifecycle events |
| 50 | + |
| 51 | +We have now configured our models to publish lifecycle events to Routemaster, but it won't send anything until you have enabled publishing and created matching publishers. Let's start with creating a `BasePublisher` that we can then inherit from: |
| 52 | + |
| 53 | +```ruby |
| 54 | +# app/publishers/base_publisher.rb |
| 55 | +require 'roo_on_rails/routemaster/publisher' |
| 56 | + |
| 57 | +class BasePublisher < RooOnRails::Routemaster::Publisher |
| 58 | + include Rails.application.routes.url_helpers |
| 59 | + |
| 60 | + # Add your method overrides here if needed |
| 61 | +end |
| 62 | +``` |
| 63 | + |
| 64 | +Then create a publisher for each model with lifecycle events enabled: |
| 65 | + |
| 66 | +```ruby |
| 67 | +# app/publishers/order_publisher.rb |
| 68 | +class OrderPublisher < BasePublisher |
| 69 | + def url |
| 70 | + api_order_url(model, host: ENV.fetch('API_HOST'), protocol: 'https') |
| 71 | + end |
| 72 | +end |
| 73 | +``` |
| 74 | + |
| 75 | +and |
| 76 | + |
| 77 | +```ruby |
| 78 | +# app/publishers/rider_publisher.rb |
| 79 | +class RiderPublisher < BasePublisher |
| 80 | + def url |
| 81 | + api_rider_url(model, host: ENV.fetch('API_HOST'), protocol: 'https') |
| 82 | + end |
| 83 | +end |
| 84 | +``` |
| 85 | + |
| 86 | +### Register the publishers with Routemaster |
| 87 | + |
| 88 | +The final step is to tell Routemaster that these publishers exist, so that it can listen to their events. We're going to do this in an initialiser: |
| 89 | + |
| 90 | +```ruby |
| 91 | +# config/initilizers/routemaster.rb |
| 92 | +require 'roo_on_rails/routemaster/publishers' |
| 93 | + |
| 94 | +PUBLISHERS = [ |
| 95 | + OrderPublisher, |
| 96 | + RiderPublisher |
| 97 | +].freeze |
| 98 | + |
| 99 | +PUBLISHERS.each do |publisher| |
| 100 | + model_class = publisher.to_s.gsub("Publisher", "").constantize |
| 101 | + RooOnRails::Routemaster::Publishers.register(publisher, model_class: model_class) |
| 102 | +end |
| 103 | +``` |
| 104 | + |
| 105 | +We should now be all set for our app to publish lifecycle events for `orders` and `riders` onto the event bus, so that other apps can listen to them. |
0 commit comments