From 11d39964d31ddd79e1ca9c7eb9364219165a4fee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Biegun?= <69080338+Anakin100100@users.noreply.github.com> Date: Sun, 3 Apr 2022 11:54:20 +0200 Subject: [PATCH] Add new fields from gdynia api (#38) --- Gemfile.lock | 6 +-- app/clients/gdynia_extra_data_client.rb | 10 +++- app/controllers/institutions_controller.rb | 6 +-- app/services/gdynia_extra_data_mapper.rb | 13 +++++- ..._new_fields_from_gdynia_to_institutions.rb | 9 ++++ db/schema.rb | 7 ++- docs/api.md | 12 +++++ spec/clients/gdynia_extra_data_client_spec.rb | 46 ++++++++++++++++++- spec/requests/get_all_institutions_spec.rb | 16 +++---- .../services/gdynia_extra_data_mapper_spec.rb | 25 +++++++++- 10 files changed, 128 insertions(+), 22 deletions(-) create mode 100644 db/migrate/20220331043816_add_new_fields_from_gdynia_to_institutions.rb diff --git a/Gemfile.lock b/Gemfile.lock index ef61328..212a0ba 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -77,7 +77,7 @@ GEM crack (0.4.5) rexml crass (1.0.6) - debug (1.4.0) + debug (1.5.0) irb (>= 1.3.6) reline (>= 0.2.7) devise (4.8.1) @@ -157,7 +157,7 @@ GEM nokogiri (1.13.3-x86_64-linux) racc (~> 1.4) orm_adapter (0.5.0) - parallel (1.22.0) + parallel (1.22.1) parser (3.1.1.0) ast (~> 2.4.1) pg (1.3.4) @@ -165,7 +165,7 @@ GEM activerecord (>= 5.2) activesupport (>= 5.2) public_suffix (4.0.6) - puma (5.6.2) + puma (5.6.4) nio4r (~> 2.0) raabro (1.4.0) racc (1.6.0) diff --git a/app/clients/gdynia_extra_data_client.rb b/app/clients/gdynia_extra_data_client.rb index f05e9c2..2e4206b 100644 --- a/app/clients/gdynia_extra_data_client.rb +++ b/app/clients/gdynia_extra_data_client.rb @@ -12,14 +12,20 @@ def raw_schools raw_data = JSON.parse(response.body) raw_data.map do |raw_record| - # We are interested in the following fields: "w51", "wx2", "wx3", "w68", "w88" + # We are intersted in the following fields: "w51", "wx2", "wx3", "w68", "w88", + # "opis_szkoły", "sport", "jezyki_obce", "profile_klas", "zajecia_dodatkowe" { 'rspo' => raw_record.fetch('rspo').to_i, 'w51' => raw_record.fetch('w51').to_i, 'wx2' => raw_record.fetch('wx2').to_i, 'wx3' => raw_record.fetch('wx3').to_i, 'w68' => raw_record.fetch('w68').to_f, - 'w88' => raw_record.fetch('w88').to_f + 'w88' => raw_record.fetch('w88').to_f, + 'opis_szkoly' => raw_record.fetch('opis_szkoly'), + 'sport' => raw_record.fetch('sport'), + 'jezyki_obce' => raw_record.fetch('jezyki_obce'), + 'profile_klas' => raw_record.fetch('profile_klas'), + 'zajecia_dodatkowe' => raw_record.fetch('zajecia_dodatkowe') } end end diff --git a/app/controllers/institutions_controller.rb b/app/controllers/institutions_controller.rb index bc2e372..4c6742d 100644 --- a/app/controllers/institutions_controller.rb +++ b/app/controllers/institutions_controller.rb @@ -15,9 +15,9 @@ def index @paginated_institutions = institutions.paginate(page: @page, per_page: @page_size) render status: '200', json: { - "results": @paginated_institutions, - "page": @page, - "totalItems": @paginated_institutions.size, + results: @paginated_institutions, + page: @page, + totalItems: Institution.count } end diff --git a/app/services/gdynia_extra_data_mapper.rb b/app/services/gdynia_extra_data_mapper.rb index 96ad291..221bc93 100644 --- a/app/services/gdynia_extra_data_mapper.rb +++ b/app/services/gdynia_extra_data_mapper.rb @@ -11,8 +11,14 @@ # "wx3","Oddziały sportowe" # "w68","Zmianowość" # "w88","Liczba uczniów na nauczyciela" +# "opis_szkoły","Opis szkoły" +# "sport", "Zajęcia sportowe oferowane przez szkołę" +# "jezyki_obce", "Zajęcia językowe oferowane przez szkołę" +# "profile_klas", "Profile klas które są oferowane przez daną szkołę" +# "zajecia_dodatkowe", "Zajęcia dodatkowe oferowane przez szkołę" -# We are intersted in the following fields: "w51", "wx2", "wx3", "w68", "w88" +# We are intersted in the following fields: "w51", "wx2", "wx3", "w68", "w88", +# "opis_szkoły", "sport", "jezyki_obce", "profile_klas", "zajecia_dodatkowe" class GdyniaExtraDataMapper < ApplicationService def call(raw_data) raw_data.each do |raw_school| @@ -29,6 +35,11 @@ def call(raw_data) rspo_match[:sport_facilities] = raw_school.fetch('wx3') rspo_match[:working_time] = raw_school.fetch('w68') rspo_match[:students_per_teacher] = raw_school.fetch('w88') + rspo_match[:description] = raw_school.fetch('opis_szkoly') + rspo_match[:sports] = raw_school.fetch('sport').join(',') + rspo_match[:foreign_languages] = raw_school.fetch('jezyki_obce').join(',') + rspo_match[:class_profiles] = raw_school.fetch('profile_klas').join(',') + rspo_match[:extracurricular_activities] = raw_school.fetch('zajecia_dodatkowe').join(',') rspo_match.save end diff --git a/db/migrate/20220331043816_add_new_fields_from_gdynia_to_institutions.rb b/db/migrate/20220331043816_add_new_fields_from_gdynia_to_institutions.rb new file mode 100644 index 0000000..de2988d --- /dev/null +++ b/db/migrate/20220331043816_add_new_fields_from_gdynia_to_institutions.rb @@ -0,0 +1,9 @@ +class AddNewFieldsFromGdyniaToInstitutions < ActiveRecord::Migration[7.0] + def change + add_column :institutions, :description, :string + add_column :institutions, :sports, :string + add_column :institutions, :foreign_languages, :string + add_column :institutions, :class_profiles, :string + add_column :institutions, :extracurricular_activities, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index e926a29..e8c8822 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2022_02_12_134157) do +ActiveRecord::Schema[7.0].define(version: 2022_03_31_043816) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -76,6 +76,11 @@ t.string "county", null: false t.string "municipality", null: false t.string "town", null: false + t.string "description" + t.string "sports" + t.string "foreign_languages" + t.string "class_profiles" + t.string "extracurricular_activities" t.index ["institution_type_id"], name: "index_institutions_on_institution_type_id" end diff --git a/docs/api.md b/docs/api.md index 2fcbe17..009ac74 100644 --- a/docs/api.md +++ b/docs/api.md @@ -32,3 +32,15 @@ paramenters: id:int, required it specifies the internal id of the institution to be shown this is not the RSPO id of the institution + + +### Institution model + +in the institution model the data that comes from Gdyania API as a list is joined using a comma. for example + +"zajecia_dodatkowe": [ + "klub turystyczny", + "sekcja karate" + ] + +institution_from_gdynia.exatracurricular_activities will return "klub turystyczny,sekcja karate" diff --git a/spec/clients/gdynia_extra_data_client_spec.rb b/spec/clients/gdynia_extra_data_client_spec.rb index 6385bd6..33c10f3 100644 --- a/spec/clients/gdynia_extra_data_client_spec.rb +++ b/spec/clients/gdynia_extra_data_client_spec.rb @@ -2,6 +2,38 @@ require 'rails_helper' +# Field info +# kod,nazwa,komentarz,opis,ikona,jednostka,okres,ostatnia_aktualizacja +# "w92","Liczba etatów przypadających na jednego nauczyciela" +# "w110","Nauczyciele wspomagający" +# "w161","Uczniowie pochodzący spoza Gdyni" +# "w51","Klasy integracyjne" +# "wx1","Aktualny dzienny koszt oświaty" +# "wx2","Liczba sal lekcyjnych" +# "wx3","Oddziały sportowe" +# "w68","Zmianowość" +# "w88","Liczba uczniów na nauczyciela" +# "opis_szkoły","Opis szkoły" +# "sport", "Zajęcia sportowe oferowane przez szkołę" +# "jezyki_obce", "Zajęcia językowe oferowane przez szkołę" +# "profile_klas", "Profile klas które są oferowane przez daną szkołę" +# "zajecia_dodatkowe", "Zajęcia dodatkowe oferowane przez szkołę" + +# We are intersted in the following fields: "w51", "wx2", "wx3", "w68", "w88", +# "opis_szkoły", "sport", "jezyki_obce", "profile_klas", "zajecia_dodatkowe" + +# Database fields: +# add_column :institutions, :integration_classes, :integer +# add_column :institutions, :classrooms, :integer +# add_column :institutions, :sport_facilities, :integer +# add_column :institutions, :working_time, :integer +# add_column :institutions, :studerns_per_teacher, :integer +# add_column :institutions, :description, :string +# add_column :institutions, :sports, :string +# add_column :institutions, :foreign_languages, :string +# add_column :institutions, :class_profiles, :string +# add_column :institutions, :extracurricular_activities, :string + RSpec.describe 'GdyniaExtraDataClient' do describe '#raw_schools' do # We are disabling webmock because we want to connect to the real api @@ -24,7 +56,12 @@ 'wx3' => 30, 'w68' => 40, 'w88' => 50, - 'rspo' => institution_1.rspo_institution_id + 'rspo' => institution_1.rspo_institution_id, + 'opis_szkoly' => 'Opis szkoły', + 'sport' => ['koszykówka', 'siatkówka'], + 'jezyki_obce' => ['angielski', 'niemiecki'], + 'profile_klas' => ['matematyczny', 'fizyczny'], + 'zajecia_dodatkowe' => ['chór', 'matematyka'] }, { 'w51' => 101, @@ -32,7 +69,12 @@ 'wx3' => 301, 'w68' => 401, 'w88' => 501, - 'rspo' => institution_2.rspo_institution_id + 'rspo' => institution_2.rspo_institution_id, + 'opis_szkoly' => 'Opis szkoły', + 'sport' => ['koszykówka', 'siatkówka'], + 'jezyki_obce' => ['angielski', 'niemiecki'], + 'profile_klas' => ['matematyczny', 'fizyczny'], + 'zajecia_dodatkowe' => ['chór', 'matematyka'] } ] diff --git a/spec/requests/get_all_institutions_spec.rb b/spec/requests/get_all_institutions_spec.rb index 0652d71..90adacf 100644 --- a/spec/requests/get_all_institutions_spec.rb +++ b/spec/requests/get_all_institutions_spec.rb @@ -13,14 +13,14 @@ it 'returns 10 institutions when no page_size is specified' do get '/institutions' - expect(JSON.parse(response.body)["results"].size).to equal(10) + expect(JSON.parse(response.body)['results'].size).to equal(10) end it 'returns the correct number of institutions when page_size is specified' do (1..20).each do |page_size| get '/institutions', params: { page_size: page_size } - expect(JSON.parse(response.body)["results"].size).to equal(page_size) - expect(JSON.parse(response.body)["totalItems"]).to equal(page_size) + expect(JSON.parse(response.body)['results'].size).to equal(page_size) + expect(JSON.parse(response.body)['totalItems']).to equal(Institution.count) end end @@ -35,14 +35,14 @@ end it "returns empty array when there aren't enough institutions" do - get '/institutions', params: { page: 99999999 } - expect(JSON.parse(response.body)["results"].size).to equal(0) - expect(JSON.parse(response.body)["page"]).to equal(99999999) + get '/institutions', params: { page: 99_999_999 } + expect(JSON.parse(response.body)['results'].size).to equal(0) + expect(JSON.parse(response.body)['page']).to equal(99_999_999) end - it "returns the correct page when page is specified" do + it 'returns the correct page when page is specified' do get '/institutions', params: { page: 15 } - expect(JSON.parse(response.body)["page"]).to equal(15) + expect(JSON.parse(response.body)['page']).to equal(15) end end end diff --git a/spec/services/gdynia_extra_data_mapper_spec.rb b/spec/services/gdynia_extra_data_mapper_spec.rb index 8298fe8..fccb699 100644 --- a/spec/services/gdynia_extra_data_mapper_spec.rb +++ b/spec/services/gdynia_extra_data_mapper_spec.rb @@ -13,8 +13,14 @@ # "wx3","Oddziały sportowe" # "w68","Zmianowość" # "w88","Liczba uczniów na nauczyciela" +# "opis_szkoły","Opis szkoły" +# "sport", "Zajęcia sportowe oferowane przez szkołę" +# "jezyki_obce", "Zajęcia językowe oferowane przez szkołę" +# "profile_klas", "Profile klas które są oferowane przez daną szkołę" +# "zajecia_dodatkowe", "Zajęcia dodatkowe oferowane przez szkołę" -# We are intersted in the following fields: "w51", "wx2", "wx3", "w68", "w88" +# We are intersted in the following fields: "w51", "wx2", "wx3", "w68", "w88", +# "opis_szkoły", "sport", "jezyki_obce", "profile_klas", "zajecia_dodatkowe" # Database fields: # add_column :institutions, :integration_classes, :integer @@ -22,6 +28,11 @@ # add_column :institutions, :sport_facilities, :integer # add_column :institutions, :working_time, :integer # add_column :institutions, :studerns_per_teacher, :integer +# add_column :institutions, :description, :string +# add_column :institutions, :sports, :string +# add_column :institutions, :foreign_languages, :string +# add_column :institutions, :class_profiles, :string +# add_column :institutions, :extracurricular_activities, :string RSpec.describe 'GdyniaExtraDataMapper', type: :service do describe '#call' do @@ -35,7 +46,12 @@ 'wx3' => 30, 'w68' => 40, 'w88' => 50, - 'rspo' => institution_created.rspo_institution_id + 'rspo' => institution_created.rspo_institution_id, + 'opis_szkoly' => 'Opis szkoły', + 'sport' => ['koszykówka', 'piłka nożna'], + 'jezyki_obce' => %w[angielski niemiecki], + 'profile_klas' => %w[matematyczny fizyczny], + 'zajecia_dodatkowe' => ['klub turystyczny', 'matematyka'] } ] GdyniaExtraDataMapper.new.call(mock_gdynia_api_data) @@ -45,6 +61,11 @@ expect(institution_from_database.sport_facilities).to eq(30) expect(institution_from_database.working_time).to eq(40) expect(institution_from_database.students_per_teacher).to eq(50) + expect(institution_from_database.description).to eq('Opis szkoły') + expect(institution_from_database.sports).to eq('koszykówka,piłka nożna') + expect(institution_from_database.foreign_languages).to eq('angielski,niemiecki') + expect(institution_from_database.class_profiles).to eq('matematyczny,fizyczny') + expect(institution_from_database.extracurricular_activities).to eq('klub turystyczny,matematyka') end end end