Skip to content

Commit ff2a507

Browse files
committed
WIP get patch working
1 parent ce79f4e commit ff2a507

6 files changed

Lines changed: 91 additions & 2 deletions

File tree

‎lib/intelligent_foods/api_client.rb‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,13 @@ def execute_request(request:, uri:, body: nil,
3434
end
3535
end
3636

37-
def build_request_with_body(uri:, body:)
38-
request = Net::HTTP::Post.new(uri)
37+
def build_patch_request(uri:, body:)
38+
build_request_with_body(uri: uri, body: body,
39+
http_method: Net::HTTP::Patch)
40+
end
41+
42+
def build_request_with_body(uri:, body:, http_method: Net::HTTP::Post)
43+
request = http_method.new(uri)
3944
request.body = body.to_json
4045
request["content-type"] = "application/json"
4146
request

‎lib/intelligent_foods/errors.rb‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ class OrderNotCancelledError < ApiError; end
88
class OrderNotCreatedError < ApiError; end
99

1010
class AuthenticationError < ApiError; end
11+
12+
class OrderNotUpdatedError < ApiError; end
1113
end

‎lib/intelligent_foods/resources/order.rb‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ def cancel!
4545
end
4646
end
4747

48+
def update!
49+
uri = URI("#{IntelligentFoods.base_url}/order/#{id}")
50+
request = client.build_patch_request(uri: uri, body: update_request_body)
51+
response = client.execute_request(request: request, uri: uri)
52+
if response.success?
53+
Order::build_from_response(response.data)
54+
else
55+
mark_as_invalid
56+
raise_error(OrderNotUpdatedError, response)
57+
end
58+
end
59+
4860
def request_body
4961
@request_body ||= {
5062
menu_id: menu.id,
@@ -55,6 +67,12 @@ def request_body
5567
}
5668
end
5769

70+
def update_request_body
71+
@update_request_body ||= {
72+
ship_to: ship_to,
73+
}
74+
end
75+
5876
def cancelled?
5977
status.downcase == CANCELLED
6078
end

‎spec/intelligent_foods/api_client_spec.rb‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@
165165
end
166166
end
167167

168+
describe "#build_patch_request" do
169+
it "builds a request using the Patch HTTP method" do
170+
client = IntelligentFoods::ApiClient.new(id: "id", secret: "secret")
171+
172+
result = client.build_patch_request(uri: "test.com", body: {})
173+
174+
expect(result.method).to eq("PATCH")
175+
end
176+
end
177+
168178
describe "#authenticated?" do
169179
it "is not authenticated" do
170180
client = IntelligentFoods::ApiClient.new(id: "id", secret: "secret")

‎spec/intelligent_foods/resources/order_spec.rb‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,45 @@
142142
end
143143
end
144144
end
145+
146+
describe "#update!" do
147+
it "updates the order" do
148+
order = build_stubbed_order
149+
body = build_order_response
150+
response = build_response(body: body)
151+
stub_api_response response: response
152+
153+
result = order.update!
154+
155+
expect(result).to be_accepted
156+
end
157+
158+
context "the response code is not 200" do
159+
it "raises a OrderNotUpdatedError" do
160+
order = build_stubbed_order
161+
response = error_response(message: "Order Not Found",
162+
http_status_code: 404)
163+
stub_api_response response: response
164+
165+
expect {
166+
order.update!
167+
}.to raise_error(IntelligentFoods::OrderNotUpdatedError)
168+
end
169+
170+
it "marks the order as not valid" do
171+
order = build_stubbed_order
172+
response = error_response(message: "Order Not Found",
173+
http_status_code: 404)
174+
stub_api_response response: response
175+
176+
begin
177+
order.update!
178+
rescue IntelligentFoods::OrderNotUpdatedError
179+
# noop
180+
end
181+
182+
expect(order).not_to be_valid
183+
end
184+
end
185+
end
145186
end

‎spec/support/helpers/api_helper.rb‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ def read_error_response
4444
parse_json_file(ERROR_API_RESPONSE)
4545
end
4646

47+
def build_stubbed_order
48+
recipient = build(:recipient)
49+
menu = build(:menu, id: "2023-01-01")
50+
order_item = build(:order_item)
51+
callback_url = "https://api.domain.com/callback"
52+
IntelligentFoods::Order.new(menu: menu,
53+
recipient: recipient,
54+
delivery_date: "2023-01-07",
55+
items: [order_item],
56+
external_id: "1337",
57+
callback_url: callback_url)
58+
end
59+
4760
def read_menu_api_response
4861
parse_json_file(MENU_API_RESPONSE)
4962
end

0 commit comments

Comments
 (0)