{"id":18336365,"url":"https://github.com/launchpadlab/notification_engine","last_synced_at":"2025-04-09T19:55:34.485Z","repository":{"id":147465462,"uuid":"89505935","full_name":"LaunchPadLab/notification_engine","owner":"LaunchPadLab","description":null,"archived":false,"fork":false,"pushed_at":"2017-05-03T03:57:19.000Z","size":46,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-09T19:55:22.760Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","has_issues":true,"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/LaunchPadLab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"MIT-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":"2017-04-26T16:56:58.000Z","updated_at":"2017-04-26T16:57:16.000Z","dependencies_parsed_at":"2023-07-25T01:16:33.118Z","dependency_job_id":null,"html_url":"https://github.com/LaunchPadLab/notification_engine","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaunchPadLab%2Fnotification_engine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaunchPadLab%2Fnotification_engine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaunchPadLab%2Fnotification_engine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaunchPadLab%2Fnotification_engine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LaunchPadLab","download_url":"https://codeload.github.com/LaunchPadLab/notification_engine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248103911,"owners_count":21048245,"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-05T20:07:41.601Z","updated_at":"2025-04-09T19:55:34.463Z","avatar_url":"https://github.com/LaunchPadLab.png","language":"Ruby","readme":"# NotificationEngine Overview\n\nFlexibly create notifications that can be sent across multiple mediums, like email, SMS, and push.\n\n## Overview\n\nThere are two types of objects: `Notification` and `Medium`\n\n`Notification` is a parent class for different types of notifications. For example, a subclass may be: CalendarEventChangeNotification which is created for every user that is notified of a calendar event change.\n\n`Mediums::Base` is a parent class for different types of communication mediums. For example, email, SMS, or push.\n\n## Usage\n\nThere are four steps to wire up a notification:\n\n1. Add Medium(s)\n2. Add Notification\n3. Write notification template for each medium\n4. Wire up callback to send notifications\n\n### 1. Creating Mediums\n\n```\nrails g medium Email\nrails g medium Sms\n```\n\nAfter making some changes to the generated file to suit our needs, we end up with something like the following:\n\n```ruby\nmodule Mediums\n  class EmailMedium \u003c NotificationEngine::Mediums::Base\n    attr_reader :email, :subject, :body\n\n    def after_init(args = {})\n      @email = args[:email]\n      @subject = args.fetch(:subject, parse_template(:subject))\n      @body = args.fetch(:body, parse_template(:body))\n    end\n\n    def deliver\n      NotificationMailer.basic_notification(message_args).deliver_later\n    end\n\n    def slug\n      'email'\n    end\n\n    private\n\n    def required_attrs\n      [:email]\n    end\n\n    def message_args\n      @message_args ||= {\n        to: email,\n        subject: subject,\n        body: body\n      }\n    end\n  end\nend\n```\n\n```ruby\nmodule Mediums\n  class SmsMedium \u003c NotificationEngine::Mediums::Base\n    attr_reader :body, :mobile_phone\n\n    def after_init(args = {})\n      @mobile_phone = args[:mobile_phone]\n      @body = args.fetch(:body, parse_template(:body))\n    end\n\n    def deliver\n      # hook up Twilio or other SMS service here\n      # we can pass in mobile_phone and body as args\n    end\n\n    def slug\n      'sms'\n    end\n\n    private\n\n    def required_attrs\n      [:mobile_phone]\n    end\n  end\nend\n```\n\nThe only two required methods for a `Medium` are `slug` and `deliver`. You'll see why the slug is important here in a bit.\n\n### 2. Creating a Notification\n\n```\nrails g notification NewTeamMember\n```\n\n```ruby\nclass NewTeamMemberNotification \u003c NotificationEngine::Notification\n  def mediums\n    [:email, :sms]\n  end\n\n  def data\n    {\n      email: 'ned@example.com',\n      mobile_phone: '123-456-1828',\n      recipient: {\n        name: 'Ned Stark'\n      },\n      new_user: {\n        name: 'Jon Snow'\n      }\n    }\n  end\nend\n```\n\nIn this case, `email` is used by the `email` medium and `mobile_phone` is used by `sms` medium. The other information in `data` will be used by our template.\n\n### 3. Write Template\n\nIn config/locales/en.yml:\n\n```yml\nen:\n  notifications:\n    new-team-member-notification:\n      email:\n        subject: '{{ new_user.name }} was just created'\n        body: \u003e\n          Hi {{ recipient.name }},\n          A new user named {{ new_user.name }} was added to your team. Please login to review the new user.\n    another-example-notification:\n      all:\n        body: An example body that would work across all mediums for this notification\n```\n\n### 4. Wire up callback to send notifications\n\n```ruby\nclass User \u003c ApplicationRecord\n  after_create :send_notifications\n\n  def send_notifications\n    team_members.each do |team_member|\n      notification = NewTeamMemberNotification.create(\n        recipient: team_member,\n        notifiable: self\n      )\n      notification.deliver\n    end\n  end\nend\n```\n\n## Installation\n\nGemfile:\n\n```ruby\ngem 'notification_engine'\n```\n\n```\nbundle\n```\n\n## License\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaunchpadlab%2Fnotification_engine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flaunchpadlab%2Fnotification_engine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flaunchpadlab%2Fnotification_engine/lists"}