Skip to content

Commit

Permalink
Merge pull request #25 from ioki-mobility/use-etags-from-create-and-u…
Browse files Browse the repository at this point in the history
…pdate

Use etags from create and update
  • Loading branch information
ans-ioki authored Nov 8, 2021
2 parents 60d7d3b + 37c2adf commit 666fff3
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 39 deletions.
7 changes: 3 additions & 4 deletions lib/ioki/apis/endpoints/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@ def call(client, model, args = [], options = {})
outgoing_model_class = @outgoing_model_class || options[:outgoing_model_class] || model_class

unless model.is_a?(outgoing_model_class)
raise(ArgumentError,
"#{model} is not an instance of #{outgoing_model_class}")
raise(ArgumentError, "#{model} is not an instance of #{outgoing_model_class}")
end

parsed_response, = client.request(
parsed_response, response = client.request(
url: client.build_request_url(*Endpoints.url_elements(name, full_path, *args)),
method: :post,
body: { data: model.serialize(:create) }.to_json,
headers: client.all_headers,
params: options[:params]
)

model_class.new(parsed_response['data'])
model_class.new(parsed_response['data'], response.headers[:etag])
end
end
end
7 changes: 3 additions & 4 deletions lib/ioki/apis/endpoints/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,18 @@ def call(client, model, args = [], options = {})
outgoing_model_class = @outgoing_model_class || options[:outgoing_model_class] || model_class

unless model.is_a?(outgoing_model_class)
raise(ArgumentError,
"#{model} is not an instance of #{outgoing_model_class}")
raise(ArgumentError, "#{model} is not an instance of #{outgoing_model_class}")
end

parsed_response, = client.request(
parsed_response, response = client.request(
url: client.build_request_url(*Endpoints.url_elements(name, full_path, *args)),
method: :patch,
body: { data: model.serialize(:update) }.to_json,
headers: client.all_headers,
params: options[:params]
)

model_class.new(parsed_response['data'])
model_class.new(parsed_response['data'], response.headers[:etag])
end
end
end
6 changes: 5 additions & 1 deletion spec/ioki/apis/endpoints/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
headers: client.all_headers,
params: params
).and_return(
{ 'data' => { 'id' => '0815', name: 'attributes altered by server' } }
[
{ 'data' => { 'id' => '0815', name: 'attributes altered by server' } },
instance_double(Faraday::Response, headers: { etag: 'ETAG' })
]
)

result = endpoint.call(client, model, [], params: params)
Expand All @@ -29,6 +32,7 @@
expect(result).not_to eq(model)
expect(result.id).to eq('0815')
expect(result.name).to eq('attributes altered by server')
expect(result._etag).to eq('ETAG')
end

context 'when the passed model instance has the wrong class' do
Expand Down
6 changes: 5 additions & 1 deletion spec/ioki/apis/endpoints/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
headers: client.all_headers,
params: params
).and_return(
{ 'data' => { 'id' => '0815', name: 'attributes altered by server' } }
[
{ 'data' => { 'id' => '0815', name: 'attributes altered by server' } },
instance_double(Faraday::Response, headers: { etag: 'ETAG' })
]
)

result = endpoint.call(client, model, ['0815'], model_class: model_class, params: params)
Expand All @@ -30,6 +33,7 @@
expect(result).not_to eq(model)
expect(result.id).to eq('0815')
expect(result.name).to eq('attributes altered by server')
expect(result._etag).to eq('ETAG')
end

