From b54333fa6ddf9f6ede89e4da345a2ae93703ee7d Mon Sep 17 00:00:00 2001 From: mmd-osm Date: Thu, 2 Jan 2020 14:37:27 +0100 Subject: [PATCH] Unify lat/lon formatting for json output --- app/models/concerns/geo_record.rb | 4 ++++ app/views/api/map/_bounds.json.jbuilder | 8 ++++---- app/views/api/nodes/_node.json.jbuilder | 4 ++-- app/views/api/old_nodes/_old_node.json.jbuilder | 4 ++-- test/controllers/api/map_controller_test.rb | 12 ++++++------ 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/app/models/concerns/geo_record.rb b/app/models/concerns/geo_record.rb index 91533ece4..447ee19df 100644 --- a/app/models/concerns/geo_record.rb +++ b/app/models/concerns/geo_record.rb @@ -12,6 +12,10 @@ module GeoRecord def to_s format("%.7f", self) end + + def as_json(_) + format("%.7f", self).to_f + end end # This scaling factor is used to convert between the float lat/lon that is diff --git a/app/views/api/map/_bounds.json.jbuilder b/app/views/api/map/_bounds.json.jbuilder index 98755900e..16fdbeb0f 100644 --- a/app/views/api/map/_bounds.json.jbuilder +++ b/app/views/api/map/_bounds.json.jbuilder @@ -1,6 +1,6 @@ json.bounds do - json.minlat format("%.7f", @bounds.min_lat) - json.minlon format("%.7f", @bounds.min_lon) - json.maxlat format("%.7f", @bounds.max_lat) - json.maxlon format("%.7f", @bounds.max_lon) + json.minlat GeoRecord::Coord.new(@bounds.min_lat) + json.minlon GeoRecord::Coord.new(@bounds.min_lon) + json.maxlat GeoRecord::Coord.new(@bounds.max_lat) + json.maxlon GeoRecord::Coord.new(@bounds.max_lon) end diff --git a/app/views/api/nodes/_node.json.jbuilder b/app/views/api/nodes/_node.json.jbuilder index 12cf2fb95..e48d5f17c 100644 --- a/app/views/api/nodes/_node.json.jbuilder +++ b/app/views/api/nodes/_node.json.jbuilder @@ -1,8 +1,8 @@ json.type "node" json.id node.id if node.visible - json.lat format("%.7f", node.lat.to_f) - json.lon format("%.7f", node.lon.to_f) + json.lat GeoRecord::Coord.new(node.lat) + json.lon GeoRecord::Coord.new(node.lon) end json.timestamp node.timestamp.xmlschema json.version node.version diff --git a/app/views/api/old_nodes/_old_node.json.jbuilder b/app/views/api/old_nodes/_old_node.json.jbuilder index 5042c84a7..211d50332 100644 --- a/app/views/api/old_nodes/_old_node.json.jbuilder +++ b/app/views/api/old_nodes/_old_node.json.jbuilder @@ -1,8 +1,8 @@ json.type "node" json.id old_node.node_id if old_node.visible - json.lat format("%.7f", old_node.lat.to_f) - json.lon format("%.7f", old_node.lon.to_f) + json.lat GeoRecord::Coord.new(old_node.lat) + json.lon GeoRecord::Coord.new(old_node.lon) end json.timestamp old_node.timestamp.xmlschema json.version old_node.version diff --git a/test/controllers/api/map_controller_test.rb b/test/controllers/api/map_controller_test.rb index 5df2dc6a3..54461868b 100644 --- a/test/controllers/api/map_controller_test.rb +++ b/test/controllers/api/map_controller_test.rb @@ -92,15 +92,15 @@ module Api assert_equal Settings.api_version, js["version"] assert_equal Settings.generator, js["generator"] - assert_equal "#{format('%.7f', minlon)}", js["bounds"]["minlon"] - assert_equal "#{format('%.7f', minlat)}", js["bounds"]["minlat"] - assert_equal "#{format('%.7f', maxlon)}", js["bounds"]["maxlon"] - assert_equal "#{format('%.7f', maxlat)}", js["bounds"]["maxlat"] + assert_equal GeoRecord::Coord.new(minlon), js["bounds"]["minlon"] + assert_equal GeoRecord::Coord.new(minlat), js["bounds"]["minlat"] + assert_equal GeoRecord::Coord.new(maxlon), js["bounds"]["maxlon"] + assert_equal GeoRecord::Coord.new(maxlat), js["bounds"]["maxlat"] result_nodes = js["elements"].select { |a| a["type"] == "node" } .select { |a| a["id"] == node.id } - .select { |a| a["lat"] == "#{format('%.7f', node.lat)}" } - .select { |a| a["lon"] == "#{format('%.7f', node.lon)}" } + .select { |a| a["lat"] == GeoRecord::Coord.new(node.lat) } + .select { |a| a["lon"] == GeoRecord::Coord.new(node.lon) } .select { |a| a["version"] == node.version } .select { |a| a["changeset"] == node.changeset_id } .select { |a| a["timestamp"] == node.timestamp.xmlschema } -- 2.39.5