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

Commit 1bd9920

Browse files
Add order model (#116)
We add accessors for the `api/v1/orders` endpoint to create, find and delete orders, including related models. Co-authored-by: Steffen <steffen@shipcloud.io> [sc-15085](https://app.shortcut.com/shipcloud/story/15085)
1 parent 329ebab commit 1bd9920

4 files changed

Lines changed: 204 additions & 1 deletion

File tree

lib/shipcloud.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module Shipcloud
2121
autoload :Shipment, "shipcloud/shipment"
2222
autoload :Carrier, "shipcloud/carrier"
2323
autoload :Address, "shipcloud/address"
24+
autoload :Order, "shipcloud/order"
2425
autoload :PickupRequest, "shipcloud/pickup_request"
2526
autoload :ShipmentQuote, "shipcloud/shipment_quote"
2627
autoload :Tracker, "shipcloud/tracker"

lib/shipcloud/order.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
module Shipcloud
4+
class Order < Base
5+
include Shipcloud::Operations::All
6+
include Shipcloud::Operations::Find
7+
include Shipcloud::Operations::Create
8+
include Shipcloud::Operations::Delete
9+
10+
attr_reader :id
11+
attr_accessor :external_customer_id, :external_order_id, :placed_at, :total_price, :total_vat,
12+
:currency, :total_weight, :weight_unit, :refundable_until,
13+
:refund_deduction_amount, :delivery_address, :order_line_items, :metadata
14+
end
15+
end

shipcloud.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ Gem::Specification.new do |spec|
1717
spec.license = "MIT"
1818

1919
spec.files = `git ls-files`.split($/)
20-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
2120
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
2221
spec.require_paths = ["lib"]
2322

spec/shipcloud/order_spec.rb

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# frozen_string_literal: true
2+
require "spec_helper"
3+
4+
describe Shipcloud::Order do
5+
describe "#initialize" do
6+
it "initializes all attributes correctly" do
7+
valid_attributes = {
8+
external_order_id: "external_order_id",
9+
external_customer_id: "external_customer_id",
10+
refundable_until: "refundable_until",
11+
total_weight: 12.34,
12+
refund_deduction_amount: 4.32,
13+
delivery_address: {
14+
id: "adress_id",
15+
},
16+
order_line_items: [
17+
{
18+
id: "order_line_item_id",
19+
},
20+
],
21+
}
22+
order = Shipcloud::Order.new(valid_attributes)
23+
24+
expect(order.external_order_id).to eq "external_order_id"
25+
expect(order.external_customer_id).to eq "external_customer_id"
26+
expect(order.refundable_until).to eq "refundable_until"
27+
expect(order.total_weight).to eq 12.34
28+
expect(order.refund_deduction_amount).to eq 4.32
29+
expect(order.delivery_address).to eq(id: "adress_id")
30+
expect(order.order_line_items).to eq [
31+
{ id: "order_line_item_id" },
32+
]
33+
end
34+
end
35+
36+
describe ".all" do
37+
it "executes a GET request to the 'orders' API endpoint" do
38+
expect(Shipcloud).to receive(:request).
39+
with(:get, "orders", {}, api_key: nil, affiliate_id: nil).
40+
and_return([])
41+
42+
Shipcloud::Order.all
43+
end
44+
45+
it "returns a list of Order objects" do
46+
stub_orders_request
47+
48+
orders = Shipcloud::Order.all
49+
50+
orders.each do |order|
51+
expect(order).to be_a Shipcloud::Order
52+
end
53+
end
54+
55+
it "returns a filtered list of order objects when using filter parameters" do
56+
filter = {
57+
"external_customer_id" => "external_customer_id",
58+
"external_order_id" => "external_order_id",
59+
}
60+
61+
expect(Shipcloud).to receive(:request).
62+
with(:get, "orders", filter, api_key: nil, affiliate_id: nil).
63+
and_return(orders_array)
64+
65+
Shipcloud::Order.all(filter, api_key: nil)
66+
end
67+
68+
it "uses the affiliate ID provided for the request" do
69+
expect(Shipcloud).to receive(:request).
70+
with(:get, "orders", {}, api_key: nil, affiliate_id: "affiliate_id").
71+
and_return([])
72+
73+
Shipcloud::Order.all(affiliate_id: "affiliate_id")
74+
end
75+
end
76+
77+
describe ".find" do
78+
it "executes a GET request to the 'orders/:id' API endpoint" do
79+
expect(Shipcloud).to receive(:request).
80+
with(:get, "orders/order_id", {}, api_key: nil, affiliate_id: nil).
81+
and_return("id" => "order_id")
82+
83+
Shipcloud::Order.find("order_id")
84+
end
85+
end
86+
87+
describe ".create" do
88+
it "executes a POST request to the 'orders' API endpoint" do
89+
expect(Shipcloud).to receive(:request).
90+
with(:post, "orders", valid_attributes, api_key: nil, affiliate_id: nil).
91+
and_return("data" => {})
92+
93+
Shipcloud::Order.create(valid_attributes)
94+
end
95+
96+
it "uses the affiliate ID provided for the request" do
97+
expect(Shipcloud).to receive(:request).
98+
with(
99+
:post,
100+
"orders",
101+
valid_attributes,
102+
api_key: nil,
103+
affiliate_id: "affiliate_id",
104+
).and_return("data" => {})
105+
106+
Shipcloud::Order.create(valid_attributes, affiliate_id: "affiliate_id")
107+
end
108+
end
109+
110+
describe ".delete" do
111+
it "executes a DELETE request to the 'orders/:id' API endpoint" do
112+
expect(Shipcloud).to receive(:request).
113+
with(:delete, "orders/123", {}, api_key: nil, affiliate_id: nil).
114+
and_return(true)
115+
116+
Shipcloud::Order.delete("123")
117+
end
118+
119+
it "does not raise an error" do
120+
stub_request(:delete, "https://api.shipcloud.io/v1/orders/123").
121+
to_return(status: 204, body: "")
122+
123+
expect { Shipcloud::Order.delete("123", api_key: "your-api-key") }.
124+
to_not raise_error
125+
end
126+
127+
it "uses the affiliate ID provided for the request" do
128+
expect(Shipcloud).to receive(:request).with(
129+
:delete, "orders/123", {}, api_key: nil, affiliate_id: "affiliate_id"
130+
).and_return(true)
131+
132+
Shipcloud::Order.delete("123", affiliate_id: "affiliate_id")
133+
end
134+
end
135+
136+
def stub_orders_request(params: {}, affiliate_id: nil)
137+
allow(Shipcloud).to receive(:request).
138+
with(:get, "orders", params, api_key: nil, affiliate_id: affiliate_id).
139+
and_return(orders_array)
140+
end
141+
142+
def orders_array
143+
[
144+
{
145+
id: "order_id",
146+
external_order_id: "external_order_id",
147+
external_customer_id: "external_customer_id",
148+
delivery_address: {
149+
id: "adress_id",
150+
},
151+
order_line_items: [
152+
{
153+
id: "order_line_item_id",
154+
},
155+
],
156+
},
157+
{
158+
id: "order_id1",
159+
external_order_id: "external_order_id1",
160+
external_customer_id: "external_customer_id1",
161+
delivery_address: {
162+
id: "adress_id1",
163+
},
164+
order_line_items: [
165+
{
166+
id: "order_line_item_id1",
167+
},
168+
],
169+
},
170+
]
171+
end
172+
173+
def valid_attributes
174+
{
175+
id: "order_id",
176+
external_order_id: "external_order_id",
177+
external_customer_id: "external_customer_id",
178+
delivery_address: {
179+
id: "adress_id",
180+
},
181+
order_line_items: [
182+
{
183+
id: "order_line_item_id",
184+
},
185+
],
186+
}
187+
end
188+
end

0 commit comments

Comments
 (0)