From 0afdbdfe26b911b16e310be9a1251b9ba97db7df Mon Sep 17 00:00:00 2001 From: Laura Mosher Date: Fri, 12 Apr 2024 11:57:28 -0400 Subject: [PATCH] feat(api): Include protomaps config This adds the protomaps API key (when available) and the configured basemaps style name to the Public Community endpoints for the Explore interface to consume and use when available. --- .../views/api/communities/index.json.jbuilder | 6 +++- .../views/api/communities/show.json.jbuilder | 3 ++ .../requests/api/public_communities_spec.rb | 28 +++++++++++++++++++ .../requests/api/public_community_spec.rb | 2 ++ rails/spec/support/active_storage.rb | 6 +++- 5 files changed, 43 insertions(+), 2 deletions(-) diff --git a/rails/app/views/api/communities/index.json.jbuilder b/rails/app/views/api/communities/index.json.jbuilder index 2d4fde710..7c3cefb1e 100644 --- a/rails/app/views/api/communities/index.json.jbuilder +++ b/rails/app/views/api/communities/index.json.jbuilder @@ -9,7 +9,8 @@ envelope(json) do json.displayImage rails_blob_url(community.display_image) end - if community.theme.static_map.attached? + # only send static map image if mapbox is configured + if !community.theme.use_maplibre? && community.theme.static_map.attached? json.staticMapUrl rails_blob_url(community.theme.static_map) end @@ -19,6 +20,9 @@ envelope(json) do json.mapbox3dEnabled community.theme.mapbox_3d json.mapProjection community.theme.map_projection + json.pmApiKey community.theme.protomaps_api_key + json.pmBasemapStyle community.theme.basemap_style + json.center community.theme.center json.zoom community.theme.zoom diff --git a/rails/app/views/api/communities/show.json.jbuilder b/rails/app/views/api/communities/show.json.jbuilder index 4c33ae419..88ff935a4 100644 --- a/rails/app/views/api/communities/show.json.jbuilder +++ b/rails/app/views/api/communities/show.json.jbuilder @@ -30,6 +30,9 @@ envelope(json) do json.mapbox3dEnabled @community.theme.mapbox_3d json.mapProjection @community.theme.map_projection + json.pmApiKey @community.theme.protomaps_api_key + json.pmBasemapStyle @community.theme.basemap_style + json.centerLat @community.theme.center_lat json.centerLong @community.theme.center_long json.swBoundaryLat @community.theme.sw_boundary_lat diff --git a/rails/spec/requests/api/public_communities_spec.rb b/rails/spec/requests/api/public_communities_spec.rb index e49979d65..804c01f87 100644 --- a/rails/spec/requests/api/public_communities_spec.rb +++ b/rails/spec/requests/api/public_communities_spec.rb @@ -24,6 +24,34 @@ expect(json_response.first.keys).to include("displayImage") end + context "static map image" do + context "when mapbox is configured" do + before { allow(Map).to receive(:use_mapbox?).and_return(true) } + + it "returns static map image url" do + public_community.theme.static_map.attach(io: file_fixture("../media/terrastories.png").open, filename: "static_map.png") + + get "/api/communities" + expect(json_response.first.keys).to include("staticMapUrl") + expect(json_response.first["staticMapUrl"]).to eq(blob_url(public_community.theme.static_map)) + end + + it "returns nil if no static map" do + get "/api/communities" + expect(json_response.first.keys).not_to include("staticMapUrl") + end + end + + context "when maplibre is configured" do + it "returns nil, even if static map had previously been attached" do + public_community.theme.static_map.attach(io: file_fixture("../media/terrastories.png").open, filename: "static_map.png") + + get "/api/communities" + expect(json_response.first.keys).not_to include("staticMapUrl") + end + end + end + context "with search" do it "can be filtered with case insensitive query" do get "/api/communities", params: {search: "cool"} diff --git a/rails/spec/requests/api/public_community_spec.rb b/rails/spec/requests/api/public_community_spec.rb index 0c9559a89..63ac4640e 100644 --- a/rails/spec/requests/api/public_community_spec.rb +++ b/rails/spec/requests/api/public_community_spec.rb @@ -51,6 +51,8 @@ "mapboxStyle", "mapbox3dEnabled", "mapProjection", + "pmApiKey", + "pmBasemapStyle", "centerLat", "centerLong", "swBoundaryLat", diff --git a/rails/spec/support/active_storage.rb b/rails/spec/support/active_storage.rb index a7ab4cb2a..816ba9282 100644 --- a/rails/spec/support/active_storage.rb +++ b/rails/spec/support/active_storage.rb @@ -2,4 +2,8 @@ config.after(:suite) do FileUtils.rm_rf(ActiveStorage::Blob.service.root) end -end \ No newline at end of file +end + +def blob_url(blob, host = "www.example.com") + Rails.application.routes.url_helpers.rails_blob_url(blob, host: host) +end