test nearest API
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user