printfection 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +2 -0
- data/.travis.yml +14 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +247 -0
- data/Rakefile +8 -0
- data/lib/printfection/actions.rb +40 -0
- data/lib/printfection/address.rb +15 -0
- data/lib/printfection/api.rb +87 -0
- data/lib/printfection/asset.rb +11 -0
- data/lib/printfection/campaign.rb +20 -0
- data/lib/printfection/error.rb +8 -0
- data/lib/printfection/item.rb +29 -0
- data/lib/printfection/line_item.rb +21 -0
- data/lib/printfection/manifest.rb +22 -0
- data/lib/printfection/order.rb +99 -0
- data/lib/printfection/product.rb +7 -0
- data/lib/printfection/relation.rb +59 -0
- data/lib/printfection/resource.rb +31 -0
- data/lib/printfection/size.rb +11 -0
- data/lib/printfection/transforms.rb +8 -0
- data/lib/printfection/util.rb +8 -0
- data/lib/printfection/version.rb +4 -0
- data/lib/printfection.rb +27 -0
- data/printfection.gemspec +29 -0
- data/spec/actions_spec.rb +83 -0
- data/spec/api_spec.rb +224 -0
- data/spec/asset_spec.rb +30 -0
- data/spec/campaign_spec.rb +44 -0
- data/spec/item_spec.rb +118 -0
- data/spec/line_item_spec.rb +44 -0
- data/spec/manifest_spec.rb +81 -0
- data/spec/order_spec.rb +277 -0
- data/spec/printfection_spec.rb +2 -0
- data/spec/product_spec.rb +21 -0
- data/spec/relation_spec.rb +154 -0
- data/spec/resource_spec.rb +62 -0
- data/spec/size_spec.rb +30 -0
- data/spec/spec_helper.rb +96 -0
- data/spec/support/resource_shared_examples.rb +36 -0
- data/spec/util_spec.rb +11 -0
- metadata +186 -0
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require 'printfection'
|
|
2
|
+
|
|
3
|
+
module Printfection
|
|
4
|
+
describe LineItem do
|
|
5
|
+
it_behaves_like "Resource"
|
|
6
|
+
include_examples "Actions::Retrieve"
|
|
7
|
+
include_examples "Actions::Create"
|
|
8
|
+
include_examples "Actions::Update"
|
|
9
|
+
include_examples "Actions::Delete"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
describe LineItem, ".uri" do
|
|
13
|
+
it "returns the base resource uri" do
|
|
14
|
+
expect(LineItem.uri).to eql "/lineitems"
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
describe LineItem, "properties" do
|
|
19
|
+
let(:json) do
|
|
20
|
+
JSON.parse <<-JSON
|
|
21
|
+
{
|
|
22
|
+
"id": 123,
|
|
23
|
+
"object": "lineitem",
|
|
24
|
+
"order_id": 1,
|
|
25
|
+
"item_id": 1,
|
|
26
|
+
"size_id": 2,
|
|
27
|
+
"quantity": 3,
|
|
28
|
+
"created_at": "2014-09-12T10:22:37Z"
|
|
29
|
+
}
|
|
30
|
+
JSON
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "gives access to JSON properties" do
|
|
34
|
+
line_item = LineItem.new(json)
|
|
35
|
+
expect(line_item.id).to eql 123
|
|
36
|
+
expect(line_item.order_id).to eql 1
|
|
37
|
+
expect(line_item.item_id).to eql 1
|
|
38
|
+
expect(line_item.size_id).to eql 2
|
|
39
|
+
expect(line_item.quantity).to eql 3
|
|
40
|
+
expect(line_item.created_at).to eql DateTime.parse("2014-09-12T10:22:37Z")
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
require 'printfection'
|
|
2
|
+
|
|
3
|
+
module Printfection
|
|
4
|
+
describe Manifest, "properties" do
|
|
5
|
+
let(:json) do
|
|
6
|
+
JSON.parse <<-JSON
|
|
7
|
+
{
|
|
8
|
+
"subtotal": 0,
|
|
9
|
+
"tax": 0,
|
|
10
|
+
"shipping": 0,
|
|
11
|
+
"fulfillment": 0,
|
|
12
|
+
"total": 0,
|
|
13
|
+
"created_at": "2014-09-12T10:22:37Z",
|
|
14
|
+
"received_at": null,
|
|
15
|
+
"approved_at": null,
|
|
16
|
+
"processed_at": null,
|
|
17
|
+
"shipped_at": null,
|
|
18
|
+
"completed_at": null,
|
|
19
|
+
"lineitems": [
|
|
20
|
+
{
|
|
21
|
+
"id": 1,
|
|
22
|
+
"object": "lineitem",
|
|
23
|
+
"order_id": 1,
|
|
24
|
+
"item_id": 2,
|
|
25
|
+
"size_id": 3,
|
|
26
|
+
"quantity": 4,
|
|
27
|
+
"created_at": "2014-09-12T10:22:37Z"
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"id": 2,
|
|
31
|
+
"object": "lineitem",
|
|
32
|
+
"order_id": 1,
|
|
33
|
+
"item_id": 2,
|
|
34
|
+
"size_id": 3,
|
|
35
|
+
"quantity": 4,
|
|
36
|
+
"created_at": "2014-09-12T10:22:37Z"
|
|
37
|
+
}
|
|
38
|
+
],
|
|
39
|
+
"shipments": [
|
|
40
|
+
{
|
|
41
|
+
"carrier": "UPS",
|
|
42
|
+
"method": "Ground",
|
|
43
|
+
"tracking_number": "12345678",
|
|
44
|
+
"created_at": "2014-09-12T10:22:37Z"
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
"carrier": "FedEx",
|
|
48
|
+
"method": "2DA",
|
|
49
|
+
"tracking_number": "90123456",
|
|
50
|
+
"created_at": "2014-09-12T10:22:37Z"
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
JSON
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it "gives access to JSON properties" do
|
|
58
|
+
manifest = Manifest.new(json)
|
|
59
|
+
expect(manifest.subtotal).to eql 0.0
|
|
60
|
+
expect(manifest.tax).to eql 0.0
|
|
61
|
+
expect(manifest.shipping).to eql 0.0
|
|
62
|
+
expect(manifest.fulfillment).to eql 0.0
|
|
63
|
+
expect(manifest.total).to eql 0.0
|
|
64
|
+
expect(manifest.created_at).to eql DateTime.parse("2014-09-12T10:22:37Z")
|
|
65
|
+
expect(manifest.received_at).to eql nil
|
|
66
|
+
expect(manifest.approved_at).to eql nil
|
|
67
|
+
expect(manifest.processed_at).to eql nil
|
|
68
|
+
expect(manifest.shipped_at).to eql nil
|
|
69
|
+
expect(manifest.completed_at).to eql nil
|
|
70
|
+
|
|
71
|
+
expect(manifest.line_items).to be_an Array
|
|
72
|
+
expect(manifest.line_items.count).to eql 2
|
|
73
|
+
expect(manifest.line_items.first).to be_a LineItem
|
|
74
|
+
|
|
75
|
+
expect(manifest.shipments).to be_an Array
|
|
76
|
+
expect(manifest.shipments.count).to eql 2
|
|
77
|
+
expect(manifest.shipments.first).to be_a Hashie::Mash
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
data/spec/order_spec.rb
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
require 'printfection'
|
|
2
|
+
|
|
3
|
+
module Printfection
|
|
4
|
+
describe Order do
|
|
5
|
+
it_behaves_like "Resource"
|
|
6
|
+
include_examples "Actions::Retrieve"
|
|
7
|
+
include_examples "Actions::List"
|
|
8
|
+
include_examples "Actions::Create"
|
|
9
|
+
include_examples "Actions::Update"
|
|
10
|
+
include_examples "Actions::Delete"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
describe Order, ".uri" do
|
|
14
|
+
it "returns the base resource uri" do
|
|
15
|
+
expect(Order.uri).to eql "/orders"
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
describe Order, "properties" do
|
|
20
|
+
let(:json) do
|
|
21
|
+
JSON.parse <<-JSON
|
|
22
|
+
{
|
|
23
|
+
"id": 1,
|
|
24
|
+
"object": "order",
|
|
25
|
+
"campaign_id": 2,
|
|
26
|
+
"status": "open",
|
|
27
|
+
"code": "nhou890o",
|
|
28
|
+
"url": "https://printfection.com/nhou890o",
|
|
29
|
+
"gift": false,
|
|
30
|
+
"gift_message": "",
|
|
31
|
+
"created_at": "2014-09-12T10:22:37Z",
|
|
32
|
+
"ship_to": {
|
|
33
|
+
"name": "Joseph Schmo",
|
|
34
|
+
"address": "123 Main Street",
|
|
35
|
+
"address2": "Suite 101",
|
|
36
|
+
"company": "ACME Inc.",
|
|
37
|
+
"city": "Denver",
|
|
38
|
+
"state": "Colorado",
|
|
39
|
+
"zip": "80202",
|
|
40
|
+
"country": "United States",
|
|
41
|
+
"email": "joseph.schmo@example.com",
|
|
42
|
+
"phone": "751-166-2910"
|
|
43
|
+
},
|
|
44
|
+
"lineitems": [
|
|
45
|
+
{
|
|
46
|
+
"id": 1,
|
|
47
|
+
"object": "lineitem",
|
|
48
|
+
"order_id": 1,
|
|
49
|
+
"item_id": 2,
|
|
50
|
+
"size_id": 3,
|
|
51
|
+
"quantity": 4,
|
|
52
|
+
"created_at": "2014-09-12T10:22:37Z"
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"id": 2,
|
|
56
|
+
"object": "lineitem",
|
|
57
|
+
"order_id": 1,
|
|
58
|
+
"item_id": 2,
|
|
59
|
+
"size_id": 3,
|
|
60
|
+
"quantity": 4,
|
|
61
|
+
"created_at": "2014-09-12T10:22:37Z"
|
|
62
|
+
}
|
|
63
|
+
],
|
|
64
|
+
"manifest": {
|
|
65
|
+
"subtotal": 0,
|
|
66
|
+
"tax": 0,
|
|
67
|
+
"shipping": 0,
|
|
68
|
+
"fulfillment": 0,
|
|
69
|
+
"total": 0,
|
|
70
|
+
"created_at": "2014-09-12T10:22:37Z",
|
|
71
|
+
"received_at": null,
|
|
72
|
+
"approved_at": null,
|
|
73
|
+
"processed_at": null,
|
|
74
|
+
"shipped_at": null,
|
|
75
|
+
"completed_at": null,
|
|
76
|
+
"lineitems": [
|
|
77
|
+
{
|
|
78
|
+
"id": 1,
|
|
79
|
+
"object": "lineitem",
|
|
80
|
+
"order_id": 1,
|
|
81
|
+
"item_id": 2,
|
|
82
|
+
"size_id": 3,
|
|
83
|
+
"quantity": 4,
|
|
84
|
+
"created_at": "2014-09-12T10:22:37Z"
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
"id": 2,
|
|
88
|
+
"object": "lineitem",
|
|
89
|
+
"order_id": 1,
|
|
90
|
+
"item_id": 2,
|
|
91
|
+
"size_id": 3,
|
|
92
|
+
"quantity": 4,
|
|
93
|
+
"created_at": "2014-09-12T10:22:37Z"
|
|
94
|
+
}
|
|
95
|
+
],
|
|
96
|
+
"shipments": [
|
|
97
|
+
{
|
|
98
|
+
"carrier": "UPS",
|
|
99
|
+
"method": "Ground",
|
|
100
|
+
"tracking_number": "12345678",
|
|
101
|
+
"created_at": "2014-09-12T10:22:37Z"
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
"carrier": "FedEx",
|
|
105
|
+
"method": "2DA",
|
|
106
|
+
"tracking_number": "90123456",
|
|
107
|
+
"created_at": "2014-09-12T10:22:37Z"
|
|
108
|
+
}
|
|
109
|
+
]
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
JSON
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
it "gives access to JSON properties" do
|
|
116
|
+
order = Order.new(json)
|
|
117
|
+
expect(order.id).to eql 1
|
|
118
|
+
expect(order.campaign_id).to eql 2
|
|
119
|
+
expect(order.created_at).to eql DateTime.parse("2014-09-12T10:22:37Z")
|
|
120
|
+
expect(order.code).to eql "nhou890o"
|
|
121
|
+
expect(order.url).to eql "https://printfection.com/nhou890o"
|
|
122
|
+
expect(order.gift).to eql false
|
|
123
|
+
expect(order.gift_message).to eql ""
|
|
124
|
+
|
|
125
|
+
expect(order.ship_to).to be_an Address
|
|
126
|
+
expect(order.manifest).to be_a Manifest
|
|
127
|
+
|
|
128
|
+
expect(order.line_items).to be_a Relation
|
|
129
|
+
expect(order.line_items.uri).to eql "/orders/1/lineitems"
|
|
130
|
+
expect(order.line_items.count).to eql 2
|
|
131
|
+
expect(order.line_items.first).to be_a LineItem
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
describe Order, "#place" do
|
|
136
|
+
it "performs a post to place the order" do
|
|
137
|
+
order = Order.new(:id => 123)
|
|
138
|
+
expect(Printfection).to receive(:post).
|
|
139
|
+
with("/orders/123/place")
|
|
140
|
+
order.place
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
describe Order, "#cancel" do
|
|
145
|
+
it "deletes the order" do
|
|
146
|
+
order = Order.new(:id => 123)
|
|
147
|
+
expect(order).to receive(:delete)
|
|
148
|
+
order.cancel
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
describe Order, "#campaign" do
|
|
153
|
+
it "returns the order's Campaign" do
|
|
154
|
+
campaign = double
|
|
155
|
+
allow(Campaign).to receive(:retrieve).
|
|
156
|
+
with(123).
|
|
157
|
+
and_return(campaign)
|
|
158
|
+
order = Order.new(campaign_id: 123)
|
|
159
|
+
expect(order.campaign).to eql campaign
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
describe Order, "#open?" do
|
|
164
|
+
it "returns true when the order's status is 'open'" do
|
|
165
|
+
order = Order.new status: "open"
|
|
166
|
+
expect(order.open?).to eql true
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
it "returns false when the order's status is not 'open'" do
|
|
170
|
+
order = Order.new status: "cancelled"
|
|
171
|
+
expect(order.open?).to eql false
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
describe Order, "#cancelled?" do
|
|
176
|
+
it "returns true when the order's status is 'cancelled'" do
|
|
177
|
+
order = Order.new status: "cancelled"
|
|
178
|
+
expect(order.cancelled?).to eql true
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
it "returns false when the order's status is not 'cancelled'" do
|
|
182
|
+
order = Order.new status: "open"
|
|
183
|
+
expect(order.cancelled?).to eql false
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
describe Order, "#received?" do
|
|
188
|
+
it "returns true when the order's status is 'received'" do
|
|
189
|
+
order = Order.new status: "received"
|
|
190
|
+
expect(order.received?).to eql true
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
it "returns true when the order's status is 'approved'" do
|
|
194
|
+
order = Order.new status: "approved"
|
|
195
|
+
expect(order.received?).to eql true
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
it "returns true when the order's status is 'processed'" do
|
|
199
|
+
order = Order.new status: "processed"
|
|
200
|
+
expect(order.received?).to eql true
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
it "returns true when the order's status is 'shipped'" do
|
|
204
|
+
order = Order.new status: "shipped"
|
|
205
|
+
expect(order.received?).to eql true
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
it "returns true when the order's status is 'completed'" do
|
|
209
|
+
order = Order.new status: "completed"
|
|
210
|
+
expect(order.received?).to eql true
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
describe Order, "#approved?" do
|
|
215
|
+
it "returns true when the order's status is 'approved'" do
|
|
216
|
+
order = Order.new status: "approved"
|
|
217
|
+
expect(order.approved?).to eql true
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
it "returns true when the order's status is 'processed'" do
|
|
221
|
+
order = Order.new status: "processed"
|
|
222
|
+
expect(order.approved?).to eql true
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
it "returns true when the order's status is 'shipped'" do
|
|
226
|
+
order = Order.new status: "shipped"
|
|
227
|
+
expect(order.approved?).to eql true
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
it "returns true when the order's status is 'completed'" do
|
|
231
|
+
order = Order.new status: "completed"
|
|
232
|
+
expect(order.approved?).to eql true
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
describe Order, "#processed?" do
|
|
237
|
+
it "returns true when the order's status is 'processed'" do
|
|
238
|
+
order = Order.new status: "processed"
|
|
239
|
+
expect(order.processed?).to eql true
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
it "returns true when the order's status is 'shipped'" do
|
|
243
|
+
order = Order.new status: "shipped"
|
|
244
|
+
expect(order.processed?).to eql true
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
it "returns true when the order's status is 'completed'" do
|
|
248
|
+
order = Order.new status: "completed"
|
|
249
|
+
expect(order.processed?).to eql true
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
describe Order, "#shipped?" do
|
|
254
|
+
it "returns true when the order's status is 'shipped'" do
|
|
255
|
+
order = Order.new status: "shipped"
|
|
256
|
+
expect(order.shipped?).to eql true
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
it "returns true when the order's status is 'completed'" do
|
|
260
|
+
order = Order.new status: "completed"
|
|
261
|
+
expect(order.shipped?).to eql true
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
describe Order, "#completed?" do
|
|
266
|
+
it "returns true when the order's status is 'completed'" do
|
|
267
|
+
order = Order.new status: "completed"
|
|
268
|
+
expect(order.completed?).to eql true
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
it "returns false when the order's status is not 'completed'" do
|
|
272
|
+
order = Order.new status: "open"
|
|
273
|
+
expect(order.completed?).to eql false
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require 'printfection'
|
|
2
|
+
|
|
3
|
+
module Printfection
|
|
4
|
+
describe Product, "properties" do
|
|
5
|
+
let(:json) do
|
|
6
|
+
JSON.parse <<-JSON
|
|
7
|
+
{
|
|
8
|
+
"id": 123,
|
|
9
|
+
"name": "American Apparel 50/50 T-Shirt"
|
|
10
|
+
}
|
|
11
|
+
JSON
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "gives access to JSON properties" do
|
|
15
|
+
product = Product.new(json)
|
|
16
|
+
expect(product.id).to eql 123
|
|
17
|
+
expect(product.name).to eql "American Apparel 50/50 T-Shirt"
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
require 'printfection'
|
|
2
|
+
|
|
3
|
+
module Printfection
|
|
4
|
+
describe Relation do
|
|
5
|
+
it "behaves like a collection" do
|
|
6
|
+
author = {id: 1}
|
|
7
|
+
books = [{title: "Book 1"}, {title: "Book 2"}]
|
|
8
|
+
|
|
9
|
+
relation = Relation.new(
|
|
10
|
+
parent: author,
|
|
11
|
+
children: books,
|
|
12
|
+
path: '/books',
|
|
13
|
+
keys: {:id => :author_id}
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
expect(relation.size).to eql (2)
|
|
17
|
+
expect(relation.length).to eql (2)
|
|
18
|
+
expect(relation.count).to eql (2)
|
|
19
|
+
|
|
20
|
+
titles = relation.map { |book| book[:title] }
|
|
21
|
+
expect(titles).to eql ["Book 1", "Book 2"]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "applies the parent keys to the children" do
|
|
25
|
+
author = {id: 1, name: "Mark Twain"}
|
|
26
|
+
books = [{title: "Book 1"}, {title: "Book 2"}]
|
|
27
|
+
|
|
28
|
+
relation = Relation.new(
|
|
29
|
+
parent: author,
|
|
30
|
+
children: books,
|
|
31
|
+
path: '/books',
|
|
32
|
+
keys: {:id => :author_id, :name => :author_name}
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
expect(relation.first).to eql({title: "Book 1", author_id: 1, author_name: "Mark Twain"})
|
|
36
|
+
expect(relation.last).to eql({title: "Book 2", author_id: 1, author_name: "Mark Twain"})
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it "applies the relation to the childrenance" do
|
|
40
|
+
book_klass = Class.new(Resource) do
|
|
41
|
+
property :title
|
|
42
|
+
property :author_id
|
|
43
|
+
property :author_name
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
author = {id: 1, name: "Mark Twain"}
|
|
47
|
+
books = [book_klass.new({title: "Book 1"}), book_klass.new({title: "Book 2"})]
|
|
48
|
+
|
|
49
|
+
relation = Relation.new(
|
|
50
|
+
children: books
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
expect(relation.first.relation).to eql relation
|
|
54
|
+
expect(relation.last.relation).to eql relation
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
it "includes the provided actions" do
|
|
59
|
+
parent = double.as_null_object
|
|
60
|
+
children = double.as_null_object
|
|
61
|
+
|
|
62
|
+
action1 = Module.new do
|
|
63
|
+
def action1_action
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
action2 = Module.new do
|
|
68
|
+
def action2_action
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
relation = Relation.new(
|
|
73
|
+
parent: parent,
|
|
74
|
+
children: children,
|
|
75
|
+
actions: [action1, action2]
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
expect(relation).to respond_to :action1_action
|
|
79
|
+
expect(relation).to respond_to :action2_action
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
describe Relation, "#uri" do
|
|
84
|
+
it "returns a URI for collection actions" do
|
|
85
|
+
author = double(:author, uri: "/authors/1").as_null_object
|
|
86
|
+
books = [{title: "Book 1"}, {title: "Book 2"}]
|
|
87
|
+
|
|
88
|
+
relation = Relation.new(
|
|
89
|
+
parent: author,
|
|
90
|
+
children: books,
|
|
91
|
+
path: '/books',
|
|
92
|
+
keys: {:id => :author_id}
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
expect(relation.uri).to eql "/authors/1/books"
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
describe Relation, "#new" do
|
|
100
|
+
it "invokes the constructor of the child class and returns the result" do
|
|
101
|
+
author = double(:author, uri: "/authors/1").as_null_object
|
|
102
|
+
books = [{title: "Book 1"}, {title: "Book 2"}]
|
|
103
|
+
|
|
104
|
+
klass = Class.new
|
|
105
|
+
instance = double.as_null_object
|
|
106
|
+
|
|
107
|
+
relation = Relation.new(
|
|
108
|
+
parent: author,
|
|
109
|
+
children: books,
|
|
110
|
+
klass: klass,
|
|
111
|
+
path: '/books',
|
|
112
|
+
keys: {:id => :author_id}
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
expect(klass).to receive(:new).
|
|
116
|
+
with({id: 123, age: 12, quantity: 100}).
|
|
117
|
+
and_return(instance)
|
|
118
|
+
|
|
119
|
+
expect(relation.new({id: 123, age: 12, quantity: 100})).to eql instance
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
it "applies the parent keys to the new instance" do
|
|
123
|
+
author = {id: 1, name: "Mark Twain"}
|
|
124
|
+
|
|
125
|
+
relation = Relation.new(
|
|
126
|
+
parent: author,
|
|
127
|
+
klass: Hashie::Mash,
|
|
128
|
+
keys: {:id => :author_id, :name => :author_name}
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
instance = relation.new({
|
|
132
|
+
id: 2,
|
|
133
|
+
author_id: 2
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
expect(instance[:id]).to eql 2
|
|
137
|
+
expect(instance[:author_id]).to eql 1
|
|
138
|
+
expect(instance[:author_name]).to eql "Mark Twain"
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
it "applies the relation to the new instance" do
|
|
142
|
+
author = double(:author, :uri => "/authors/123").as_null_object
|
|
143
|
+
|
|
144
|
+
relation = Relation.new(
|
|
145
|
+
parent: author,
|
|
146
|
+
klass: Resource
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
instance = relation.new
|
|
150
|
+
expect(instance.relation).to eql relation
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
require 'printfection'
|
|
2
|
+
|
|
3
|
+
module Printfection
|
|
4
|
+
describe Resource, "relation" do
|
|
5
|
+
it "saves its relation" do
|
|
6
|
+
relation = double
|
|
7
|
+
resource = Resource.new
|
|
8
|
+
resource.relation = relation
|
|
9
|
+
expect(resource.relation).to eql relation
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
module Printfection
|
|
15
|
+
describe Resource, "#uri" do
|
|
16
|
+
let(:widget_klass) do
|
|
17
|
+
Class.new(Resource) do
|
|
18
|
+
property :id
|
|
19
|
+
|
|
20
|
+
def self.uri
|
|
21
|
+
"/widgets"
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
context "when it does not have a relation" do
|
|
27
|
+
it "returns its class's uri joined with its id" do
|
|
28
|
+
widget = widget_klass.new(id: 123)
|
|
29
|
+
expect(widget.uri).to eql "/widgets/123"
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
context "when it has a relation" do
|
|
34
|
+
it "returns its relations's uri joined with its id" do
|
|
35
|
+
relation = double(:uri => "/manufacturers/123/widgets")
|
|
36
|
+
widget = widget_klass.new(id: 456)
|
|
37
|
+
widget.relation = relation
|
|
38
|
+
expect(widget.uri).to eql "/manufacturers/123/widgets/456"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
module Printfection
|
|
45
|
+
describe Resource, "#changes" do
|
|
46
|
+
it "returns a hash of the changed properties" do
|
|
47
|
+
klass = Class.new(Resource) do
|
|
48
|
+
property :size
|
|
49
|
+
property :color
|
|
50
|
+
property :price
|
|
51
|
+
end
|
|
52
|
+
resource = klass.new(size: "Large", color: "Blue", price: 123.45)
|
|
53
|
+
resource.price = 678.90
|
|
54
|
+
expect(resource.changes).to eql({"price" => 678.90})
|
|
55
|
+
resource.size = "Medium"
|
|
56
|
+
expect(resource.changes).to eql({"size" => "Medium", "price" => 678.90})
|
|
57
|
+
resource.price = 123.45
|
|
58
|
+
expect(resource.changes).to eql({"size" => "Medium"})
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
data/spec/size_spec.rb
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require 'printfection'
|
|
2
|
+
|
|
3
|
+
module Printfection
|
|
4
|
+
describe Size, "properties" do
|
|
5
|
+
let(:json) do
|
|
6
|
+
JSON.parse <<-JSON
|
|
7
|
+
{
|
|
8
|
+
"id": 1,
|
|
9
|
+
"object": "size",
|
|
10
|
+
"name": "Medium",
|
|
11
|
+
"short_name": "M",
|
|
12
|
+
"stock": {
|
|
13
|
+
"available": 498
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
JSON
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "gives access to JSON properties" do
|
|
20
|
+
size = Size.new(json)
|
|
21
|
+
expect(size.id).to eql 1
|
|
22
|
+
expect(size.name).to eql "Medium"
|
|
23
|
+
expect(size.short_name).to eql "M"
|
|
24
|
+
|
|
25
|
+
expect(size.stock).to be_a Hashie::Mash
|
|
26
|
+
expect(size.stock.available).to eql 498
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|