From 34c12a9536fce311d5f670eeddf5e115531d7ffa Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Fri, 1 Nov 2024 20:49:59 +0100 Subject: [PATCH] Add GET /api/v1/countries/borders endpoint to get countries for scratch map feature --- .app_version | 2 +- CHANGELOG.md | 6 ++++++ .../api/v1/countries/borders_controller.rb | 11 +++++++++++ app/javascript/controllers/maps_controller.js | 2 +- config/routes.rb | 4 ++++ .../assets/countries.json | 0 spec/requests/api/v1/countries/borders_spec.rb | 15 +++++++++++++++ 7 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 app/controllers/api/v1/countries/borders_controller.rb rename public/countries.geojson => lib/assets/countries.json (100%) create mode 100644 spec/requests/api/v1/countries/borders_spec.rb diff --git a/.app_version b/.app_version index 8ddf20e4..18eaa035 100644 --- a/.app_version +++ b/.app_version @@ -1 +1 @@ -0.15.12 +0.15.13 diff --git a/CHANGELOG.md b/CHANGELOG.md index f2a3b8a7..1fde85df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +# 0.15.13 - 2024-11-01 + +### Added + +- `GET /api/v1/countries/borders` endpoint to get countries for scratch map feature + # 0.15.12 - 2024-11-01 ### Added diff --git a/app/controllers/api/v1/countries/borders_controller.rb b/app/controllers/api/v1/countries/borders_controller.rb new file mode 100644 index 00000000..9d880528 --- /dev/null +++ b/app/controllers/api/v1/countries/borders_controller.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class Api::V1::Countries::BordersController < ApplicationController + def index + countries = Rails.cache.fetch('dawarich/countries_codes', expires_in: 1.day) do + Oj.load(File.read(Rails.root.join('lib/assets/countries.json'))) + end + + render json: countries + end +end diff --git a/app/javascript/controllers/maps_controller.js b/app/javascript/controllers/maps_controller.js index ef3b9175..8a3f31b1 100644 --- a/app/javascript/controllers/maps_controller.js +++ b/app/javascript/controllers/maps_controller.js @@ -157,7 +157,7 @@ export default class extends Controller { try { // Up-to-date version can be found on Github: // https://raw.githubusercontent.com/datasets/geo-countries/master/data/countries.geojson - const response = await fetch('/countries.geojson', { + const response = await fetch('/api/v1/countries/borders.json', { headers: { 'Accept': 'application/geo+json,application/json' } diff --git a/config/routes.rb b/config/routes.rb index 8eef1d85..16d7fdb2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -72,6 +72,10 @@ Rails.application.routes.draw do namespace :owntracks do resources :points, only: :create end + + namespace :countries do + resources :borders, only: :index + end end end end diff --git a/public/countries.geojson b/lib/assets/countries.json similarity index 100% rename from public/countries.geojson rename to lib/assets/countries.json diff --git a/spec/requests/api/v1/countries/borders_spec.rb b/spec/requests/api/v1/countries/borders_spec.rb new file mode 100644 index 00000000..1162e198 --- /dev/null +++ b/spec/requests/api/v1/countries/borders_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe 'Api::V1::Countries::Borders', type: :request do + describe 'GET /index' do + it 'returns a list of countries with borders' do + get '/api/v1/countries/borders' + + expect(response).to have_http_status(:success) + expect(response.body).to include('AF') + expect(response.body).to include('ZW') + end + end +end