test nearest API

This commit is contained in:
Emil Tin
2013-02-03 20:17:06 +01:00
parent 7544727f7a
commit ccdd0f599a
5 changed files with 239 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
class FuzzyMatch
def self.match got, want
if got == want
return true
elsif want.match /(.*)\s+~(.+)%$/ #percentage range: 100 ~5%
margin = 1 - $2.to_f*0.01
from = $1.to_f*margin
to = $1.to_f/margin
return got.to_f >= from && got.to_f <= to
elsif want.match /(.*)\s+\+\-(.+)$/ #absolute range: 100 +-5
margin = $2.to_f
from = $1.to_f-margin
to = $1.to_f+margin
return got.to_f >= from && got.to_f <= to
elsif want =~ /^\/(.*)\/$/ #regex: /a,b,.*/
return got =~ /#{$1}/
else
return false
end
end
def self.match_location got, want
match( got[0], "#{want.lat} ~0.002%" ) &&
match( got[1], "#{want.lon} ~0.002%" )
end
end
+17
View File
@@ -0,0 +1,17 @@
require 'net/http'
def request_nearest_url path
@query = path
uri = URI.parse "#{HOST}/#{path}"
Timeout.timeout(REQUEST_TIMEOUT) do
Net::HTTP.get_response uri
end
rescue Errno::ECONNREFUSED => e
raise "*** osrm-routed is not running."
rescue Timeout::Error
raise "*** osrm-routed did not respond."
end
def request_nearest a
request_nearest_url "nearest?loc=#{a}"
end