Project

catch_box

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
A lightweight and straightforward system for easy hooks set up
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.1, >= 2.1.4
~> 1.1
~> 13.0, >= 13.0.1
~> 3.9
~> 0.17.1

Runtime

> 1, < 3
 Project Readme

Gem Version RSpec

Catch Box

A lightweight and straightforward system for easy hooks set up.

Installation

Add this line to your application's Gemfile:

gem "catch_box", "~> 0.1.0"

And then execute:

bundle

Usage

Base

Define fanout

require "catch_box/fanout"

class Delivered
  def initialize(logger: Logger.new)
    @logger = logger
  end

  def call(payload)
    @logger.info(payload)
  end
end

class Fanout
  extend ::CatchBox::Fanout

  event "event-data.event"

  auth do |payload, env|
    # https://documentation.mailgun.com/en/latest/user_manual.html#webhooks
    payload["signature"]["signature"] == \
      ::OpenSSL::HMAC.hexdigest(
        ::OpenSSL::Digest::SHA256.new,
        ENV["MAILGUN_API_KEY"],
        [payload["signature"]["timestamp"], payload["signature"]["token"]].join
      )
  end

  on "delivered", Delivered.new

  all do |payload|
    ::Logger.new(::STDOUT).info(payload)
  end
end

Use middleware

require "sinatra/base"
require "catch_box/middleware"

class Application < Sinatra::Base
  use ::CatchBox::Middleware, fanout: Fanout, endpoint: "/mailgun"

  get "/" do
    "index"
  end
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/chubchenko/catch_box.

License

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