Skip to content

Commit

Permalink
Merge pull request #401 from ioki-mobility/zones
Browse files Browse the repository at this point in the history
Operator Api | Add endpoints for `Zone`
  • Loading branch information
tom-ioki authored Jul 4, 2024
2 parents 9a05d83 + 67787e4 commit ec330b9
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/ioki/apis/operator_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ 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
Expand Down
11 changes: 9 additions & 2 deletions lib/ioki/model/operator/geojson.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@ 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, :update]
attribute :type, type: :string, on: [:create, :read, :update]

def serialize(usecase = :read, only_changed: true)
case usecase
when :read then super
else @_raw_attributes.to_json
end
end
end
end
end
Expand Down
40 changes: 40 additions & 0 deletions lib/ioki/model/operator/zone.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# 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_geojson,
on: [:create, :read, :update],
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
67 changes: 67 additions & 0 deletions spec/ioki/operator_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ec330b9

Please sign in to comment.