Class: Moip::Webhooks

Inherits:
Object
  • Object
show all
Defined in:
lib/moip/webhooks.rb

Overview

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dateObject

Returns the value of attribute date.



4
5
6
# File 'lib/moip/webhooks.rb', line 4

def date
  @date
end

#envObject

Returns the value of attribute env.



4
5
6
# File 'lib/moip/webhooks.rb', line 4

def env
  @env
end

#eventObject

Returns the value of attribute event.



4
5
6
# File 'lib/moip/webhooks.rb', line 4

def event
  @event
end

#resourceObject

Returns the value of attribute resource.



4
5
6
# File 'lib/moip/webhooks.rb', line 4

def resource
  @resource
end

Class Method Details

.build(params) ⇒ Object



40
41
42
43
44
45
46
47
48
# 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| ... } ⇒ Object

Yields:

  • (hook)


50
51
52
53
54
# File 'lib/moip/webhooks.rb', line 50

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

Instance Method Details

#eventsObject



6
7
8
# File 'lib/moip/webhooks.rb', line 6

def events
  @events ||= []
end

#on(event, kind, &block) ⇒ Object



10
11
12
13
14
# File 'lib/moip/webhooks.rb', line 10

def on event, kind, &block
  hash = {}
  hash[event] = { :kind => kind, :runnable => block }
  self.events << hash
end

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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