post/get handler added, background section for HTTP request
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
require 'net/http'
|
||||
|
||||
def generate_request_url path
|
||||
if @http_method.eql? "POST"
|
||||
pos = path.index('?') - 1
|
||||
service = path[0..pos]
|
||||
uri = URI.parse "#{HOST}/#{service}"
|
||||
else
|
||||
uri = URI.parse "#{HOST}/#{path}"
|
||||
end
|
||||
end
|
||||
|
||||
def send_simple_request uri, path
|
||||
Timeout.timeout(OSRM_TIMEOUT) do
|
||||
if @http_method.eql? "POST"
|
||||
pos = path.index('=') + 1
|
||||
path.slice!(0, pos)
|
||||
response = Net::HTTP.post_form uri, "loc" => path
|
||||
else
|
||||
response = Net::HTTP.get_response uri
|
||||
end
|
||||
end
|
||||
rescue Errno::ECONNREFUSED => e
|
||||
raise "*** osrm-routed is not running."
|
||||
rescue Timeout::Error
|
||||
raise "*** osrm-routed did not respond."
|
||||
end
|
||||
|
||||
def send_request uri, waypoints, timestamps, options
|
||||
@query = uri.to_s
|
||||
Timeout.timeout(OSRM_TIMEOUT) do
|
||||
if @http_method.eql? "POST"
|
||||
datas = {}
|
||||
if waypoints.length > 0
|
||||
datas[:loc] = waypoints.compact.map { |w| "#{w.lat},#{w.lon}" }
|
||||
end
|
||||
if timestamps.length > 0
|
||||
datas[:t] = timestamps.compact.map { |t| "#{t}" }
|
||||
end
|
||||
datas.merge! options
|
||||
response = Net::HTTP.post_form uri, datas
|
||||
else
|
||||
response = Net::HTTP.get_response uri
|
||||
end
|
||||
end
|
||||
rescue Errno::ECONNREFUSED => e
|
||||
raise "*** osrm-routed is not running."
|
||||
rescue Timeout::Error
|
||||
raise "*** osrm-routed did not respond."
|
||||
end
|
||||
@@ -1,32 +1,12 @@
|
||||
require 'net/http'
|
||||
|
||||
def request_locate_url path, method={}
|
||||
def request_locate_url path
|
||||
@query = path
|
||||
|
||||
if method.has_key?("post")
|
||||
request_method = "POST"
|
||||
else
|
||||
request_method = "GET"
|
||||
end
|
||||
if request_method.eql? "GET"
|
||||
uri = URI.parse "#{HOST}/#{path}"
|
||||
elsif request_method.eql? "POST"
|
||||
uri = URI.parse "#{HOST}/locate"
|
||||
end
|
||||
Timeout.timeout(OSRM_TIMEOUT) do
|
||||
if request_method.eql? "GET"
|
||||
Net::HTTP.get_response uri
|
||||
elsif request_method.eql? "POST"
|
||||
path.slice!(0, 11)
|
||||
Net::HTTP.post_form uri, "loc" => path
|
||||
end
|
||||
end
|
||||
rescue Errno::ECONNREFUSED => e
|
||||
raise "*** osrm-routed is not running."
|
||||
rescue Timeout::Error
|
||||
raise "*** osrm-routed did not respond."
|
||||
uri = generate_request_url path
|
||||
response = send_simple_request uri, path
|
||||
end
|
||||
|
||||
def request_locate a, method
|
||||
request_locate_url "locate?loc=#{a}", method
|
||||
def request_locate a
|
||||
request_locate_url "locate?loc=#{a}"
|
||||
end
|
||||
|
||||
@@ -14,34 +14,7 @@ def request_matching trace=[], timestamps=[], options={}
|
||||
params = (trace_params + defaults.merge(options).to_param).join('&')
|
||||
params = nil if params==""
|
||||
|
||||
if options.has_key?("post")
|
||||
request_method = "POST"
|
||||
options.delete("post")
|
||||
else
|
||||
request_method = "GET"
|
||||
end
|
||||
if request_method.eql? "GET"
|
||||
uri = URI.parse ["#{HOST}/match", params].compact.join('?')
|
||||
elsif request_method.eql? "POST"
|
||||
uri = URI.parse "#{HOST}/match"
|
||||
end
|
||||
@query = uri.to_s
|
||||
Timeout.timeout(OSRM_TIMEOUT) do
|
||||
if request_method.eql? "GET"
|
||||
Net::HTTP.get_response uri
|
||||
elsif request_method.eql? "POST"
|
||||
datas = {}
|
||||
datas[:loc] = trace.compact.map { |w| "#{w.lat},#{w.lon}" }
|
||||
if ts.length > 0
|
||||
datas[:t] = timestamps.compact.map { |t| "#{t}" }
|
||||
end
|
||||
datas.merge! options
|
||||
Net::HTTP.post_form uri, datas
|
||||
end
|
||||
end
|
||||
rescue Errno::ECONNREFUSED => e
|
||||
raise "*** osrm-routed is not running."
|
||||
rescue Timeout::Error
|
||||
raise "*** osrm-routed did not respond."
|
||||
uri = generate_request_url ("match" + '?' + params)
|
||||
response = send_request uri, trace, timestamps, options
|
||||
end
|
||||
|
||||
|
||||
@@ -1,32 +1,12 @@
|
||||
require 'net/http'
|
||||
|
||||
def request_nearest_url path, method={}
|
||||
def request_nearest_url path
|
||||
@query = path
|
||||
|
||||
if method.has_key?("post")
|
||||
request_method = "POST"
|
||||
else
|
||||
request_method = "GET"
|
||||
end
|
||||
if request_method.eql? "GET"
|
||||
uri = URI.parse "#{HOST}/#{path}"
|
||||
elsif request_method.eql? "POST"
|
||||
uri = URI.parse "#{HOST}/nearest"
|
||||
end
|
||||
Timeout.timeout(OSRM_TIMEOUT) do
|
||||
if request_method.eql? "GET"
|
||||
Net::HTTP.get_response uri
|
||||
elsif request_method.eql? "POST"
|
||||
path.slice!(0, 12)
|
||||
Net::HTTP.post_form uri, "loc" => path
|
||||
end
|
||||
end
|
||||
rescue Errno::ECONNREFUSED => e
|
||||
raise "*** osrm-routed is not running."
|
||||
rescue Timeout::Error
|
||||
raise "*** osrm-routed did not respond."
|
||||
uri = generate_request_url path
|
||||
response = send_simple_request uri, path
|
||||
end
|
||||
|
||||
def request_nearest a, method
|
||||
request_nearest_url "nearest?loc=#{a}", method
|
||||
def request_nearest a
|
||||
request_nearest_url "nearest?loc=#{a}"
|
||||
end
|
||||
|
||||
@@ -16,32 +16,12 @@ def request_path path, waypoints=[], options={}
|
||||
params = (locs + options.to_param).join('&')
|
||||
params = nil if params==""
|
||||
|
||||
if options.has_key?("post")
|
||||
request_method = "POST"
|
||||
options.delete("post")
|
||||
if params == nil
|
||||
uri = generate_request_url (path)
|
||||
else
|
||||
request_method = "GET"
|
||||
uri = generate_request_url (path + '?' + params)
|
||||
end
|
||||
if request_method.eql? "GET"
|
||||
uri = URI.parse ["#{HOST}/#{path}", params].compact.join('?')
|
||||
elsif request_method.eql? "POST"
|
||||
uri = URI.parse "#{HOST}/#{path}"
|
||||
end
|
||||
@query = uri.to_s
|
||||
Timeout.timeout(OSRM_TIMEOUT) do
|
||||
if request_method.eql? "GET"
|
||||
Net::HTTP.get_response uri
|
||||
elsif request_method.eql? "POST"
|
||||
datas = {}
|
||||
datas[:loc] = waypoints.compact.map { |w| "#{w.lat},#{w.lon}" }
|
||||
datas.merge! options
|
||||
Net::HTTP.post_form uri, datas
|
||||
end
|
||||
end
|
||||
rescue Errno::ECONNREFUSED => e
|
||||
raise "*** osrm-routed is not running."
|
||||
rescue Timeout::Error
|
||||
raise "*** osrm-routed did not respond."
|
||||
response = send_request uri, waypoints, {}, options
|
||||
end
|
||||
|
||||
def request_url path
|
||||
|
||||
Reference in New Issue
Block a user