{"id":18319759,"url":"https://github.com/redding/much-rails-pub-sub","last_synced_at":"2025-04-09T14:22:25.856Z","repository":{"id":49490646,"uuid":"377168891","full_name":"redding/much-rails-pub-sub","owner":"redding","description":"A Pub/Sub API/framework for MuchRails using ActiveJob","archived":false,"fork":false,"pushed_at":"2021-06-16T21:19:22.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-15T16:18:23.059Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/redding.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-06-15T13:16:34.000Z","updated_at":"2021-06-16T21:19:23.000Z","dependencies_parsed_at":"2022-07-31T14:49:14.470Z","dependency_job_id":null,"html_url":"https://github.com/redding/much-rails-pub-sub","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fmuch-rails-pub-sub","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fmuch-rails-pub-sub/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fmuch-rails-pub-sub/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fmuch-rails-pub-sub/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/redding","download_url":"https://codeload.github.com/redding/much-rails-pub-sub/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248054200,"owners_count":21039952,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-05T18:14:10.486Z","updated_at":"2025-04-09T14:22:25.828Z","avatar_url":"https://github.com/redding.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MuchRailsPubSub\n\nA Pub/Sub API/framework for MuchRails using ActiveJob.\n\n## Setup\n\n### Add an ActiveJob to publish events\n\nIn e.g. `app/jobs/pub_sub_publish_job.rb`:\n\n```ruby\nclass PubSubPublishJob \u003c ApplicationJob\n  include MuchRailsPubSub::PublishJobBehaviors\n\n  # add any additional desired ActiveJob configurations\n  queue_as :critical\nend\n```\n\n### Add a config file for event subscriptions\n\nCreate an empty config file named e.g. `config/pub_sub.rb`. This file will hold the configured event subscriptions.\n\n### Add an initializer\n\nThis will configure the PubSubPublishJob and load subscriptions in `config/pub_sub.rb`. In e.g. `config/initializers/pub_sub.rb`:\n\n```ruby\nMuchRailsPubSub.configure do |config|\n  config.publish_job = PubSubPublishJob\n  config.logger = Rails.logger\nend\n\n# `MuchRailsPubSub` needs to load subscriptions after the Rails app has\n# been initialized. This allows initialization callbacks to configure\n# `ActiveJob` before pub/sub job classes are required and evaluated by\n# loading subscriptions.\nRails.application.config.after_initialize do\n  MuchRailsPubSub.load_subscriptions(Rails.root.join(\"config/pub_sub.rb\"))\nend\n```\n\n## Usage\n\n### Add an event handler job\n\nIn e.g. `app/jobs/events/thing/create_v1_job.rb`:\n\n```ruby\nclass Events::Thing::CreatedV1Job \u003c ApplicationJob\n  def perform(params)\n    puts \"do something when a Thing is created ...\"\n    puts \"params: #{params.inspect}\"\n  end\nend\n```\n\n### Subscribe the event handler job to an event\n\nIn e.g. `config/pub_sub.rb`:\n\n```ruby\nMuchRailsPubSub.subscribe \"thing.created.v1\",\n                          job: Events::Thing::CreatedV1Job\n```\n\n### Publish the event in your code\n\nE.g.:\n\n```ruby\nMuchRailsPubSub.publish(\"thing.created.v1\", key: \"value\")\n```\n\nIn the Rails logger:\n\n```\nEnqueued PubSubPublishJob (Job ID: fc834ce6-2f2d-4953-bb83-e9a272bf2a08) to Sidekiq(critical) with arguments: {\"event_id\"=\u003e\"5aaa5129-69b5-46fe-bff3-2e60c9749d62\", \"event_name\"=\u003e\"thing.created.v1\", \"event_params\"=\u003e{:key=\u003e\"value\"}}\n2021-06-16T13:14:15.116Z pid=31539 tid=mxn INFO: [MuchRailsPubSub] Published \"thing.created.v1\":\n  ID: \"5aaa5129-69b5-46fe-bff3-2e60c9749d62\"\n  PARAMS: {:key=\u003e\"value\"}\n\n2021-06-16T13:14:15.117Z pid=31902 tid=e62 class=PubSubPublishJob jid=1e73eeee286bebe1f57a0e97 INFO: start\n2021-06-16T13:14:15.126Z pid=31902 tid=e0y class=Events::Thing::CreatedV1Job jid=f22958c1fa80a4aee91b2731 INFO: start\n2021-06-16T13:14:15.127Z pid=31902 tid=e62 class=PubSubPublishJob jid=1e73eeee286bebe1f57a0e97 INFO: [MuchRailsPubSub] Dispatched 1 subscription job(s) for \"thing.created.v1\" (5aaa5129-69b5-46fe-bff3-2e60c9749d62):\n  - Events::Thing::CreatedV1Job\n2021-06-16T13:14:15.127Z pid=31902 tid=e62 class=PubSubPublishJob jid=1e73eeee286bebe1f57a0e97 elapsed=0.01 INFO: done\ndo something when a Thing is created ...\nparams: {:key=\u003e\"value\"}\n2021-06-16T13:14:15.129Z pid=31902 tid=e0y class=Events::Thing::CreatedV1Job jid=f22958c1fa80a4aee91b2731 elapsed=0.002 INFO: done\n```\n\n## Testing\n\n### Event Handler Jobs\n\nThese are just ActiveJobs; test them like you would test any other ActiveJob.\n\n### Event publishes\n\nMuchRailsPubSub comes with a `TestPublisher` and `TestingPublishedEvents` classes that produce the same side-effects of publishing an event without _actually_ publishing the event. You can then test for these side-effects, in e.g. unit tests, to verify event publishes are happening as expected.\n\nThis example assumes you are using [Assert](https://github.com/redding/assert) as your test framework. However, this can be adapted to whatever framework you use.\n\nIn e.g. `test/helper.rb`\n\n```ruby\nrequire \"test/support/fake_logger\"\nrequire \"much-rails-pub-sub\"\n\nMuchRailsPubSub.setup_test_publishing\nMuchRailsPubSub.config.logger = FakeLogger.new\n\nAssert::Context.teardown do\n  MuchRailsPubSub.published_events.clear\nend\n```\n\nIn a test:\n\n```ruby\nMuchRailsPubSub.publish(\"thing.created.v1\", key: \"value\")\n\nlatest_event = MuchRailsPubSub.published_events.last\nassert_that(latest_event.name).equals(\"thing.created.v1\")\nassert_that(latest_event.params).equals(key: \"value\")\n```\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem \"much-rails-pub-sub\"\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install much-rails-pub-sub\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am \"Added some feature\"`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredding%2Fmuch-rails-pub-sub","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fredding%2Fmuch-rails-pub-sub","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredding%2Fmuch-rails-pub-sub/lists"}