context 'when the passed model instance has the wrong class' do
Expand Down
2 changes: 1 addition & 1 deletion spec/ioki/driver_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
it 'calls request on the client with expected params' do
expect(driver_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/driver/request_tokens')
result_with_data
[result_with_data, full_response]
end

expect(driver_client.create_request_token(request_token, options)).
Expand Down
17 changes: 8 additions & 9 deletions spec/ioki/passenger_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
it 'calls request on the client with expected params' do
expect(passenger_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/passenger/ride_inquiry')
result_with_data
[result_with_data, full_response]
end

expect(passenger_client.create_ride_inquiry(ride_inquiry, options)).
Expand All @@ -92,7 +92,7 @@
it 'calls request on the client with expected params' do
expect(passenger_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/passenger/rides')
result_with_data
[result_with_data, full_response]
end

expect(passenger_client.create_ride(ride, options)).
Expand Down Expand Up @@ -120,7 +120,7 @@
}.to_json
)

result_with_data
[result_with_data, full_response]
end

expect(passenger_client.create_cancellation(ride, cancellation)).
Expand All @@ -136,7 +136,7 @@
expect(passenger_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/passenger/rides/RIDE_ID/booking')
expect(params[:body]).to eq '{"data":{"ride_version":2,"payment_method":null}}'
result_with_data
[result_with_data, full_response]
end

expect(passenger_client.create_booking(ride, booking, options)).
Expand All @@ -152,7 +152,7 @@
it 'calls request on the client with expected params' do
expect(passenger_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/passenger/phone_verification_requests')
result_with_data
[result_with_data, full_response]
end

expect(passenger_client.create_phone_verification_request(phone_verification_request, options)).
Expand All @@ -166,7 +166,7 @@
it 'calls request on the client with expected params' do
expect(passenger_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/passenger/user/phone_number')
result_with_data
[result_with_data, full_response]
end

expect(passenger_client.update_user_phone_number(user_phone_number, options.merge(model: user_phone_number))).
Expand All @@ -185,8 +185,7 @@
expect(params[:url].to_s).to eq('https://app.io.ki/api/passenger/user')
expect(JSON.parse(params[:body])['data']).
to match(hash_including('first_name' => 'Lupe', 'last_name' => 'Smiles'))

result_with_data
[result_with_data, full_response]
end

expect(passenger_client.update_user(user, options)).
Expand All @@ -212,7 +211,7 @@
it 'calls request on the client with expected params' do
expect(passenger_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/passenger/rides/RIDE_ID/rating')
result_with_data
[result_with_data, full_response]
end

expect(passenger_client.create_rating(ride, rating,
Expand Down
35 changes: 16 additions & 19 deletions spec/ioki/platform_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
it 'calls request on the client with expected params' do
expect(platform_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/stations')
result_with_data
[result_with_data, full_response]
end

expect(platform_client.create_station('0815', station, options)).
Expand All @@ -117,7 +117,7 @@
expect(platform_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/stations/4711')
expect(params[:method]).to eq :patch
result_with_data
[result_with_data, full_response]
end

expect(platform_client.update_station('0815', station, options)).
Expand Down Expand Up @@ -167,7 +167,7 @@
it 'calls request on the client with expected params' do
expect(platform_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/vehicles')
result_with_data
[result_with_data, full_response]
end

expect(platform_client.create_vehicle('0815', vehicle, options)).
Expand All @@ -181,7 +181,7 @@
it 'calls request on the client with expected params' do
expect(platform_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/vehicles/4711')
result_with_data
[result_with_data, full_response]
end

expect(platform_client.update_vehicle('0815', vehicle, options)).
Expand Down Expand Up @@ -231,7 +231,7 @@
it 'calls request on the client with expected params' do
expect(platform_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/rides')
result_with_data
[result_with_data, full_response]
end

expect(platform_client.create_ride('0815', ride, options)).
Expand Down Expand Up @@ -269,7 +269,7 @@
it 'calls request on the client with expected params' do
expect(platform_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/providers/0815/users')
result_with_data
[result_with_data, full_response]
end

expect(platform_client.create_user('0815', user, options)).
Expand All @@ -283,7 +283,7 @@
it 'calls request on the client with expected params' do
expect(platform_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/providers/0815/users/4711')
result_with_data
[result_with_data, full_response]
end

expect(platform_client.update_user('0815', user, options)).
Expand Down Expand Up @@ -333,7 +333,7 @@
it 'calls request on the client with expected params' do
expect(platform_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/drivers')
result_with_data
[result_with_data, full_response]
end

expect(platform_client.create_driver('0815', driver, options)).
Expand All @@ -347,8 +347,7 @@
it 'calls request on the client with expected params' do
expect(platform_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/drivers/4711')

result_with_data
[result_with_data, full_response]
end

expect(platform_client.update_driver('0815', driver, options)).
Expand Down Expand Up @@ -398,7 +397,7 @@
it 'calls request on the client with expected params' do
expect(platform_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/driver_vehicle_connections')
result_with_data
[result_with_data, full_response]
end

expect(platform_client.create_driver_vehicle_connection('0815', driver_vehicle_connection, options)).
Expand All @@ -412,8 +411,7 @@
it 'calls request on the client with expected params' do
expect(platform_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/driver_vehicle_connections/4711')

result_with_data
[result_with_data, full_response]
end

expect(platform_client.update_driver_vehicle_connection('0815', driver_vehicle_connection, options)).
Expand Down Expand Up @@ -475,7 +473,7 @@
it 'calls request on the client with expected params' do
expect(platform_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/rides/4711/rating')
result_with_data
[result_with_data, full_response]
end

expect(platform_client.create_rating('0815', rating, options)).
Expand All @@ -489,8 +487,7 @@
it 'calls request on the client with expected params' do
expect(platform_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/rides/4711/rating/1337')

result_with_data
[result_with_data, full_response]
end

expect(platform_client.update_rating('0815', '4711', rating, options)).
Expand All @@ -516,7 +513,7 @@
it 'calls request on the client with expected params' do
expect(platform_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/rides/4711/booking')
result_with_data
[result_with_data, full_response]
end

expect(platform_client.create_booking('0815', '4711', booking, options)).
Expand All @@ -530,7 +527,7 @@
it 'calls request on the client with expected params' do
expect(platform_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/providers/0815/users/4711/request_tokens')
result_with_data
[result_with_data, full_response]
end

expect(platform_client.create_request_token('0815', '4711', passenger_request_token, options)).
Expand All @@ -544,7 +541,7 @@
it 'calls request on the client with expected params' do
expect(platform_client).to receive(:request) do |params|
expect(params[:url].to_s).to eq('https://app.io.ki/api/platform/products/0815/ride_inquiry')
result_with_data
[result_with_data, full_response]
end

expect(platform_client.create_ride_inquiry('0815', ride_inquiry, options)).
Expand Down

0 comments on commit 666fff3

Please sign in to comment.