after_ship 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe DateUtils do
4
+ context 'parse' do
5
+ it 'empty string' do
6
+ date = DateUtils.parse('')
7
+ expect(date).to be_nil
8
+ end
9
+
10
+ it 'nil' do
11
+ date = DateUtils.parse(nil)
12
+ expect(date).to be_nil
13
+ end
14
+
15
+ it 'YYYY-MM-DD' do
16
+ date = DateUtils.parse('2014-07-29')
17
+ expected = Date.parse('2014-07-29')
18
+ expect(date).to eq(expected)
19
+ expect(date.to_s).to eq('2014-07-29')
20
+ expect(date.strftime('%Y-%m-%d %H:%M:%S')).to eq('2014-07-29 00:00:00')
21
+ end
22
+
23
+ it 'YYYY-MM-DDTHH:MM:SS' do
24
+ date = DateUtils.parse('2014-07-29T16:08:23')
25
+ expected = DateTime.parse('2014-07-29T16:08:23')
26
+ expect(date).to eq(expected)
27
+ expect(date.to_s).to eq('2014-07-29T16:08:23+00:00')
28
+ expect(date.strftime('%Y-%m-%d %H:%M:%S')).to eq('2014-07-29 16:08:23')
29
+ end
30
+
31
+ it 'YYYY-MM-DDTHH:MM:SSZ' do
32
+ date = DateUtils.parse('2014-07-29T16:08:23Z')
33
+ expected = DateTime.parse('2014-07-29T16:08:23Z')
34
+ expect(date).to eq(expected)
35
+ expect(date.to_s).to eq('2014-07-29T16:08:23+00:00')
36
+ expect(date.strftime('%Y-%m-%d %H:%M:%S')).to eq('2014-07-29 16:08:23')
37
+ end
38
+
39
+ it 'YYYY-MM-DDTHH:MM:SS+HH:MM' do
40
+ date = DateUtils.parse('2014-07-29T16:08:23+02:00')
41
+ expected = DateTime.parse('2014-07-29T16:08:23+02:00')
42
+ expect(date).to eq(expected)
43
+ expect(date.to_s).to eq('2014-07-29T16:08:23+02:00')
44
+ expect(date.strftime('%Y-%m-%d %H:%M:%S')).to eq('2014-07-29 16:08:23')
45
+ end
46
+
47
+ it 'YYYY-MM-DDTHH:MM:SS-HH:MM' do
48
+ date = DateUtils.parse('2014-07-29T16:08:23-02:00')
49
+ expected = DateTime.parse('2014-07-29T16:08:23-02:00')
50
+ expect(date).to eq(expected)
51
+ expect(date.to_s).to eq('2014-07-29T16:08:23-02:00')
52
+ expect(date.strftime('%Y-%m-%d %H:%M:%S')).to eq('2014-07-29 16:08:23')
53
+ end
54
+
55
+ it 'everything else raises an error' do
56
+ expect { DateUtils.parse('xxx') }.to raise_error(ArgumentError)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe AfterShip::Tracking do
4
+ before do
5
+ @client = AfterShip.new(api_key: 'key')
6
+ end
7
+
8
+ context 'Real data' do
9
+ it 'builds delivered Tracking (simple)' do
10
+ tracking = @client.tracking('delivered-ok', 'ups')
11
+ expect(tracking).to be_a(AfterShip::Tracking)
12
+ expect(tracking.checkpoints.size).to eq(15)
13
+ end
14
+
15
+ it 'builds delivered Tracking (quite crazy)' do
16
+ tracking = @client.tracking('delivered-wild', 'ups')
17
+ expect(tracking).to be_a(AfterShip::Tracking)
18
+ expect(tracking.checkpoints.size).to eq(41)
19
+ end
20
+
21
+ it 'builds in-transit Tracking' do
22
+ tracking = @client.tracking('in-transit', 'usps')
23
+ expect(tracking).to be_a(AfterShip::Tracking)
24
+ expect(tracking.checkpoints.size).to eq(27)
25
+ expect(tracking.expected_delivery.to_s).to eq('2014-07-02T00:00:00+00:00')
26
+ end
27
+ end
28
+
29
+ it 'has courier' do
30
+ data = { slug: 'ups' }
31
+ tracking = AfterShip::Tracking.new(data)
32
+ expect(tracking.courier).to eq('UPS')
33
+ end
34
+
35
+ context 'status' do
36
+ it 'Pending' do
37
+ data = { tag: 'Pending' }
38
+ tracking = AfterShip::Tracking.new(data)
39
+ expect(tracking.status).to eq('Pending')
40
+ end
41
+
42
+ it 'InfoReceived' do
43
+ data = { tag: 'InfoReceived' }
44
+ tracking = AfterShip::Tracking.new(data)
45
+ expect(tracking.status).to eq('Info Received')
46
+ end
47
+
48
+ it 'InTransit' do
49
+ data = { tag: 'InTransit' }
50
+ tracking = AfterShip::Tracking.new(data)
51
+ expect(tracking.status).to eq('In Transit')
52
+ end
53
+
54
+ it 'OutForDelivery' do
55
+ data = { tag: 'OutForDelivery' }
56
+ tracking = AfterShip::Tracking.new(data)
57
+ expect(tracking.status).to eq('Out for Delivery')
58
+ end
59
+
60
+ it 'AttemptFail' do
61
+ data = { tag: 'AttemptFail' }
62
+ tracking = AfterShip::Tracking.new(data)
63
+ expect(tracking.status).to eq('Attempt Failed')
64
+ end
65
+
66
+ it 'Delivered' do
67
+ data = { tag: 'Delivered' }
68
+ tracking = AfterShip::Tracking.new(data)
69
+ expect(tracking.status).to eq('Delivered')
70
+ end
71
+
72
+ it 'Exception' do
73
+ data = { tag: 'Exception' }
74
+ tracking = AfterShip::Tracking.new(data)
75
+ expect(tracking.status).to eq('Exception')
76
+ end
77
+
78
+ it 'Expired' do
79
+ data = { tag: 'Expired' }
80
+ tracking = AfterShip::Tracking.new(data)
81
+ expect(tracking.status).to eq('Expired')
82
+ end
83
+
84
+ it 'error' do
85
+ data = { tag: 'error' }
86
+ expect { AfterShip::Tracking.new(data) }.to raise_error(KeyError)
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,133 @@
1
+ # trackings
2
+ WebMock.stub_request(
3
+ :get,
4
+ 'https://api.aftership.com/v3/trackings'
5
+ ).with(
6
+ body: '{}',
7
+ headers: { 'Aftership-Api-Key' => 'key' }
8
+ ).to_return(
9
+ status: 200,
10
+ body: %({"meta":{"code":200},"data":{"trackings":[{"slug":"ups"}]}})
11
+ )
12
+
13
+ # tracking with real delivered data (simple)
14
+ WebMock.stub_request(
15
+ :get,
16
+ 'https://api.aftership.com/v3/trackings/ups/delivered-ok'
17
+ ).with(
18
+ body: '{}',
19
+ headers: { 'Aftership-Api-Key' => 'key' }
20
+ ).to_return(
21
+ status: 200,
22
+ body: File.open('spec/requests/tracking/delivered_ok.json')
23
+ )
24
+
25
+ # tracking with real delivered data (a lot of checkpoints)
26
+ WebMock.stub_request(
27
+ :get,
28
+ 'https://api.aftership.com/v3/trackings/ups/delivered-wild'
29
+ ).with(
30
+ body: '{}',
31
+ headers: { 'Aftership-Api-Key' => 'key' }
32
+ ).to_return(
33
+ status: 200,
34
+ body: File.open('spec/requests/tracking/delivered_wild.json')
35
+ )
36
+
37
+ # tracking with real in-transit data
38
+ WebMock.stub_request(
39
+ :get,
40
+ 'https://api.aftership.com/v3/trackings/usps/in-transit'
41
+ ).with(
42
+ body: '{}',
43
+ headers: { 'Aftership-Api-Key' => 'key' }
44
+ ).to_return(
45
+ status: 200,
46
+ body: File.open('spec/requests/tracking/in_transit.json')
47
+ )
48
+
49
+ # tracking
50
+ WebMock.stub_request(
51
+ :get,
52
+ 'https://api.aftership.com/v3/trackings/ups/ABC123'
53
+ ).with(
54
+ body: '{}',
55
+ headers: { 'Aftership-Api-Key' => 'key' }
56
+ ).to_return(
57
+ status: 200,
58
+ body: %({"meta":{"code":200},"data":{"tracking":{}}})
59
+ )
60
+
61
+ [201, 400, 401, 402, 404, 409, 429, 500, 502, 503, 504, 666].each do |code|
62
+ WebMock.stub_request(
63
+ :get,
64
+ "https://api.aftership.com/v3/trackings/ups/#{ code }"
65
+ ).with(
66
+ body: '{}',
67
+ headers: { 'Aftership-Api-Key' => 'key' }
68
+ ).to_return(
69
+ status: 200,
70
+ body: %({"meta":{"code":#{code}},"data":{"tracking":{"slug":"ups"}}})
71
+ )
72
+ end
73
+
74
+ # create_tracking
75
+ WebMock.stub_request(
76
+ :post,
77
+ 'https://api.aftership.com/v3/trackings'
78
+ ).with(
79
+ body: %({"tracking":{"tracking_number":"ABC123","slug":"ups"}}),
80
+ headers: { 'Aftership-Api-Key' => 'key' }
81
+ ).to_return(
82
+ status: 200,
83
+ body: %({"meta":{"code":200},"data":{"tracking":{"slug":"ups"}}})
84
+ )
85
+
86
+ WebMock.stub_request(
87
+ :post,
88
+ 'https://api.aftership.com/v3/trackings'
89
+ ).with(
90
+ body: %({"tracking":{"tracking_number":"ABC123","slug":"ups","order_id":"1234"}}), # rubocop:disable Metrics/LineLength
91
+ headers: { 'Aftership-Api-Key' => 'key' }
92
+ ).to_return(
93
+ status: 200,
94
+ body: %({"meta":{"code":200},"data":{"tracking":{"slug":"ups"}}})
95
+ )
96
+
97
+ # update_tracking
98
+
99
+ WebMock.stub_request(
100
+ :put,
101
+ 'https://api.aftership.com/v3/trackings/ups/ABC123'
102
+ ).with(
103
+ body: %({"tracking":{"order_id":"1234"}}),
104
+ headers: { 'Aftership-Api-Key' => 'key' }
105
+ ).to_return(
106
+ status: 200,
107
+ body: %({"meta":{"code":200},"data":{"tracking":{"slug":"ups"}}})
108
+ )
109
+
110
+ ## App-specific AfterShip tests
111
+
112
+ # create_tracking
113
+ WebMock.stub_request(
114
+ :post,
115
+ 'https://api.aftership.com/v3/trackings'
116
+ ).with(
117
+ body: %({"tracking":{"tracking_number":"ABC123","slug":"ups","order_id":"1234"}}), # rubocop:disable Metrics/LineLength
118
+ headers: { 'Aftership-Api-Key' => 'key' }
119
+ ).to_return(
120
+ status: 200,
121
+ body: %({"meta":{"code":200},"data":{"tracking":{"slug":"ups"}}})
122
+ )
123
+
124
+ WebMock.stub_request(
125
+ :post,
126
+ 'https://api.aftership.com/v3/trackings'
127
+ ).with(
128
+ body: %({"tracking":{"tracking_number":"EXISTING","slug":"ups","order_id":"1234"}}), # rubocop:disable Metrics/LineLength
129
+ headers: { 'Aftership-Api-Key' => 'key' }
130
+ ).to_return(
131
+ status: 200,
132
+ body: %({"meta":{"code":409}})
133
+ )
@@ -0,0 +1,263 @@
1
+ {
2
+ "meta": {
3
+ "code": 200
4
+ },
5
+ "data": {
6
+ "tracking": {
7
+ "created_at": "2014-05-05T13:50:27+00:00",
8
+ "updated_at": "2014-07-18T08:04:49+00:00",
9
+ "tracking_number": "1ZA2207X6791425225",
10
+ "slug": "ups",
11
+ "active": false,
12
+ "custom_fields": {
13
+ },
14
+ "customer_name": null,
15
+ "destination_country_iso3": "USA",
16
+ "emails": [
17
+
18
+ ],
19
+ "expected_delivery": null,
20
+ "order_id": "PL-55120855",
21
+ "order_id_path": null,
22
+ "origin_country_iso3": "IND",
23
+ "shipment_package_count": 0,
24
+ "shipment_type": "EXPEDITED",
25
+ "signed_by": "FRONT DOOR",
26
+ "smses": [
27
+
28
+ ],
29
+ "source": "api",
30
+ "tag": "Delivered",
31
+ "title": "1ZA2207X6791425225",
32
+ "tracked_count": 29,
33
+ "unique_token": "xJ8CBha3T",
34
+ "checkpoints": [
35
+ {
36
+ "slug": "ups",
37
+ "city": null,
38
+ "created_at": "2014-05-05T13:50:27+00:00",
39
+ "country_name": "IN",
40
+ "message": "BILLING INFORMATION RECEIVED",
41
+ "country_iso3": "IND",
42
+ "tag": "InfoReceived",
43
+ "checkpoint_time": "2014-05-04T14:06:23",
44
+ "coordinates": [
45
+
46
+ ],
47
+ "state": null,
48
+ "zip": null
49
+ },
50
+ {
51
+ "slug": "ups",
52
+ "city": "MUMBAI",
53
+ "created_at": "2014-05-05T18:19:57+00:00",
54
+ "country_name": "IN",
55
+ "message": "PICKUP SCAN",
56
+ "country_iso3": null,
57
+ "tag": "InTransit",
58
+ "checkpoint_time": "2014-05-05T19:56:00",
59
+ "coordinates": [
60
+
61
+ ],
62
+ "state": null,
63
+ "zip": null
64
+ },
65
+ {
66
+ "slug": "ups",
67
+ "city": "MUMBAI",
68
+ "created_at": "2014-05-06T00:30:19+00:00",
69
+ "country_name": "IN",
70
+ "message": "ORIGIN SCAN",
71
+ "country_iso3": null,
72
+ "tag": "InTransit",
73
+ "checkpoint_time": "2014-05-06T03:43:00",
74
+ "coordinates": [
75
+
76
+ ],
77
+ "state": null,
78
+ "zip": null
79
+ },
80
+ {
81
+ "slug": "ups",
82
+ "city": "MUMBAI",
83
+ "created_at": "2014-05-06T00:30:19+00:00",
84
+ "country_name": "IN",
85
+ "message": "DEPARTURE SCAN",
86
+ "country_iso3": null,
87
+ "tag": "InTransit",
88
+ "checkpoint_time": "2014-05-06T05:44:00",
89
+ "coordinates": [
90
+
91
+ ],
92
+ "state": null,
93
+ "zip": null
94
+ },
95
+ {
96
+ "slug": "ups",
97
+ "city": "MUMBAI",
98
+ "created_at": "2014-05-06T04:37:39+00:00",
99
+ "country_name": "IN",
100
+ "message": "ARRIVAL SCAN",
101
+ "country_iso3": null,
102
+ "tag": "InTransit",
103
+ "checkpoint_time": "2014-05-06T06:11:00",
104
+ "coordinates": [
105
+
106
+ ],
107
+ "state": null,
108
+ "zip": null
109
+ },
110
+ {
111
+ "slug": "ups",
112
+ "city": "MUMBAI",
113
+ "created_at": "2014-05-06T13:23:12+00:00",
114
+ "country_name": "IN",
115
+ "message": "DEPARTURE SCAN",
116
+ "country_iso3": null,
117
+ "tag": "InTransit",
118
+ "checkpoint_time": "2014-05-06T15:43:00",
119
+ "coordinates": [
120
+
121
+ ],
122
+ "state": null,
123
+ "zip": null
124
+ },
125
+ {
126
+ "slug": "ups",
127
+ "city": "KOELN (COLOGNE)",
128
+ "created_at": "2014-05-06T21:53:37+00:00",
129
+ "country_name": "DE",
130
+ "message": "ARRIVAL SCAN",
131
+ "country_iso3": null,
132
+ "tag": "InTransit",
133
+ "checkpoint_time": "2014-05-06T20:30:00",
134
+ "coordinates": [
135
+
136
+ ],
137
+ "state": null,
138
+ "zip": null
139
+ },
140
+ {
141
+ "slug": "ups",
142
+ "city": "KOELN (COLOGNE)",
143
+ "created_at": "2014-05-07T04:08:35+00:00",
144
+ "country_name": "DE",
145
+ "message": "DEPARTURE SCAN",
146
+ "country_iso3": null,
147
+ "tag": "InTransit",
148
+ "checkpoint_time": "2014-05-07T05:30:00",
149
+ "coordinates": [
150
+
151
+ ],
152
+ "state": null,
153
+ "zip": null
154
+ },
155
+ {
156
+ "slug": "ups",
157
+ "city": "PHILADELPHIA",
158
+ "created_at": "2014-05-07T13:04:20+00:00",
159
+ "country_name": "US",
160
+ "message": "ARRIVAL SCAN",
161
+ "country_iso3": null,
162
+ "tag": "InTransit",
163
+ "checkpoint_time": "2014-05-07T08:05:00",
164
+ "coordinates": [
165
+
166
+ ],
167
+ "state": "PA",
168
+ "zip": null
169
+ },
170
+ {
171
+ "slug": "ups",
172
+ "city": "PHILADELPHIA",
173
+ "created_at": "2014-05-07T17:36:54+00:00",
174
+ "country_name": "US",
175
+ "message": "IMPORT SCAN",
176
+ "country_iso3": null,
177
+ "tag": "InTransit",
178
+ "checkpoint_time": "2014-05-07T10:31:00",
179
+ "coordinates": [
180
+
181
+ ],
182
+ "state": "PA",
183
+ "zip": null
184
+ },
185
+ {
186
+ "slug": "ups",
187
+ "city": "PHILADELPHIA",
188
+ "created_at": "2014-05-08T08:54:42+00:00",
189
+ "country_name": "US",
190
+ "message": "DEPARTURE SCAN",
191
+ "country_iso3": null,
192
+ "tag": "InTransit",
193
+ "checkpoint_time": "2014-05-08T02:52:00",
194
+ "coordinates": [
195
+
196
+ ],
197
+ "state": "PA",
198
+ "zip": null
199
+ },
200
+ {
201
+ "slug": "ups",
202
+ "city": "PHILADELPHIA",
203
+ "created_at": "2014-05-08T08:54:42+00:00",
204
+ "country_name": "US",
205
+ "message": "ARRIVAL SCAN",
206
+ "country_iso3": null,
207
+ "tag": "InTransit",
208
+ "checkpoint_time": "2014-05-08T03:13:00",
209
+ "coordinates": [
210
+
211
+ ],
212
+ "state": "PA",
213
+ "zip": null
214
+ },
215
+ {
216
+ "slug": "ups",
217
+ "city": "PHILADELPHIA",
218
+ "created_at": "2014-05-08T13:22:52+00:00",
219
+ "country_name": "US",
220
+ "message": "OUT FOR DELIVERY",
221
+ "country_iso3": null,
222
+ "tag": "OutForDelivery",
223
+ "checkpoint_time": "2014-05-08T06:35:00",
224
+ "coordinates": [
225
+
226
+ ],
227
+ "state": "PA",
228
+ "zip": null
229
+ },
230
+ {
231
+ "slug": "ups",
232
+ "city": "PHILADELPHIA",
233
+ "created_at": "2014-05-08T16:41:11+00:00",
234
+ "country_name": "US",
235
+ "message": "DESTINATION SCAN",
236
+ "country_iso3": null,
237
+ "tag": "InTransit",
238
+ "checkpoint_time": "2014-05-08T06:35:00",
239
+ "coordinates": [
240
+
241
+ ],
242
+ "state": "PA",
243
+ "zip": null
244
+ },
245
+ {
246
+ "slug": "ups",
247
+ "city": "PHILADELPHIA",
248
+ "created_at": "2014-05-08T16:41:11+00:00",
249
+ "country_name": "US",
250
+ "message": "DELIVERED",
251
+ "country_iso3": null,
252
+ "tag": "Delivered",
253
+ "checkpoint_time": "2014-05-08T12:31:00",
254
+ "coordinates": [
255
+
256
+ ],
257
+ "state": "PA",
258
+ "zip": "19106"
259
+ }
260
+ ]
261
+ }
262
+ }
263
+ }