{"id":18585991,"url":"https://github.com/cloud66-oss/noticent","last_synced_at":"2025-04-05T15:08:40.172Z","repository":{"id":36408922,"uuid":"186803868","full_name":"cloud66-oss/noticent","owner":"cloud66-oss","description":"Ruby gem for user notification management.","archived":false,"fork":false,"pushed_at":"2023-12-06T13:00:28.000Z","size":255,"stargazers_count":143,"open_issues_count":7,"forks_count":12,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-03-29T14:09:46.477Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cloud66-oss.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-15T10:27:25.000Z","updated_at":"2025-02-08T12:42:42.000Z","dependencies_parsed_at":"2024-11-07T00:38:48.868Z","dependency_job_id":"55ac97cc-2325-4b49-bc82-d0e1b7c38544","html_url":"https://github.com/cloud66-oss/noticent","commit_stats":{"total_commits":94,"total_committers":9,"mean_commits":"10.444444444444445","dds":"0.14893617021276595","last_synced_commit":"19390ef42b319bd1d689ce021ada7a1ad5b03056"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloud66-oss%2Fnoticent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloud66-oss%2Fnoticent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloud66-oss%2Fnoticent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloud66-oss%2Fnoticent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cloud66-oss","download_url":"https://codeload.github.com/cloud66-oss/noticent/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247353746,"owners_count":20925329,"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-07T00:36:17.838Z","updated_at":"2025-04-05T15:08:40.047Z","avatar_url":"https://github.com/cloud66-oss.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cimg src=\"http://cdn2-cloud66-com.s3.amazonaws.com/images/oss-sponsorship.png\" width=150/\u003e\n\n# Noticent\n\nNoticent is a Ruby gem for user notification management. It is written to deliver a developer friendly way to managing application notifications in a typical web application. Many applications have user notification: sending emails when a task is done or support for webhooks or Slack upon certain events. Noticent makes it easy to write maintainable code for notification subscription and delivery in a typical web application.\n\n[![Codeship Status for cloud66-oss/noticent](https://app.codeship.com/projects/f5bd9b70-646f-0137-d7e7-7232cc99892f/status?branch=master)](https://app.codeship.com/projects/344893)\n\nThe primary design goal for Noticent is developer friendliness. Using Noticent, you should be able to:\n\n- Create new notification types\n- Tell the current state of notifications, subscriptions and distribution channels.\n- Support multiple notification channels like email, chat applications, mobile push, webhooks and more.\n- Test your notifications\n\n## Installation\n\n### Notice on Rails version\nNoticent 0.0.6 has been upgraded to work with Rails 7.0. If you would like to use it with an older version of Rails \u003c= 5, please use Noticent 0.0.4\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'noticent'\n```\n\nAnd then execute:\n\n```bash\nbundle\n```\n\nOr install it yourself as:\n\n```bash\ngem install noticent\n```\n\n### Run Generators\n\n```bash\nrails g noticent:install\n```\n\nNow run the migrations\n\n```bash\nrake db:migrate\n```\n\n## Usage\n\nNoticent is written to be used in a Rails application but you should be able to use it in other Ruby / Rack based applications if you need to.\n\n### Basics\n\nNoticent uses the following concepts:\n\n#### Alert\n\nAlert is a type of notification. For example, a new user signing up could be defined as an Alert.\n\n#### Scope\n\nScope is like a namespace for Alerts. In many applications, you will only have 1 Scope. However, sometimes you might have different types of Alerts for different parts of your application. For example, \"new blog post written\" and \"blog post updated\" Alerts could be associated with the a \"blog\" Scope, while a \"new comment added\" Alert is associated with another Scope. Using Scope is useful when you have many different groups of Alerts in your application.\n\n#### Channel\n\nChannel is a distribution channel for your Alerts. Examples of Channels are Email, Slack, Webhook, Mobile or browser notification.\n\n#### Recipient\n\nRecipient is a person or system that receives Alerts. This could be a user for Channels like Email or a system for a Webhook Channel.\n\n#### Payload\n\nPayload is a data structure (class) that carries everything you'd need to send an Alert to a Recipient over a Channel.\n\n#### Product \n\nA product acts as a filter on alerts. For example, you might want to list different set of alerts for different parts of your application. To achieve that you can define each part as a product and use `applies.to` and `applies.not_to` to say how an alert applies to each part of the application.\nProducts don't have an effect on how alerts are run, but they can be used to list which alerts apply to each part of an application using `Noticent.configuration.product_by_alert` method.\n\nIf an alert doesn't have an `applies`, it will be applicable to none of the products defined. \n\n### Integration\n\nNoticent tries to make very few assumptions about your application, like what you call your Recipients or what your Scopes are. However it also enforces some opinions to make the whole system easier to use and maintain.\n\n### Configuration\n\nThe most important part of using Noticent is the configuration part where you define scopes, channels and your alerts. Once configured, you can hook it up to the rest of your code. This example assumes integration in a Rails application.\n\nIf you have run the generators, you should now have a file called `config/initializers/noticent.rb`. You can edit it as you like: \n\n```ruby\nNoticent.configure do\n    channel :email\n\n    scope :account do\n        alert(:new_signup) { notify :owner}\n        alert(:new_team_member) { notify :users }\n    end\nend\n```\n\nNow you'd need to tell Noticent how to send emails by creating an email channel. This can be done in `app/models/noticent/channels/email.rb`:\n\n```ruby\nclass Email \u003c ::Noticent::Channel\n    def new_signup\n        # send email here\n    end\n\n    def new_team_member\n        # send email here\n    end\nend\n```\n\nNow that we have our channel, we can define a Payload. We can do this in `app/models/noticent/account_payload.rb`:\n\n```ruby\nclass AccountPayload\n    attr_reader :account\n    attr_reader :current_user\n\n    def initializer(account_id, current_user)\n        @account = Account.find account_id\n    end\n\n    def users\n        @account.users\n    end\n\n    def owner\n        @account.owner\n    end\nend\n```\n\nYou can now create your email templates in `app/models/noticent/views/email` with 2 files called `new_signup.html.erb` and `new_team_member.html.erb` the same way you would write email templates for Rails mailers:\n\n```html\nHello \u003c%= @owner.name %\u003e!\nA new user just signed up.\n```\n\nand\n\n```html\nYou now have a new team member. Make sure to say hi!\n```\n\nUntil now, this is very much like Rail's own mailers and follows the same principles: Payload is like a model, Channel is the equivalent of a controller and the html view file is the view.\n\nThis first difference here is that you can use \"front matter\" in your views. This is useful when you need more than just text or HTML in your notifications. For an email channel, the front matter can hold a template for the email subject for example:\n\n```html\nsubject: New member for \u003c%= @team.name %\u003e\n---\nHello!\n\nYou now have a new team member who signed up as an admin for \u003c%= @team.name %\u003e\n```\n\nIn the channel, you can use this:\n\n```ruby\nclass EmailChannel \u003c ::Noticent::Channel\n    def new_member\n        data, content = render\n        send_email(subject: data[:subject], content: content) # this is an example code\n    end\nend\n```\n\nThe `render` method looks for the right file under the `views` directory and loads and renders the ERB file while returning any front matter if available.\n\nUse of front matter becomes more important in channels that have a more complex API like Slack (message color can be stored in the front matter) for example.\n\nNow let's go back to our configuration file and see what else we can do. Here is an example of a Noticent configuration file in full:\n\n```ruby\nNoticent.configure do\n    hooks :pre_channel_registration, my_hooks\n    hooks :post_alert_registration, my_hooks\n\n    channel :email\n    channel :slack\n    channel :webhook, klass: MyWebhookChannel\n    channel :dashboard, group: :internal\n    \n    product :product_foo\n    product :product_buzz\n    product :product_bar\n\n    scope :account, check_constructor: false do\n        alert :new_user do\n            applies.to :product_foo\n            notify :users\n            notify(:staff).on(:internal)\n            notify :owners\n            \n            default true             \n        end\n    end\n\n    scope :comment do\n        alert :new_comment, constructor_name: :some_constructor do\n            applies.not_to :product_buzz\n            notify :commenter\n            notify :author\n            \n            default true  \n            default(false) { on(:email) }            \n        end\n        alert :comment_updated do\n            notify :commenter\n        end\n    end\n\n    scope :staff_comment, payload_class: AnotherPayloadClass do\n        alert :marked_as_answer do\n            notify(:staff).on(:internal)\n        end\n    end\nend\n```\n\n### Sending Alerts\n\nTo send an Alert, call the `notify` method:\n\n```ruby\naccount_payload = AccountPayload.new(1, user.first)\nNoticent.notify(:new_user, account_payload)\n```\n\nWhile it is possible to define and use alert names as symbols, Noticent also creates a constant with the name of the alert under the `Noticent` namespace to help with the use of alert names.\nBy using the constants you can make sure alert names are free of typos.\n\nFor example, if you have an alert called `some_event` then after configuration there will be a constant called `Noticent::ALERT_SOME_EVENT` available to use with the value `:some_event`.  \n\n### Using Each Noticent Component\n\n#### Payload\n\nTo understand how to use Noticent, it's important to know the conventions it uses. First, payloads: A payload is a class and should have methods named after each one of the recipient groups specified in the configuration. For example, if an alert should be sent to `users` then payload should have a method or attribute called `users`. This method is called at the point the notifications need to be sent to retrieve the recipients. It is up to you what each recipient is: it could be an email address (string) or the entire user object or an ID. Your channel class will be given this and should know how to handle it.\n\nIt is recommended to explicitly define the class type of each scope. This ensures integrity of the alerts in runtime:\n\n```ruby\nNoticent.configure do\n    scope :account, payload_class: SomeOtherClass do\n        #...\n    end\nend\n```\n\nIf specified, the type of the payload is checked against this class at runtime (when `Notify` is called).\n\nTo enforce development type consistency payload should have class method constructors that are named after the alert names. This can be turned off by setting `check_constructor` on scopes to `false`. \nTo share the same class method constructor for different alerts, you can use the `constructor_name` on alert to tell Noticent to look for a constructor that is not named after the alert itself.\nThis is a validation step only and doesn't affect the performance of Noticent.\n\n#### Channel\n\nChannels should be derived from `::Noticent::Channel` class and called the same as with the name of the channel with a `Channel` suffix: `email` would be `EmailChannel` and `slack` will be `SlackChannel`. Also, channels should have a method for each type of alert they are supposed to handle. Channel class can be changed using the `klass` argument during definition.\n\nChannels can also have groups. If no group is supplied, a channel will belong to the `default` group. Groups can be used to send alerts to a subset of channels:\n\n```ruby\nNoticent.configure do\n    channel :email\n    channel :private_emails, group: :internal\n    channel :slack\n    channel(:team_slack, klass: Slack, group: :internal) do\n      using(fuzz: :buzz) \n    end \n\n    alert :some_event do\n        notify :users\n        notify(:staff).on(:internal)\n    end\nend\n```\n\nIn the example above, we are creating 2 flavors of the slack channel, one called `team_slack` but using the same class and configured differently. When `using` is used in a channel, any attribute passed into `using` will be called on the channel after creation with the given values.\nFor example, in this example, the `Slack` class is instantiated and attribute `fuzz` is set to `:buzz` on it before the alert method is called.\n\nYou can use `on` with a channel name instead of a channel group name instead:\n\n```ruby\nNoticent.configure do\n    channel :email\n    channel :private_emails, group: :internal\n    channel :slack\n\n    alert :some_event do\n        notify(:users).on(:internal) # this is a group name\n        notify(:staff).on(:slack)    # this is a channel name\n    end\nend\n```  \n\nYou can use `render` in the channel code to render and return the view file and its front matter (if available). By default, channel will look for `html` and `erb` as the file content and format. You can change these both when calling `render` or at the top of the controller:\n\n```ruby\nclass SlackChannel \u003c ::Noticent::Channel\n    default_format :json\n    default_ext :erb\nend\n```\n\nor\n\n```ruby\ndata, content = render(format: :erb, ext: :json)\n```\n\nYou can also use a different layout for each render:\n\n```ruby\ndata, content = render layout: 'my_layout'\n```\n\nBy default, no layout is used.\n\n#### Views\n\nViews are like Rails views. Noticent supports rendering ERB files. You can also use layouts just like Rails. A layout is like a shared template `layout.html.erb`:\n\n```html\nThis is at the top\n\n\u003c%= @content %\u003e\n\nThis is at the bottom\n```\n\n`some_event.html.erb`:\n\n```html\nfoo: bar\nbuzz: fuzz\n---\nThis will be in the middle\n```\n\nViews can be of any type, like HTML or JSON which can be useful with API based channels.\n\n## Opt-ins\n\nNoticent uses a combination of channel, alert and scope to determine if a recipient has subscribed to receive an alert or not. By default it uses the `ActiveRecordOptInProvider` class which uses a single database table for the process. You can write your own Opt-in provider if you want to store subscription (opt-in) state in a different place. See `ActiveRecordOptInProvider` for what such provider requires to operate.\n\nUse `Noticent.configuration.opt_in_provider`'s `opt_in`, `opt_out` and `opted_in?` methods to change the opt-in state of each recipient.\n\n## Default Values\n\nYou can specify a default opt-in value for each alert. By default alerts have a default value of `false` (no opt-in) unless this is globally changed (see Customization section). \n\nThe default value for an alert can be set while this can also be changed per channel. For example:\n\n```ruby\nNoticent.configure do\n  channel :email \n  channel :slack\n  channel :webhook\n  \n  scope :post do \n    alert :foo do \n      notify :users \n      default(true) # sets the default value for all channels for this alert to true \n      default(false) { on(:slack) } # sets the default value for this alert to false for the slack channel only\n    end\n  end\nend\n```\n\n## Migration\n\nNoticent provides a method to add new alerts or remove deprecated alerts from the existing recipients. To add a new alert type, you can use `ActiveRecordOptInProvider.add_alert` method:\n\n```ruby\nNoticent.opt_in_provider.add_alert(scope: :foo, alert_name: :some_new_alert, recipient_ids: [1, 2, 3, 5, 6], channel: :email)\n```\n\nThis will opt-in recipients with the given IDs for the new alert on the `email` channel. \n\nTo remove any deprecated alert, use the `ActiveRecordOptInProvider.remove_alert` method:\n\n```ruby\nNoticent.opt_in_provider.remove_alert(scope: :foo, alert_name: :some_old_alert)\n``` \n\nThis removes all instances of the old alert from the opt-ins.\n\n## New Recipient Sign up\n\nWhen a new recipient signs up, you might want to make sure they have all the default alerts setup for them. You can achieve this by calling `Noticent.setup_recipient`:\n\n```ruby\nNoticent.setup_recipient(recipient_id: 1, scope: :post, entity_ids: [2])\n``` \n\nThis will adds the default opt-ins for recipient 1 on all channels that are applicable to it on scope `post` for entity 2.\n\n## Validation\n\nEvery time Noticent starts, it runs some validations on the configuration, classes that are defined, channels and alerts to make sure they are defined correctly and the supporting classes are in compliance with the requirements.\n\n## Testing Your Alerts\n\nTO BE WRITTEN\n\n## Hooks\n\nHooks are extension points for Noticent. You can register them in your configuration:\n\n```ruby\nNoticent.configure do\n    hooks.add(:pre_channel_registration, custom_hook)\nend\n```\n\nThe valid hook points are `pre_channel_registration`, `post_channel_registration`, `pre_alert_registration` and `post_alert_registration`. Once the hook point is reached, the given object is called on the same method name as with the hook name.\n\n## Customization\n\nThe following items can be customized:\n\n`base_dir`: Base directory for all Noticent assets.\n\n`base_module_name`: Base module name for all Noticent assets.\n\n`opt_in_provider`: Opt-in provider class. Default is `ActiveRecordOptInProvider`.\n\n`logger`: Logger class. Default is `stdout`\n\n`halt_on_error`: Should notification fail after the first incident of an error during rendering. Default is `false`\n\n`default_value`: Default value for all alerts unless explicitly specified. Default is `false`\n\n`use_sub_modules`: If set to true, Noticent will look for Channel and Scope classes in sub modules under the `base_module_name`.\nWith `use_sub_modules` set to false, a channel named `:email` should be called `Noticent::Email` (if `base_module_name` is `Noticent`), while with `use_sub_modules` set to true, the same class should be `Noticent::Channels::Email`.\nFor Payloads, the sub module name will be `Payloads`. \n\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/khash/noticent.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloud66-oss%2Fnoticent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcloud66-oss%2Fnoticent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloud66-oss%2Fnoticent/lists"}