Skip to content

Instantly share code, notes, and snippets.

@libryder
Created January 19, 2012 16:23
Show Gist options
  • Save libryder/1640969 to your computer and use it in GitHub Desktop.
Save libryder/1640969 to your computer and use it in GitHub Desktop.
Attempt at adhearsion canadian georouting
#return a 6 digit DTMF entry from the caller
postcode = input 6, :speak => { :text => 'enter your postal code' }
postparts = postcode.split(//)
#simple map of possible alphabetical entries for a canadian post code
dtmf_map = { '2' => ['"A"','"B"','"C"'], '3' => ['"E"'], '4' => ['"G"','"H"'], '5' => ['"J"','"K"','"L"'], '6' => ['"M"','"N"'], '7' => ['"P"','"R"','"S"'], '8' => ['"T"','"V"'], '9' => ['"X"','"Y"'] }
#first, third, and fifth characters are always alphabetical
first_char = dtmf_map[postparts[0]].join(',')
third_char = dtmf_map[postparts[2]].join(',')
fifth_char = dtmf_map[postparts[4]].join(',')
query = "substring(post_code, 1, 1) in (#{first_char}) and
substring(post_code, 2, 1) = '#{postparts[1]}' and
substring(post_code, 3, 1) in (#{third_char}) and
substring(post_code, 4, 1) = '#{postparts[3]}' and
substring(post_code, 5, 1) in (#{fifth_char}) and
substring(post_code, 6, 1) = '#{postparts[5]}'"
codes = CanadaPost.where(query)
#this query returns anywhere between 1-10 matches. For my purpose, this was too many results
if codes.length == 1
speak "You are calling from #{codes.first.location}"
else
speak "the number of matches - #{codes.length} - are too damn high"
end
hangup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment