lib/punchblock/component/asterisk/ami/action.rb in punchblock-1.9.4 vs lib/punchblock/component/asterisk/ami/action.rb in punchblock-2.0.0.beta1
- old
+ new
@@ -1,146 +1,59 @@
# encoding: utf-8
-require 'punchblock/key_value_pair_node'
-
module Punchblock
module Component
module Asterisk
module AMI
class Action < ComponentNode
register :action, :ami
- def self.new(options = {})
- super().tap do |new_node|
- options.each_pair { |k,v| new_node.send :"#{k}=", v }
- end
- end
+ attribute :name
+ attribute :params, Hash, default: {}
- def name
- read_attr :name
- end
-
- def name=(other)
- write_attr :name, other
- end
-
- ##
- # @return [Hash] hash of key-value pairs of params
- #
- def params_hash
- params.inject({}) do |hash, param|
- hash[param.name.downcase.gsub('-', '_').to_sym] = param.value
- hash
+ def inherit(xml_node)
+ xml_node.xpath('//ns:param', ns: self.class.registered_ns).to_a.each do |param|
+ params[param[:name]] = param[:value]
end
+ super
end
- ##
- # @return [Array[Param]] params
- #
- def params
- find('//ns:param', :ns => self.class.registered_ns).map do |i|
- Param.new i
- end
+ def rayo_attributes
+ {'name' => name}
end
- ##
- # @param [Hash, Array] params A hash of key-value param pairs, or an array of Param objects
- #
- def params=(params)
- find('//ns:param', :ns => self.class.registered_ns).each(&:remove)
- if params.is_a? Hash
- params.each_pair { |k,v| self << Param.new(k, v) }
- elsif params.is_a? Array
- [params].flatten.each { |i| self << Param.new(i) }
+ def rayo_children(root)
+ super
+ params.each do |name, value|
+ root.param name: name, value: value
end
end
- def inspect_attributes # :nodoc:
- [:name, :params] + super
- end
-
- class Param < RayoNode
- include KeyValuePairNode
- end
-
class Complete
class Success < Event::Complete::Reason
register :success, :ami_complete
- def self.new(options = {})
- super().tap do |new_node|
- case options
- when Nokogiri::XML::Node
- new_node.inherit options
- else
- options.each_pair { |k,v| new_node.send :"#{k}=", v }
- end
- end
- end
+ attribute :message
+ attribute :text_body
+ attribute :headers, Hash, default: {}
- def message_node
- mn = if self.class.registered_ns
- find_first 'ns:message', :ns => self.class.registered_ns
- else
- find_first 'message'
- end
+ alias :attributes :headers
- unless mn
- self << (mn = RayoNode.new('message', self.document))
- mn.namespace = self.class.registered_ns
- end
- mn
- end
+ def inherit(xml_node)
+ message_node = xml_node.at_xpath 'ns:message', ns: self.class.registered_ns
+ self.message = message_node.text if message_node
- def message
- message_node.text
- end
+ text_body_node = xml_node.at_xpath 'ns:text-body', ns: self.class.registered_ns
+ self.text_body = text_body_node.text if text_body_node
- def message=(other)
- message_node.content = other
- end
-
- ##
- # @return [Hash] hash of key-value pairs of attributes
- #
- def attributes_hash
- attributes.inject({}) do |hash, attribute|
- hash[attribute.name.downcase.gsub('-', '_').to_sym] = attribute.value
- hash
+ xml_node.xpath('//ns:attribute', ns: self.class.registered_ns).to_a.each do |attribute|
+ headers[attribute[:name]] = attribute[:value]
end
+ super
end
-
- ##
- # @return [Array[Attribute]] attributes
- #
- def attributes
- find('//ns:attribute', :ns => self.class.registered_ns).map do |i|
- Attribute.new i
- end
- end
-
- ##
- # @param [Hash, Array] attributes A hash of key-value attribute pairs, or an array of Attribute objects
- #
- def attributes=(attributes)
- find('//ns:attribute', :ns => self.class.registered_ns).each(&:remove)
- if attributes.is_a? Hash
- attributes.each_pair { |k,v| self << Attribute.new(k, v) }
- elsif attributes.is_a? Array
- [attributes].flatten.each { |i| self << Attribute.new(i) }
- end
- end
-
- def inspect_attributes
- [:message, :attributes_hash]
- end
end
-
- class Attribute < RayoNode
- include KeyValuePairNode
- end
- end # Complete
- end # Action
- end # AMI
- end # Asterisk
- end # Component
-end # Punchblock
+ end
+ end
+ end
+ end
+ end
+end