Skip to content
This repository was archived by the owner on Aug 14, 2024. It is now read-only.

Commit 329ebab

Browse files
authored
Ensure compatibilty with ruby 2.x and 3.x (#113)
We were affected by the change in positional keyword arguments for ruby 3.x, so we need to change the `.update` and `#update` implementations slightly to be compatible with ruby 2.x and 3.x. [sc-14935](https://app.shortcut.com/shipcloud/story/14935)
1 parent 8b31952 commit 329ebab

3 files changed

Lines changed: 61 additions & 11 deletions

File tree

lib/shipcloud/operations/update.rb

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ module Shipcloud
44
module Operations
55
module Update
66
module ClassMethods
7-
# Updates a object
7+
# Updates an object
88
# @param [String] id The id of the object that should be updated
99
# @param [Hash] attributes The attributes that should be updated
10-
# @param \[String\] optional api_key The api key. If no api key is given, Shipcloud.api_key
10+
# @param [String] optional api_key The api key. If no api key is given, Shipcloud.api_key
1111
# will be used for the request
12-
def update(id, attributes, api_key: nil, affiliate_id: nil)
12+
# @param [String] optional affiliate_id Your affiliate ID. If no affiliate ID is given,
13+
# Shipcloud.affiliate_id will be used for the request
14+
def update(id, attributes = {}, **kwargs)
15+
attributes.merge!(kwargs)
16+
options = attributes.slice(:api_key, :affiliate_id) || {}
17+
attributes.reject! { |key| [:api_key, :affiliate_id].include?(key) }
1318
response = Shipcloud.request(
14-
:put, "#{base_url}/#{id}", attributes, api_key: api_key, affiliate_id: affiliate_id
19+
:put, "#{base_url}/#{id}", attributes,
20+
api_key: options[:api_key], affiliate_id: options[:affiliate_id]
1521
)
1622
new(response)
1723
end
@@ -21,16 +27,15 @@ def self.included(base)
2127
base.extend(ClassMethods)
2228
end
2329

24-
# Updates a object
30+
# Updates an object
2531
#
2632
# @param [Hash] attributes The attributes that should be updated
27-
# @param \[String\] optional api_key The api key. If no api key is given, Shipcloud.api_key
33+
# @param [String] optional api_key The api key. If no api key is given, Shipcloud.api_key
2834
# will be used for the request
29-
def update(attributes, api_key: nil, affiliate_id: nil)
30-
response = Shipcloud.request(
31-
:put, "#{base_url}/#{id}", attributes, api_key: api_key, affiliate_id: affiliate_id
32-
)
33-
set_attributes(response)
35+
# @param [String] optional affiliate_id Your affiliate ID. If no affiliate ID is given,
36+
# Shipcloud.affiliate_id will be used for the request
37+
def update(attributes = {}, **kwargs)
38+
self.class.update(id, attributes, **kwargs)
3439
end
3540
end
3641
end

spec/shipcloud/address_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,28 @@
131131
end
132132
end
133133

134+
describe "#update" do
135+
it "makes a new PUT request using the correct API endpoint" do
136+
expect(Shipcloud).to receive(:request).with(
137+
:put, "addresses/123", { street: "Mittelweg" }, api_key: nil, affiliate_id: nil
138+
).and_return("data" => {})
139+
140+
address = Shipcloud::Address.new(id: "123")
141+
142+
address.update(street: "Mittelweg")
143+
end
144+
145+
it "uses the affiliate ID provided for the request" do
146+
expect(Shipcloud).to receive(:request).with(
147+
:put, "addresses/123", { street: "Mittelweg" }, api_key: nil, affiliate_id: "affiliate_id"
148+
).and_return("data" => {})
149+
150+
address = Shipcloud::Address.new(id: "123")
151+
152+
address.update({ street: "Mittelweg" }, affiliate_id: "affiliate_id")
153+
end
154+
end
155+
134156
def stub_addresses_request(affiliate_id: nil)
135157
allow(Shipcloud).to receive(:request).
136158
with(:get, "addresses", {}, api_key: nil, affiliate_id: affiliate_id).

spec/shipcloud/shipment_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,29 @@
193193
end
194194
end
195195

196+
describe "#update" do
197+
it "makes a new PUT request using the correct API endpoint" do
198+
expect(Shipcloud).to receive(:request).
199+
with(:put, "shipments/123", { carrier: "ups" }, api_key: nil, affiliate_id: nil).
200+
and_return("data" => {})
201+
202+
shipment = Shipcloud::Shipment.new(id: "123")
203+
204+
shipment.update(carrier: "ups")
205+
end
206+
207+
it "uses the affiliate ID provided for the request" do
208+
expect(Shipcloud).to receive(:request).
209+
with(
210+
:put, "shipments/123", { carrier: "ups" }, api_key: nil, affiliate_id: "affiliate_id"
211+
).and_return("data" => {})
212+
213+
shipment = Shipcloud::Shipment.new(id: "123")
214+
215+
shipment.update({ carrier: "ups" }, affiliate_id: "affiliate_id")
216+
end
217+
end
218+
196219
def stub_shipments_requests(affiliate_id: nil)
197220
allow(Shipcloud).to receive(:request).
198221
with(:get, "shipments", {}, api_key: nil, affiliate_id: affiliate_id).

0 commit comments

Comments
 (0)