after_ship 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,104 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe AfterShip do
4
+ it 'fails to make a client' do
5
+ expect { AfterShip.new }.to raise_error
6
+ end
7
+
8
+ context 'With api_key' do
9
+ let(:client) { AfterShip.new(api_key: 'key') }
10
+
11
+ it 'api_key' do
12
+ expect(client.api_key).to eq('key')
13
+ end
14
+
15
+ it 'debug' do
16
+ AfterShip.debug = true
17
+ expect(AfterShip.debug).to eq(true)
18
+ AfterShip.debug = nil
19
+ end
20
+
21
+ it 'trackings' do
22
+ allow(AfterShip::Request).to receive(:get).and_yield(
23
+ data: {
24
+ trackings: [
25
+ {
26
+ slug: 'ups'
27
+ }
28
+ ]
29
+ }
30
+ )
31
+
32
+ trackings = client.trackings
33
+ expect(trackings.size).to eq(1)
34
+ end
35
+
36
+ it 'tracking' do
37
+ allow(AfterShip::Request).to receive(:get).and_yield(
38
+ data: {
39
+ tracking: {
40
+ slug: 'ups'
41
+ }
42
+ }
43
+ )
44
+
45
+ tracking = client.tracking('tracking-number', 'ups')
46
+ expect(tracking.slug).to eq('ups')
47
+ end
48
+
49
+ it 'create_tracking' do
50
+ allow(AfterShip::Request).to receive(:post).and_yield(
51
+ data: {
52
+ tracking: {
53
+ slug: 'ups',
54
+ order_id: 'order-id'
55
+ }
56
+ }
57
+ )
58
+
59
+ tracking = client.create_tracking(
60
+ 'tracking-number',
61
+ 'ups',
62
+ order_id: 'order-id'
63
+ )
64
+
65
+ expect(tracking.slug).to eq('ups')
66
+ expect(tracking.order_id).to eq('order-id')
67
+ end
68
+
69
+ it 'update_tracking' do
70
+ allow(AfterShip::Request).to receive(:put).and_yield(
71
+ data: {
72
+ tracking: {
73
+ slug: 'ups',
74
+ order_id: 'order-id'
75
+ }
76
+ }
77
+ )
78
+
79
+ tracking = client.update_tracking(
80
+ 'tracking-number',
81
+ 'ups',
82
+ order_id: 'order-id'
83
+ )
84
+
85
+ expect(tracking.slug).to eq('ups')
86
+ expect(tracking.order_id).to eq('order-id')
87
+ end
88
+
89
+ it 'couriers' do
90
+ allow(AfterShip::Request).to receive(:get).and_yield(
91
+ data: {
92
+ couriers: [
93
+ {
94
+ slug: 'ups'
95
+ }
96
+ ]
97
+ }
98
+ )
99
+
100
+ couriers = client.couriers
101
+ expect(couriers.size).to eq(1)
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,135 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe AfterShip::Checkpoint do
4
+ context 'Attributes' do
5
+ it 'created_at' do
6
+ data = { created_at: '2014-10-30T10:05:48' }
7
+ tracking = AfterShip::Checkpoint.new(data)
8
+ expect(tracking.created_at).to be_a(DateTime)
9
+ expect(tracking.created_at.to_s).to eq('2014-10-30T10:05:48+00:00')
10
+ end
11
+
12
+ it 'slug' do
13
+ data = { slug: 'ups' }
14
+ tracking = AfterShip::Checkpoint.new(data)
15
+ expect(tracking.slug).to eq('ups')
16
+ end
17
+
18
+ it 'courier' do
19
+ data = { slug: 'ups' }
20
+ tracking = AfterShip::Checkpoint.new(data)
21
+ expect(tracking.courier).to eq('UPS')
22
+ end
23
+
24
+ it 'checkpoint_time' do
25
+ data = { checkpoint_time: '2014-10-30T10:05:48+00:00' }
26
+ tracking = AfterShip::Checkpoint.new(data)
27
+ expect(tracking.checkpoint_time).to be_a(DateTime)
28
+ expect(tracking.checkpoint_time.to_s)
29
+ .to eq('2014-10-30T10:05:48+00:00')
30
+ end
31
+
32
+ it 'city' do
33
+ data = { city: 'MUMBAI' }
34
+ tracking = AfterShip::Checkpoint.new(data)
35
+ expect(tracking.city).to eq('MUMBAI')
36
+ end
37
+
38
+ it 'country_iso3' do
39
+ data = { country_iso3: 'IND' }
40
+ tracking = AfterShip::Checkpoint.new(data)
41
+ expect(tracking.country_iso3).to eq('IND')
42
+ end
43
+
44
+ it 'country_name' do
45
+ data = { country_name: 'IN' }
46
+ tracking = AfterShip::Checkpoint.new(data)
47
+ expect(tracking.country_name).to eq('IN')
48
+ end
49
+
50
+ it 'message' do
51
+ data = { message: 'PICKUP SCAN' }
52
+ tracking = AfterShip::Checkpoint.new(data)
53
+ expect(tracking.message).to eq('PICKUP SCAN')
54
+ end
55
+
56
+ it 'state' do
57
+ data = { state: 'CA' }
58
+ tracking = AfterShip::Checkpoint.new(data)
59
+ expect(tracking.state).to eq('CA')
60
+ end
61
+
62
+ it 'tag' do
63
+ data = { tag: 'Delivered' }
64
+ tracking = AfterShip::Checkpoint.new(data)
65
+ expect(tracking.tag).to eq('Delivered')
66
+ end
67
+
68
+ it 'status' do
69
+ data = { tag: 'Delivered' }
70
+ tracking = AfterShip::Checkpoint.new(data)
71
+ expect(tracking.status).to eq('Delivered')
72
+ end
73
+
74
+ it 'zip' do
75
+ data = { zip: '94110' }
76
+ tracking = AfterShip::Checkpoint.new(data)
77
+ expect(tracking.zip).to eq('94110')
78
+ end
79
+ end
80
+
81
+ context 'status' do
82
+ it 'Pending' do
83
+ data = { tag: 'Pending' }
84
+ checkpoint = AfterShip::Checkpoint.new(data)
85
+ expect(checkpoint.status).to eq('Pending')
86
+ end
87
+
88
+ it 'InfoReceived' do
89
+ data = { tag: 'InfoReceived' }
90
+ checkpoint = AfterShip::Checkpoint.new(data)
91
+ expect(checkpoint.status).to eq('Info Received')
92
+ end
93
+
94
+ it 'InTransit' do
95
+ data = { tag: 'InTransit' }
96
+ checkpoint = AfterShip::Checkpoint.new(data)
97
+ expect(checkpoint.status).to eq('In Transit')
98
+ end
99
+
100
+ it 'OutForDelivery' do
101
+ data = { tag: 'OutForDelivery' }
102
+ checkpoint = AfterShip::Checkpoint.new(data)
103
+ expect(checkpoint.status).to eq('Out for Delivery')
104
+ end
105
+
106
+ it 'AttemptFail' do
107
+ data = { tag: 'AttemptFail' }
108
+ checkpoint = AfterShip::Checkpoint.new(data)
109
+ expect(checkpoint.status).to eq('Attempt Failed')
110
+ end
111
+
112
+ it 'Delivered' do
113
+ data = { tag: 'Delivered' }
114
+ checkpoint = AfterShip::Checkpoint.new(data)
115
+ expect(checkpoint.status).to eq('Delivered')
116
+ end
117
+
118
+ it 'Exception' do
119
+ data = { tag: 'Exception' }
120
+ checkpoint = AfterShip::Checkpoint.new(data)
121
+ expect(checkpoint.status).to eq('Exception')
122
+ end
123
+
124
+ it 'Expired' do
125
+ data = { tag: 'Expired' }
126
+ checkpoint = AfterShip::Checkpoint.new(data)
127
+ expect(checkpoint.status).to eq('Expired')
128
+ end
129
+
130
+ it 'error' do
131
+ data = { tag: 'error' }
132
+ expect { AfterShip::Checkpoint.new(data) }.to raise_error(KeyError)
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe AfterShip::Courier do
4
+ context 'Attributes' do
5
+ it 'slug' do
6
+ data = { slug: 'ups' }
7
+ tracking = AfterShip::Courier.new(data)
8
+ expect(tracking.slug).to eq('ups')
9
+ end
10
+
11
+ it 'name' do
12
+ data = { name: 'UPS' }
13
+ tracking = AfterShip::Courier.new(data)
14
+ expect(tracking.name).to eq('UPS')
15
+ end
16
+
17
+ it 'phone' do
18
+ data = { phone: '+1 800 742 5877' }
19
+ tracking = AfterShip::Courier.new(data)
20
+ expect(tracking.phone).to eq('+1 800 742 5877')
21
+ end
22
+
23
+ it 'other_name' do
24
+ data = { other_name: 'United Parcel Service' }
25
+ tracking = AfterShip::Courier.new(data)
26
+ expect(tracking.other_name).to eq('United Parcel Service')
27
+ end
28
+
29
+ it 'web_url' do
30
+ data = { web_url: 'http://www.ups.com' }
31
+ tracking = AfterShip::Courier.new(data)
32
+ expect(tracking.web_url).to eq('http://www.ups.com')
33
+ end
34
+
35
+ it 'required_fields' do
36
+ data = { required_fields: [] }
37
+ tracking = AfterShip::Courier.new(data)
38
+ expect(tracking.required_fields).to eq([])
39
+ end
40
+ end
41
+ end
@@ -1,19 +1,27 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe DateUtils do
3
+ RSpec.describe AfterShip::DateUtils do
4
4
  context 'parse' do
