Class: Voice::Actions::Notify

Inherits:
Object
  • Object
show all
Defined in:
lib/vonage/voice/actions/notify.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Notify

Returns a new instance of Notify.



8
9
10
11
12
13
14
# File 'lib/vonage/voice/actions/notify.rb', line 8

def initialize(attributes = {})
  @payload = attributes.fetch(:payload)
  @eventUrl = attributes.fetch(:eventUrl)
  @eventMethod = attributes.fetch(:eventMethod, nil)

  after_initialize!
end

Instance Attribute Details

#eventMethodObject

Returns the value of attribute eventMethod.



6
7
8
# File 'lib/vonage/voice/actions/notify.rb', line 6

def eventMethod
  @eventMethod
end

#eventUrlObject

Returns the value of attribute eventUrl.



6
7
8
# File 'lib/vonage/voice/actions/notify.rb', line 6

def eventUrl
  @eventUrl
end

#payloadObject

Returns the value of attribute payload.



6
7
8
# File 'lib/vonage/voice/actions/notify.rb', line 6

def payload
  @payload
end

Instance Method Details

#actionObject



44
45
46
# File 'lib/vonage/voice/actions/notify.rb', line 44

def action
  create_notify!(self)
end

#after_initialize!Object



16
17
18
19
20
21
22
# File 'lib/vonage/voice/actions/notify.rb', line 16

def after_initialize!
  validate_event_url

  if self.eventMethod
    validate_event_method
  end
end

#create_notify!(builder) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vonage/voice/actions/notify.rb', line 48

def create_notify!(builder)
  ncco = [
    {
      action: 'notify',
      payload: builder.payload,
      eventUrl: builder.eventUrl
    }
  ]

  ncco[0].merge!(eventMethod: builder.eventMethod) if builder.eventMethod

  ncco
end

#validate_event_methodObject

Raises:



38
39
40
41
42
# File 'lib/vonage/voice/actions/notify.rb', line 38

def validate_event_method
  valid_methods = ['GET', 'POST']

  raise ClientError.new("Invalid 'eventMethod' value. must be either: 'GET' or 'POST'") unless valid_methods.include?(self.eventMethod.upcase)
end

#validate_event_urlObject

Raises:



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/vonage/voice/actions/notify.rb', line 24

def validate_event_url
  event_url = self.eventUrl

  unless event_url.is_a?(Array) && event_url.length == 1 && event_url[0].is_a?(String)
    raise ClientError.new("Expected 'eventUrl' parameter to be an Array containing a single string item")
  end

  uri = URI.parse(event_url[0])

  raise ClientError.new("Invalid 'eventUrl' value, array must contain a valid URL") unless uri.kind_of?(URI::HTTP) || uri.kind_of?(URI::HTTPS)

  self.eventUrl
end