diff options
author | Stefan Wintermeyer <stefan.wintermeyer@amooma.de> | 2013-06-20 19:06:19 +0200 |
---|---|---|
committer | Stefan Wintermeyer <stefan.wintermeyer@amooma.de> | 2013-06-20 19:06:19 +0200 |
commit | eb0e1cc5c26275ff3e5c341404e8bc558f8312b8 (patch) | |
tree | 71f449ccd6f15422717de3ac24f87d5e888ddd79 /app/models/call_route.rb | |
parent | df6e17e48995f25e72509986f30700d778b179b6 (diff) | |
parent | 3b27a5d45b12f6bac65da2a8e17387bfda42a2f1 (diff) |
Merge branch 'develop'
Diffstat (limited to 'app/models/call_route.rb')
-rw-r--r-- | app/models/call_route.rb | 66 |
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 |