Skip to content

Instantly share code, notes, and snippets.

@Electron-libre
Created March 31, 2011 13:18
Show Gist options
  • Save Electron-libre/896314 to your computer and use it in GitHub Desktop.
Save Electron-libre/896314 to your computer and use it in GitHub Desktop.
def self.resolve(cdr)
prefixes_array = Array.new
cdr.called_number.each_char do |char|
prefixes_array << (prefixes_array[-1] or '') + char
end
prefixes_array.reverse!
best_match = Proc.new do |destinations, prefixes|
result = nil
while result == nil && prefixes != [] do
prefix = prefixes.shift
destinations.each do |entry|
result = entry.reload if entry.prefixes.include?(prefix)
end
end
result
end
destination = best_match.call(
Destination.only(:prefixes).where(:for_vno => cdr.deployment_id).any_in(:prefixes => prefixes_array).entries,
prefixes_array.dup
)
if destination == nil
destination = best_match.call(
Destination.only(:prefixes).where(:for_vno => nil).any_in(:prefixes => prefixes_array).entries,
prefixes_array.dup
)
end
return destination
end
@Electron-libre
Copy link
Author

def self.resolve(cdr)

prefixes_array = Array.new
cdr.called_number.each_char do |char|
  prefixes_array << (prefixes_array[-1] or '') + char
end
prefixes_array.reverse!

best_match = Proc.new do |vno, prefixes|
  result = nil
  while result == nil && prefixes != [] do
    prefix = prefixes.shift
    Destination.only(:prefixes).where(:for_vno => vno).any_in(:prefixes => prefixes_array).entries.each do |entry|
      result = entry.reload if entry.prefixes.include?(prefix)
    end
  end
  result
end

destination = best_match.call(
    cdr.deployment_id,
    prefixes_array.dup
)

if destination == nil
  destination = best_match.call(
      nil,
      prefixes_array.dup
  )
end

return destination

end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment