class Moip::Webhooks

see moiplabs.github.io/assinaturas-docs/webhooks.html

Attributes

date[RW]
env[RW]
event[RW]
resource[RW]

Public Class Methods

build(params) click to toggle source
# File lib/moip/webhooks.rb, line 40
def build params
        object = new
        
        params.each do |key, value|
                object.send("#{key}=".to_sym, value) if object.respond_to? key.to_sym
        end

        object
end
listen(json) { |hook| ... } click to toggle source
# File lib/moip/webhooks.rb, line 50
def listen(json, &block)
        hook = build json
        yield hook
        hook.run
end

Public Instance Methods

events() click to toggle source
# File lib/moip/webhooks.rb, line 6
def events
        @events ||= []
end
on(event, kind, &block) click to toggle source
# File lib/moip/webhooks.rb, line 10
def on event, kind, &block
        hash = {}
        hash[event] = { :kind => kind, :runnable => block }
        self.events << hash
end
resource=(params) click to toggle source
# File lib/moip/webhooks.rb, line 33
def resource= params
        klass = "Moip::#{self.event.split(".")[0].capitalize}" # E.g Moip::Plan when event plan.created
        klass = eval klass
        @resource = klass.build params
end
run() click to toggle source
# File lib/moip/webhooks.rb, line 16
def run
        event = self.event.split(".")[0]
        kind = self.event.split(".")[1]
        action = nil

        self.events.each do |hash|
                e_sym = event.to_sym 
                if hash.has_key? e_sym
                        if hash[e_sym][:kind].to_s == kind
                                action = hash[e_sym][:runnable]
                        end
                end
        end

        action.call if action.is_a? Proc
end