From ee65dcfba30019bb43c71f5925f87a7fbcf2c4cb Mon Sep 17 00:00:00 2001 From: Tobias Matz Date: Tue, 2 Jul 2024 09:54:10 +0200 Subject: [PATCH 1/9] Add zone endpoints --- lib/ioki/apis/operator_api.rb | 7 +++- spec/ioki/operator_api_spec.rb | 67 ++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/lib/ioki/apis/operator_api.rb b/lib/ioki/apis/operator_api.rb index 8f9b4c37..bf83345f 100644 --- a/lib/ioki/apis/operator_api.rb +++ b/lib/ioki/apis/operator_api.rb @@ -308,7 +308,12 @@ class OperatorApi :fleet_state, base_path: [API_BASE_PATH], model_class: Ioki::Model::Operator::FleetState - ) + ), + Endpoints.crud_endpoints( + :zone, + base_path: [API_BASE_PATH, 'products', :id], + model_class: Ioki::Model::Operator::Zone + ), ].freeze end end diff --git a/spec/ioki/operator_api_spec.rb b/spec/ioki/operator_api_spec.rb index 7d2e2ae7..6abf9b50 100644 --- a/spec/ioki/operator_api_spec.rb +++ b/spec/ioki/operator_api_spec.rb @@ -1542,4 +1542,71 @@ expect(operator_client.fleet_state(options)).to be_a Ioki::Model::Operator::FleetState end end + + describe '#zones(product_id)' do + it 'calls request on the client with expected params' do + expect(operator_client).to receive(:request) do |params| + expect(params[:url].to_s).to eq('operator/products/0815/zones') + result_with_index_data + end + + expect(operator_client.zones('0815', options)) + .to all(be_a(Ioki::Model::Operator::Zone)) + end + end + + describe '#zone(product_id, zone_id)' do + it 'calls request on the client with expected params' do + expect(operator_client).to receive(:request) do |params| + expect(params[:url].to_s).to eq('operator/products/0815/zones/4711') + [result_with_data, full_response] + end + + expect(operator_client.zone('0815', '4711', options)) + .to be_a(Ioki::Model::Operator::Zone) + end + end + + describe '#create_zone(product_id, zone)' do + let(:zone) { Ioki::Model::Operator::Zone.new({ id: '4711' }) } + + it 'calls request on the client with expected params' do + expect(operator_client).to receive(:request) do |params| + expect(params[:url].to_s).to eq('operator/products/0815/zones') + expect(params[:method]).to eq(:post) + [result_with_data, full_response] + end + + expect(operator_client.create_zone('0815', zone, options)) + .to be_a(Ioki::Model::Operator::Zone) + end + end + + describe '#update_zone(product_id, zone)' do + let(:zone) { Ioki::Model::Operator::Zone.new({ id: '4711' }) } + + it 'calls request on the client with expected params' do + expect(operator_client).to receive(:request) do |params| + expect(params[:url].to_s).to eq('operator/products/0815/zones/4711') + expect(params[:method]).to eq(:patch) + [result_with_data, full_response] + end + + expect(operator_client.update_zone('0815', zone, options)) + .to be_a(Ioki::Model::Operator::Zone) + end + end + + describe '#delete_zone(product_id, zone_id)' do + it 'calls request on the client with expected params' do + expect(operator_client).to receive(:request) do |params| + expect(params[:url].to_s).to eq('operator/products/0815/zones/4711') + expect(params[:method]).to eq(:delete) + result_with_data + end + + expect(operator_client.delete_zone('0815', '4711', options)) + .to be_a(Ioki::Model::Operator::Zone) + end + end end From 0c9605a995b0d0df2968a5bb9219c96f59896b44 Mon Sep 17 00:00:00 2001 From: Tobias Matz Date: Tue, 2 Jul 2024 09:54:29 +0200 Subject: [PATCH 2/9] Add Zone model --- lib/ioki/model/operator/zone.rb | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 lib/ioki/model/operator/zone.rb diff --git a/lib/ioki/model/operator/zone.rb b/lib/ioki/model/operator/zone.rb new file mode 100644 index 00000000..67b5d46b --- /dev/null +++ b/lib/ioki/model/operator/zone.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +module Ioki + module Model + module Operator + class Zone < Base + attribute :type, + on: :read, + type: :string + + attribute :id, + on: :read, + type: :string + + attribute :created_at, + on: :read, + type: :date_time + + attribute :updated_at, + on: :read, + type: :date_time + + attribute :area, + on: :read, + type: :object, + class_name: 'Geojson' + + attribute :area_geojson, + on: [:create, :update], + type: :string + + attribute :inverted_area, + on: :read, + type: :object, + class_name: 'Geojson' + + attribute :name, + on: [:create, :read, :update], + omit_if_nil_on: [:create, :update], + type: :string + + attribute :slug, + on: [:create, :read, :update], + omit_if_nil_on: [:create, :update], + type: :string + end + end + end +end From c969cbb6e10d8e530ad3ad5d220b313090307731 Mon Sep 17 00:00:00 2001 From: Tobias Matz Date: Tue, 2 Jul 2024 09:55:02 +0200 Subject: [PATCH 3/9] Fix linting issues --- lib/ioki/apis/operator_api.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ioki/apis/operator_api.rb b/lib/ioki/apis/operator_api.rb index bf83345f..083f5983 100644 --- a/lib/ioki/apis/operator_api.rb +++ b/lib/ioki/apis/operator_api.rb @@ -313,7 +313,7 @@ class OperatorApi :zone, base_path: [API_BASE_PATH, 'products', :id], model_class: Ioki::Model::Operator::Zone - ), + ) ].freeze end end From 6d1a9c08b1dc738b500903788f27be6b81b78fdb Mon Sep 17 00:00:00 2001 From: Tobias Matz Date: Tue, 2 Jul 2024 10:38:29 +0200 Subject: [PATCH 4/9] Remove deprecated attributes --- lib/ioki/model/operator/zone.rb | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/lib/ioki/model/operator/zone.rb b/lib/ioki/model/operator/zone.rb index 67b5d46b..bddb785c 100644 --- a/lib/ioki/model/operator/zone.rb +++ b/lib/ioki/model/operator/zone.rb @@ -20,17 +20,8 @@ class Zone < Base on: :read, type: :date_time - attribute :area, - on: :read, - type: :object, - class_name: 'Geojson' - attribute :area_geojson, - on: [:create, :update], - type: :string - - attribute :inverted_area, - on: :read, + on: [:create, :read, :update], type: :object, class_name: 'Geojson' From 4ce70424f0e38f7e4c00fbc70d0e2cc2b434ec63 Mon Sep 17 00:00:00 2001 From: Tobias Matz Date: Wed, 3 Jul 2024 11:33:32 +0200 Subject: [PATCH 5/9] Change access rights for Geojson object --- lib/ioki/model/operator/geojson.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ioki/model/operator/geojson.rb b/lib/ioki/model/operator/geojson.rb index 9ac3a1b5..f556fee9 100644 --- a/lib/ioki/model/operator/geojson.rb +++ b/lib/ioki/model/operator/geojson.rb @@ -6,8 +6,8 @@ module Operator class Geojson < Base unvalidated true - attribute :coordinates, type: :array, on: :read - attribute :type, type: :string, on: :read + attribute :coordinates, type: :array, on: [:create, :read] + attribute :type, type: :string, on: [:create, :read] end end end From 2a127ab4a707cbd5934fd2c4c05ecfe298005f30 Mon Sep 17 00:00:00 2001 From: Tobias Matz Date: Wed, 3 Jul 2024 12:00:43 +0200 Subject: [PATCH 6/9] Adjust Geojson model --- lib/ioki/model/operator/geojson.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/ioki/model/operator/geojson.rb b/lib/ioki/model/operator/geojson.rb index f556fee9..1d804bd2 100644 --- a/lib/ioki/model/operator/geojson.rb +++ b/lib/ioki/model/operator/geojson.rb @@ -8,6 +8,15 @@ class Geojson < Base attribute :coordinates, type: :array, on: [:create, :read] attribute :type, type: :string, on: [:create, :read] + + def serialize(usecase = :read, only_changed: true) + case usecase + when :read + super + when :create + { coordinates: }.to_json + end + end end end end From 65f86635beeed13f7ffbe2c335d1d1d8f42c76f2 Mon Sep 17 00:00:00 2001 From: Tobias Matz Date: Wed, 3 Jul 2024 12:07:30 +0200 Subject: [PATCH 7/9] Add type --- lib/ioki/model/operator/geojson.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ioki/model/operator/geojson.rb b/lib/ioki/model/operator/geojson.rb index 1d804bd2..ebf8babe 100644 --- a/lib/ioki/model/operator/geojson.rb +++ b/lib/ioki/model/operator/geojson.rb @@ -14,7 +14,7 @@ def serialize(usecase = :read, only_changed: true) when :read super when :create - { coordinates: }.to_json + { coordinates:, type: }.to_json end end end From 89059a841bc02047b21fdedbff124257312b03dd Mon Sep 17 00:00:00 2001 From: Tobias Matz Date: Wed, 3 Jul 2024 14:50:01 +0200 Subject: [PATCH 8/9] Refine Geojson model --- lib/ioki/model/operator/geojson.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/ioki/model/operator/geojson.rb b/lib/ioki/model/operator/geojson.rb index ebf8babe..942a87da 100644 --- a/lib/ioki/model/operator/geojson.rb +++ b/lib/ioki/model/operator/geojson.rb @@ -6,15 +6,14 @@ module Operator class Geojson < Base unvalidated true - attribute :coordinates, type: :array, on: [:create, :read] - attribute :type, type: :string, on: [:create, :read] + attribute :coordinates, type: :array, on: [:create, :read, :update] + attribute :type, type: :string, on: [:create, :read, :update] def serialize(usecase = :read, only_changed: true) case usecase - when :read - super - when :create - { coordinates:, type: }.to_json + when :read then super + else @_raw_attributes.to_json + # else { coordinates:, type: }.to_json end end end From 67787e4811dcfa06e597661e367e394862d693d8 Mon Sep 17 00:00:00 2001 From: Tobias Matz Date: Thu, 4 Jul 2024 14:52:33 +0200 Subject: [PATCH 9/9] Fix failing spec --- lib/ioki/model/operator/geojson.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/ioki/model/operator/geojson.rb b/lib/ioki/model/operator/geojson.rb index 942a87da..5990ecc2 100644 --- a/lib/ioki/model/operator/geojson.rb +++ b/lib/ioki/model/operator/geojson.rb @@ -13,7 +13,6 @@ def serialize(usecase = :read, only_changed: true) case usecase when :read then super else @_raw_attributes.to_json - # else { coordinates:, type: }.to_json end end end