summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
authorPeter Kozak <spag@golwen.net>2013-05-23 11:49:00 +0200
committerPeter Kozak <spag@golwen.net>2013-05-23 11:49:00 +0200
commit918b10d26c2f1e4dfddd0894944b0e964636a5e7 (patch)
tree64209bd6ec037be54489a01f256e02874d2fca25 /app/models
parent8e239c856f6100d89611fca00a507499a342858e (diff)
Route XML interpreter added
Diffstat (limited to 'app/models')
-rw-r--r--app/models/call_route.rb66
1 files changed, 66 insertions, 0 deletions
diff --git a/app/models/call_route.rb b/app/models/call_route.rb
index 590d49b..7b98e5d 100644
--- a/app/models/call_route.rb
+++ b/app/models/call_route.rb
@@ -15,6 +15,8 @@ class CallRoute < ActiveRecord::Base
acts_as_list :scope => '`routing_table` = \'#{routing_table}\''
+ after_save :create_elements
+
def to_s
name.to_s
end
@@ -253,6 +255,47 @@ class CallRoute < ActiveRecord::Base
end
end
+ def xml
+ @xml
+ end
+
+ def xml=(xml_string)
+ @xml = xml_string
+ if xml_string.blank?
+ return
+ end
+
+ begin
+ route_hash = Hash.from_xml(xml_string)
+ rescue Exception => e
+ errors.add(:xml, e.message)
+ return
+ end
+
+ if route_hash['call_route'].class == Hash
+ call_route = route_hash['call_route']
+ self.routing_table = call_route['routing_table'].downcase
+ self.name = call_route['name'].downcase
+ self.position = call_route['position']
+ self.endpoint_type = call_route['endpoint_type']
+ endpoint_from_type_name(call_route['endpoint_type'], call_route['endpoint'])
+
+ if route_hash['call_route']['route_elements'] && route_hash['call_route']['route_elements']['route_element']
+ if route_hash['call_route']['route_elements']['route_element'].class == Hash
+ @elements_array = [route_hash['call_route']['route_elements']['route_element']]
+ else
+ @elements_array = route_hash['call_route']['route_elements']['route_element']
+ end
+ end
+ elsif route_hash['route_elements'].class == Hash && route_hash['route_elements']['route_element']
+ if route_hash['route_elements']['route_element'].class == Hash
+ @elements_array = [route_hash['route_elements']['route_element']]
+ else
+ @elements_array = route_hash['route_elements']['route_element']
+ end
+ end
+
+ end
def self.test_route(table, caller)
arguments = ["'#{table}' table"]
@@ -269,4 +312,27 @@ class CallRoute < ActiveRecord::Base
return JSON.parse(result)
end
+ private
+ def endpoint_from_type_name(endpoint_type, endpoint_name)
+ endpoint_type = endpoint_type.to_s.downcase
+ if endpoint_type == 'phonenumber'
+ self.endpoint_type = 'PhoneNumber'
+ self.endpoint_id = nil
+ elsif endpoint_type == 'gateway'
+ gateway = Gateway.where(:name => endpoint_name).first
+ if gateway
+ self.endpoint_type ='Gateway'
+ self.endpoint_id = gateway.id
+ end
+ end
+ end
+
+ def create_elements
+ if @elements_array && @elements_array.any?
+ @elements_array.each do |element_hash|
+ element = self.route_elements.create(element_hash)
+ end
+ end
+ end
+
end