5
5
  it 'empty string' do
6
- date = DateUtils.parse('')
6
+ date = AfterShip::DateUtils.parse('')
7
7
  expect(date).to be_nil
8
8
  end
9
9
 
10
10
  it 'nil' do
11
- date = DateUtils.parse(nil)
11
+ date = AfterShip::DateUtils.parse(nil)
12
12
  expect(date).to be_nil
13
13
  end
14
14
 
15
+ it 'YYYYMMDD' do
16
+ date = AfterShip::DateUtils.parse('20140729')
17
+ expected = Date.parse('20140729')
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
+
15
23
  it 'YYYY-MM-DD' do
16
- date = DateUtils.parse('2014-07-29')
24
+ date = AfterShip::DateUtils.parse('2014-07-29')
17
25
  expected = Date.parse('2014-07-29')
18
26
  expect(date).to eq(expected)
19
27
  expect(date.to_s).to eq('2014-07-29')
@@ -21,7 +29,7 @@ RSpec.describe DateUtils do
21
29
  end
22
30
 
23
31
  it 'YYYY-MM-DDTHH:MM:SS' do
24
- date = DateUtils.parse('2014-07-29T16:08:23')
32
+ date = AfterShip::DateUtils.parse('2014-07-29T16:08:23')
25
33
  expected = DateTime.parse('2014-07-29T16:08:23')
26
34
  expect(date).to eq(expected)
27
35
  expect(date.to_s).to eq('2014-07-29T16:08:23+00:00')
@@ -29,7 +37,7 @@ RSpec.describe DateUtils do
29
37
  end
30
38
 
31
39
  it 'YYYY-MM-DDTHH:MM:SSZ' do
32
- date = DateUtils.parse('2014-07-29T16:08:23Z')
40
+ date = AfterShip::DateUtils.parse('2014-07-29T16:08:23Z')
33
41
  expected = DateTime.parse('2014-07-29T16:08:23Z')
34
42
  expect(date).to eq(expected)
35
43
  expect(date.to_s).to eq('2014-07-29T16:08:23+00:00')
@@ -37,7 +45,7 @@ RSpec.describe DateUtils do
37
45
  end
38
46
 
39
47
  it 'YYYY-MM-DDTHH:MM:SS+HH:MM' do
40
- date = DateUtils.parse('2014-07-29T16:08:23+02:00')
48
+ date = AfterShip::DateUtils.parse('2014-07-29T16:08:23+02:00')
41
49
  expected = DateTime.parse('2014-07-29T16:08:23+02:00')
42
50
  expect(date).to eq(expected)
43
51
  expect(date.to_s).to eq('2014-07-29T16:08:23+02:00')
@@ -45,7 +53,7 @@ RSpec.describe DateUtils do
45
53
  end
46
54
 
47
55
  it 'YYYY-MM-DDTHH:MM:SS-HH:MM' do
48
- date = DateUtils.parse('2014-07-29T16:08:23-02:00')
56
+ date = AfterShip::DateUtils.parse('2014-07-29T16:08:23-02:00')
49
57
  expected = DateTime.parse('2014-07-29T16:08:23-02:00')
50
58
  expect(date).to eq(expected)
51
59
  expect(date.to_s).to eq('2014-07-29T16:08:23-02:00')
@@ -53,7 +61,7 @@ RSpec.describe DateUtils do
53
61
  end
54
62
 
55
63
  it 'everything else raises an error' do
56
- expect { DateUtils.parse('xxx') }.to raise_error(ArgumentError)
64
+ expect { AfterShip::DateUtils.parse('xxx') }.to raise_error(ArgumentError)
57
65
  end
58
66
  end
59
67
  end
@@ -0,0 +1,168 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe AfterShip::ErrorHandler do
4
+ it 'precheck' do
5
+ response = double('Typhoeus Response')
6
+ allow(response).to receive(:timed_out?).and_return(false)
7
+ expect { AfterShip::ErrorHandler.precheck(response) }
8
+ .to_not raise_error
9
+ end
10
+
11
+ it 'precheck with timeout raises an error' do
12
+ response = double('Typhoeus Response')
13
+ allow(response).to receive(:timed_out?).and_return(true)
14
+ allow(response).to receive(:effective_url).and_return('test-url')
15
+ expect { AfterShip::ErrorHandler.precheck(response) }
16
+ .to raise_error(AfterShip::Error::Timeout)
17
+ end
18
+
19
+ it 'check with missing code raises an error' do
20
+ meta = {}
21
+ expect { AfterShip::ErrorHandler.check(meta) }
22
+ .to raise_error(KeyError)
23
+ end
24
+
25
+ it 'check with success code' do
26
+ meta = { code: 200 }
27
+ expect { AfterShip::ErrorHandler.check(meta) }
28
+ .to_not raise_error
29
+ end
30
+
31
+ it 'check with error code' do
32
+ meta = { code: 400 }
33
+ expect { AfterShip::ErrorHandler.check(meta) }
34
+ .to raise_error(AfterShip::Error::BadRequest)
35
+ end
36
+
37
+ context 'error_class_for' do
38
+ it '400' do
39
+ expect(AfterShip::ErrorHandler.error_class_for(400))
40
+ .to eq(AfterShip::Error::BadRequest)
41
+ end
42
+
43
+ it '4001' do
44
+ expect(AfterShip::ErrorHandler.error_class_for(4001))
45
+ .to eq(AfterShip::Error::InvalidJsonData)
46
+ end
47
+
48
+ it '4002' do
49
+ expect(AfterShip::ErrorHandler.error_class_for(4002))
50
+ .to eq(AfterShip::Error::InvalidJsonData)
51
+ end
52
+
53
+ it '4003' do
54
+ expect(AfterShip::ErrorHandler.error_class_for(4003))
55
+ .to eq(AfterShip::Error::TrackingAlreadyExists)
56
+ end
57
+
58
+ it '4004' do
59
+ expect(AfterShip::ErrorHandler.error_class_for(4004))
60
+ .to eq(AfterShip::Error::TrackingDoesNotExist)
61
+ end
62
+
63
+ it '4005' do
64
+ expect(AfterShip::ErrorHandler.error_class_for(4005))
65
+ .to eq(AfterShip::Error::TrackingNumberInvalid)
66
+ end
67
+
68
+ it '4006' do
69
+ expect(AfterShip::ErrorHandler.error_class_for(4006))
70
+ .to eq(AfterShip::Error::TrackingObjectRequired)
71
+ end
72
+
73
+ it '4007' do
74
+ expect(AfterShip::ErrorHandler.error_class_for(4007))
75
+ .to eq(AfterShip::Error::TrackingNumberRequired)
76
+ end
77
+
78
+ it '4008' do
79
+ expect(AfterShip::ErrorHandler.error_class_for(4008))
80
+ .to eq(AfterShip::Error::FieldInvalid)
81
+ end
82
+
83
+ it '4009' do
84
+ expect(AfterShip::ErrorHandler.error_class_for(4009))
85
+ .to eq(AfterShip::Error::FieldRequired)
86
+ end
87
+
88
+ it '4010' do
89
+ expect(AfterShip::ErrorHandler.error_class_for(4010))
90
+ .to eq(AfterShip::Error::SlugInvalid)
91
+ end
92
+
93
+ it '4011' do
94
+ expect(AfterShip::ErrorHandler.error_class_for(4011))
95
+ .to eq(AfterShip::Error::CourierFieldInvalid)
96
+ end
97
+
98
+ it '4012' do
99
+ expect(AfterShip::ErrorHandler.error_class_for(4012))
100
+ .to eq(AfterShip::Error::CourierNotDetected)
101
+ end
102
+
103
+ it '4013' do
104
+ expect(AfterShip::ErrorHandler.error_class_for(4013))
105
+ .to eq(AfterShip::Error::RetrackNotAllowed)
106
+ end
107
+
108
+ it '4016' do
109
+ expect(AfterShip::ErrorHandler.error_class_for(4016))
110
+ .to eq(AfterShip::Error::RetrackNotAllowed)
111
+ end
112
+
113
+ it '4014' do
114
+ expect(AfterShip::ErrorHandler.error_class_for(4014))
115
+ .to eq(AfterShip::Error::NotificationRequired)
116
+ end
117
+
118
+ it '4015' do
119
+ expect(AfterShip::ErrorHandler.error_class_for(4015))
120
+ .to eq(AfterShip::Error::IdInvalid)
121
+ end
122
+
123
+ it '401' do
124
+ expect(AfterShip::ErrorHandler.error_class_for(401))
125
+ .to eq(AfterShip::Error::Unauthorized)
126
+ end
127
+
128
+ it '403' do
129
+ expect(AfterShip::ErrorHandler.error_class_for(403))
130
+ .to eq(AfterShip::Error::Forbidden)
131
+ end
132
+
133
+ it '404' do
134
+ expect(AfterShip::ErrorHandler.error_class_for(404))
135
+ .to eq(AfterShip::Error::NotFound)
136
+ end
137
+
138
+ it '429' do
139
+ expect(AfterShip::ErrorHandler.error_class_for(429))
140
+ .to eq(AfterShip::Error::TooManyRequests)
141
+ end
142
+
143
+ it '500' do
144
+ expect(AfterShip::ErrorHandler.error_class_for(500))
145
+ .to eq(AfterShip::Error::InternalError)
146
+ end
147
+
148
+ it '502' do
149
+ expect(AfterShip::ErrorHandler.error_class_for(502))
150
+ .to eq(AfterShip::Error::InternalError)
151
+ end
152
+
153
+ it '503' do
154
+ expect(AfterShip::ErrorHandler.error_class_for(503))
155
+ .to eq(AfterShip::Error::InternalError)
156
+ end
157
+
158
+ it '504' do
159
+ expect(AfterShip::ErrorHandler.error_class_for(504))
160
+ .to eq(AfterShip::Error::InternalError)
161
+ end
162
+
163
+ it '666' do
164
+ expect(AfterShip::ErrorHandler.error_class_for(666))
165
+ .to eq(AfterShip::Error::UnknownError)
166
+ end
167
+ end
168
+ end