Class: Osrmclient
- Inherits:
-
Object
- Object
- Osrmclient
- Defined in:
- lib/osrmclient.rb,
lib/osrmclient/version.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Osrmclient
constructor
A new instance of Osrmclient.
- #locate(options = {}) ⇒ Object
- #nearest(options = {}) ⇒ Object
- #table(options = {}) ⇒ Object
- #viaroute(options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Osrmclient
Returns a new instance of Osrmclient.
7 8 9 10 11 12 13 14 15 |
# File 'lib/osrmclient.rb', line 7 def initialize( = {}) server = [:server] || "localhost" port = [:port] || 80 Faraday::Utils.default_params_encoder = Faraday::FlatParamsEncoder @connection = Faraday.new(url: "#{server}:#{port}") do |f| f.adapter :typhoeus end end |
Instance Method Details
#locate(options = {}) ⇒ Object
17 18 19 20 21 |
# File 'lib/osrmclient.rb', line 17 def locate( = {}) lat = [:lat] || 0 lon = [:lon] || 0 @connection.get("/locate", {loc: "#{lat},#{lon}"}) end |
#nearest(options = {}) ⇒ Object
24 25 26 27 28 |
# File 'lib/osrmclient.rb', line 24 def nearest( = {}) lat = [:lat] || 0 lon = [:lon] || 0 @connection.get("/nearest", {loc: "#{lat},#{lon}"}) end |
#table(options = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/osrmclient.rb', line 50 def table( = {}) url = "/table?" ops = .dup locations = ops.delete(:locations) unless locations.nil? locations.each_slice(2) do |lat, lon| url << "&loc=#{lat[1]},#{lon[1]}" end end unless .nil? ops.each_pair do |key, value| url << "&#{key}=#{value}" end end @connection.get(url) end |
#viaroute(options = {}) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/osrmclient.rb', line 31 def viaroute( = {}) ops = .dup lat1 = ops.delete(:lat1) || 0 lon1 = ops.delete(:lon1) || 0 lat2 = ops.delete(:lat2) || 0 lon2 = ops.delete(:lon2) || 0 url = "/viaroute?" url << "loc=#{lat1},#{lon1}&loc=#{lat2},#{lon2}" unless ops.nil? ops.each_pair do |key, value| url << "&#{key}=#{value}" end end @connection.get(url) end |