Sha256: af6869cd764bc592845d7ceff0f55ed3cf4e3a201be3310cb297dd927bf098be

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

module Moip::Assinaturas
  class Webhooks
    attr_accessor :model, :event, :date, :env, :resource, :events

    class << self
      def build(json)
        object = new
        object.model    = get_model(json['event'])
        object.event    = get_event(json['event'])
        object.events   = {}
        object.date     = json['date']
        object.env      = json['env']
        object.resource = json['resource']

        object
      end

      def listen(params, &block)
        hook = build(params)
        yield hook
        hook.run
      end

      private
      def get_model(event)
        event.split(".")[0].to_sym
      end

      def get_event(event)
        event.split(".")[1].to_sym
      end
    end

    def on(model, event, &block)
      unless events[model]
        events[model] = {}
      end

      unless events[model][event]
        events[model][event] = []
      end

      events[model][event] << block
    end

    def run
      events[model][event].each { |action| action.call } if (events[model] && events[model][event])
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
moip-assinaturas-0.1.0 lib/moip-assinaturas/webhooks.rb