Git Product home page Git Product logo

pubsub_notifier's Introduction

PubsubNotifier

Build Status Gem Version

Publish-Subscribe Notifier for Ruby on Rails.

This gem relies on krisleech/wisper provides Pub/Sub capabilities.

Installation

gem 'pubsub_notifier'

And you can run the generator, which will set up an application notifier with some useful defaults for you:

rails g pubsub_notifier:install

After generating your application notifier, restart the Rails server so that Rails can pick up any classes in the new app/notifiers/ directory.

Optionally, you need to perform asynchronously. Add sidekiq or resuque to your Gemfile:

gem 'sidekiq'

Then set your queue adapter of active_job:

Rails.application.config.active_job.queue_adapter = :sidekiq

Usage

ActiveRecord

class User < ApplicationRecord
  subscribe :UserMailer
  subscribe :UserNotifier, async: true, queue: :high
end

class UserMailer < ApplicationMailer
  acts_as_notifier

  default from: '[email protected]'

  def welcome(user)
    @user = user
    mail(to: @user.email, subject: 'Welcome to My Awesome Site')
  end
end

class UserNotifier < ApplicationNotifier
  use :slack, channel: '#general', username: 'angel', icon_emoji: ':innocent:'

  def welcome(user)
    notify_success("#{user.name} has joined.")
  end
end

user = User.first
user.broadcast(:welcome) # or user.publish(:welcome)

Pure Ruby Object

class Register
  include PubsubNotifier::Proxy

  subscribe :UserRegisterNotifier

  attr_accessor :name

  validates :name, presence: true

  def execute
    if valid?
      broadcast(:success)
    else
      broadcast(:failure)
    end
  end
end

class UserRegisterNotifier < ApplicationNotifier
  use :slack, channel: '#random', username: 'noreply', icon_emoji: ':grinning:'

  def success(context)
    notify_success('succeed')
  end

  def failure(context)
    notify_failure('failure')
  end
end

register = Register.new(name: 'hello')
register.execute

Configuration

# config/initializers/pubsub_notifier.rb
PubsubNotifier.config.logger = Rails.logger

require "pubsub_notifier/slack_client"

PubsubNotifier::SlackClient.configure do |config|
  config.default_channel    = ENV['SLACK_DEFAULT_CHANNEL']
  config.default_username   = ENV['SLACK_DEFAULT_USERNAME']
  config.default_icon_emoji = ENV['SLACK_DEFAULT_ICON_EMOJI']
  config.webhook_url        = ENV['SLACK_WEBHOOK_URL']
end

Generate a Notifier class

You can generate a notifier class by generate command:

rails g notifier user
      create  app/notifiers/user_notifier.rb
      invoke  test_unit
      create    test/notifiers/user_notifier_test.rb

Implement Notification Client

You can easily implement Client for Notification like plugin:

module PubsubNotifier
  class SomethingClient < ::PubsubNotifier::Client::Base
    def initialize(options = {})
      # implement this
    end

    def notify_success(message)
      # implement this
    end

    def notify_failure(message)
      # implement this
    end

    def something_special(*args)
      # implement if you need
    end
  end
end

PubsubNotifier.clients :something, PubsubNotifier::SomethingClient

Then you can use on a notifier class:

class SomeNotifier < ApplicationNotifier
  use :something

  def welcome(recipient)
    notify_success(recipient.name)
  end
end

If you want to know more information to implement notification client, please check lib/pubsub_notifier/slack_client.rb.

License

The gem is available as open source under the terms of the MIT License.

pubsub_notifier's People

Contributors

yhirano55 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

pubsub_notifier's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.