shipcloud 0.6.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +5 -5
  2. data/.codeclimate.yml +1 -0
  3. data/.hound.yml +1 -1
  4. data/.rubocop.yml +713 -2
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +18 -6
  7. data/CHANGELOG.md +70 -0
  8. data/README.md +76 -8
  9. data/bin/rubocop +29 -0
  10. data/install-cc-test-reporter.sh +4 -0
  11. data/lib/shipcloud.rb +28 -14
  12. data/lib/shipcloud/address.rb +2 -1
  13. data/lib/shipcloud/operations/all.rb +12 -2
  14. data/lib/shipcloud/operations/create.rb +10 -2
  15. data/lib/shipcloud/operations/delete.rb +10 -2
  16. data/lib/shipcloud/operations/find.rb +10 -2
  17. data/lib/shipcloud/operations/update.rb +12 -4
  18. data/lib/shipcloud/pickup_request.rb +12 -0
  19. data/lib/shipcloud/request/base.rb +14 -10
  20. data/lib/shipcloud/request/connection.rb +18 -12
  21. data/lib/shipcloud/request/info.rb +7 -5
  22. data/lib/shipcloud/shipcloud_error.rb +70 -0
  23. data/lib/shipcloud/shipment.rb +3 -2
  24. data/lib/shipcloud/tracker.rb +13 -0
  25. data/lib/shipcloud/version.rb +1 -1
  26. data/lib/shipcloud/webhook.rb +2 -1
  27. data/shipcloud.gemspec +9 -8
  28. data/spec/shipcloud/address_spec.rb +114 -43
  29. data/spec/shipcloud/carrier_spec.rb +12 -5
  30. data/spec/shipcloud/pickup_request_spec.rb +136 -0
  31. data/spec/shipcloud/request/base_spec.rb +51 -13
  32. data/spec/shipcloud/request/connection_spec.rb +3 -3
  33. data/spec/shipcloud/request/info_spec.rb +34 -0
  34. data/spec/shipcloud/shipcloud_error_spec.rb +125 -0
  35. data/spec/shipcloud/shipment_quote_spec.rb +14 -1
  36. data/spec/shipcloud/shipment_spec.rb +126 -11
  37. data/spec/shipcloud/tracker_spec.rb +141 -0
  38. data/spec/shipcloud/webhooks_spec.rb +70 -6
  39. data/spec/shipcloud_spec.rb +82 -20
  40. data/spec/spec_helper.rb +2 -2
  41. metadata +55 -43
  42. data/.ruby-style.yml +0 -240
  43. data/lib/shipcloud/request/validator.rb +0 -33
  44. data/spec/shipcloud/request/validator_spec.rb +0 -24
@@ -18,7 +18,36 @@ describe Shipcloud::Shipment do
18
18
  length: 40,
19
19
  width: 20,
20
20
  height: 20
21
- }
21
+ },
22
+ pickup: {
23
+ pickup_time: {
24
+ earliest: "2015-09-15T09:00:00+02:00",
25
+ latest: "2015-09-15T18:00:00+02:00"
26
+ },
27
+ pickup_address: {
28
+ company: "Sender Ltd.",
29
+ first_name: "Jane",
30
+ last_name: "Doe",
31
+ street: "Musterstraße",
32
+ street_no: "42",
33
+ zip_code: "54321",
34
+ city: "Musterstadt",
35
+ country: "DE"
36
+ },
37
+ },
38
+ metadata: {
39
+ product: {
40
+ name: "foo"
41
+ },
42
+ category: {
43
+ id: "123456",
44
+ name: "bar"
45
+ }
46
+ },
47
+ customs_declaration: {
48
+ id: "123456",
49
+ contents_type: "commercial_goods",
50
+ },
22
51
  }
23
52
  end
24
53
 
@@ -42,41 +71,119 @@ describe Shipcloud::Shipment do
42
71
  expect(shipment.package[:length]).to eq 40
43
72
  expect(shipment.package[:width]).to eq 20
44
73
  expect(shipment.package[:height]).to eq 20
74
+
75
+ expect(shipment.pickup[:pickup_time][:earliest]).to eq "2015-09-15T09:00:00+02:00"
76
+ expect(shipment.pickup[:pickup_time][:latest]).to eq "2015-09-15T18:00:00+02:00"
77
+ expect(shipment.pickup[:pickup_address][:company]).to eq "Sender Ltd."
78
+ expect(shipment.pickup[:pickup_address][:first_name]).to eq "Jane"
79
+ expect(shipment.pickup[:pickup_address][:last_name]).to eq "Doe"
80
+ expect(shipment.pickup[:pickup_address][:street]).to eq "Musterstraße"
81
+ expect(shipment.pickup[:pickup_address][:street_no]).to eq "42"
82
+ expect(shipment.pickup[:pickup_address][:zip_code]).to eq "54321"
83
+ expect(shipment.pickup[:pickup_address][:city]).to eq "Musterstadt"
84
+ expect(shipment.pickup[:pickup_address][:country]).to eq "DE"
85
+
86
+ expect(shipment.customs_declaration[:id]).to eq "123456"
87
+ expect(shipment.customs_declaration[:contents_type]).to eq "commercial_goods"
88
+ end
89
+
90
+ it "initializes the metadata correctly" do
91
+ metadata = {
92
+ category: {
93
+ id: "123456",
94
+ name: "bar"
95
+ },
96
+ product: {
97
+ name: "foo"
98
+ }
99
+ }
100
+
101
+ expect(shipment.metadata).to eq metadata
45
102
  end
46
103
  end
47
104
 
48
105
  describe ".create" do
49
106
  it "makes a new POST request using the correct API endpoint" do
50
- Shipcloud.should_receive(:request).with(:post, "shipments", valid_attributes).and_return("data" => {})
107
+ expect(Shipcloud).to receive(:request).
108
+ with(:post, "shipments", valid_attributes, api_key: nil, affiliate_id: nil).
109
+ and_return("data" => {})
51
110
  Shipcloud::Shipment.create(valid_attributes)
52
111
  end
112
+
113
+ it "use the affiliate ID provided for the request" do
114
+ expect(Shipcloud).to receive(:request).
115
+ with(
116
+ :post,
117
+ "shipments",
118
+ valid_attributes,
119
+ api_key: nil,
120
+ affiliate_id: "affiliate_id",
121
+ ).and_return("data" => {})
122
+ Shipcloud::Shipment.create(valid_attributes, affiliate_id: "affiliate_id")
123
+ end
53
124
  end
54
125
 
55
126
  describe ".find" do
56
127
  it "makes a new GET request using the correct API endpoint to receive a specific subscription" do
57
- Shipcloud.should_receive(:request).with(:get, "shipments/123", {}).and_return("id" => "123")
128
+ expect(Shipcloud).to receive(:request).
129
+ with(:get, "shipments/123", {}, api_key: nil, affiliate_id: nil).
130
+ and_return("id" => "123")
58
131
  Shipcloud::Shipment.find("123")
59
132
  end
133
+
134
+ it "use the affiliate ID provided for the request" do
135
+ expect(Shipcloud).to receive(:request).with(
136
+ :get, "shipments/123", {}, api_key: nil, affiliate_id: "affiliate_id"
137
+ ).and_return("id" => "123")
138
+ Shipcloud::Shipment.find("123", affiliate_id: "affiliate_id")
139
+ end
60
140
  end
61
141
 
62
142
  describe ".update" do
63
143
  it "makes a new PUT request using the correct API endpoint" do
64
- Shipcloud.should_receive(:request).with(:put, "shipments/123", {:carrier => 'ups' }).and_return("data" => {})
65
- Shipcloud::Shipment.update("123", {:carrier => 'ups' })
144
+ expect(Shipcloud).to receive(:request).
145
+ with(:put, "shipments/123", { carrier: "ups" }, api_key: nil, affiliate_id: nil).
146
+ and_return("data" => {})
147
+ Shipcloud::Shipment.update("123", carrier: "ups")
148
+ end
149
+
150
+ it "use the affiliate ID provided for the request" do
151
+ expect(Shipcloud).to receive(:request).
152
+ with(
153
+ :put, "shipments/123", { carrier: "ups" }, api_key: nil, affiliate_id: "affiliate_id"
154
+ ).and_return("data" => {})
155
+ Shipcloud::Shipment.update("123", { carrier: "ups" }, affiliate_id: "affiliate_id")
66
156
  end
67
157
  end
68
158
 
69
159
  describe ".delete" do
70
160
  it "makes a new DELETE request using the correct API endpoint" do
71
- Shipcloud.should_receive(:request).with(:delete, "shipments/123", {}).and_return(true)
161
+ expect(Shipcloud).to receive(:request).
162
+ with(:delete, "shipments/123", {}, api_key: nil, affiliate_id: nil).
163
+ and_return(true)
72
164
  Shipcloud::Shipment.delete("123")
73
165
  end
166
+
167
+ it "doesn't raise an error" do
168
+ stub_request(:delete, "https://api.shipcloud.io/v1/shipments/123").
169
+ to_return(status: 204, body: "")
170
+
171
+ expect { Shipcloud::Shipment.delete("123", api_key: "your-api-key") }.
172
+ to_not raise_error
173
+ end
174
+
175
+ it "use the affiliate ID provided for the request" do
176
+ expect(Shipcloud).to receive(:request).with(
177
+ :delete, "shipments/123", {}, api_key: nil, affiliate_id: "affiliate_id"
178
+ ).and_return(true)
179
+ Shipcloud::Shipment.delete("123", affiliate_id: "affiliate_id")
180
+ end
74
181
  end
75
182
 
76
183
  describe ".all" do
77
184
  it "makes a new Get request using the correct API endpoint" do
78
185
  expect(Shipcloud).to receive(:request).
79
- with(:get, "shipments", {}).
186
+ with(:get, "shipments", {}, api_key: nil, affiliate_id: nil).
80
187
  and_return("shipments" => [])
81
188
 
82
189
  Shipcloud::Shipment.all
@@ -104,16 +211,24 @@ describe Shipcloud::Shipment do
104
211
  }
105
212
 
106
213
  expect(Shipcloud).to receive(:request).
107
- with(:get, "shipments", filter).
214
+ with(:get, "shipments", filter, api_key: nil, affiliate_id: nil).
108
215
  and_return("shipments" => shipments_array)
109
216
 
110
- Shipcloud::Shipment.all(filter)
217
+ Shipcloud::Shipment.all(filter, api_key: nil)
218
+ end
219
+
220
+ it "use the affiliate ID provided for the request" do
221
+ expect(Shipcloud).to receive(:request).
222
+ with(:get, "shipments", {}, api_key: nil, affiliate_id: "affiliate_id").
223
+ and_return("shipments" => [])
224
+
225
+ Shipcloud::Shipment.all(affiliate_id: "affiliate_id")
111
226
  end
112
227
  end
113
228
 
114
- def stub_shipments_requests
229
+ def stub_shipments_requests(affiliate_id: nil)
115
230
  allow(Shipcloud).to receive(:request).
116
- with(:get, "shipments", {}).
231
+ with(:get, "shipments", {}, api_key: nil, affiliate_id: affiliate_id).
117
232
  and_return("shipments" => shipments_array)
118
233
  end
119
234
 
@@ -0,0 +1,141 @@
1
+ # frozen_string_literal: true
2
+ require "spec_helper"
3
+
4
+ describe Shipcloud::PickupRequest do
5
+ valid_attributes = {
6
+ carrier: "ups",
7
+ carrier_tracking_no: "723558934169",
8
+ }
9
+
10
+ describe "#initialize" do
11
+ it "initializes all attributes correctly" do
12
+ tracker = Shipcloud::Tracker.new(valid_attributes)
13
+
14
+ expect(tracker.carrier).to eq "ups"
15
+ expect(tracker.carrier_tracking_no).to eq "723558934169"
16
+ end
17
+ end
18
+
19
+ describe ".all" do
20
+ it "makes a new GET request using the correct API endpoint" do
21
+ expect(Shipcloud).to receive(:request).
22
+ with(:get, "trackers", {}, api_key: nil, affiliate_id: nil).
23
+ and_return("trackers" => [])
24
+
25
+ Shipcloud::Tracker.all
26
+ end
27
+
28
+ it "returns a list of Tracker objects" do
29
+ stub_trackers_requests
30
+
31
+ trackers = Shipcloud::Tracker.all
32
+
33
+ trackers.each do |tracker|
34
+ expect(tracker).to be_a Shipcloud::Tracker
35
+ end
36
+ end
37
+
38
+ it "use the affiliate ID provided for the request" do
39
+ expect(Shipcloud).to receive(:request).
40
+ with(:get, "trackers", {}, api_key: nil, affiliate_id: "affiliate_id").
41
+ and_return("trackers" => [])
42
+
43
+ Shipcloud::Tracker.all(affiliate_id: "affiliate_id")
44
+ end
45
+ end
46
+
47
+ describe ".create" do
48
+ it "makes a new POST request using the correct API endpoint" do
49
+ expect(Shipcloud).to receive(:request).
50
+ with(:post, "trackers", valid_attributes, api_key: nil, affiliate_id: nil).
51
+ and_return("data" => {})
52
+
53
+ Shipcloud::Tracker.create(valid_attributes)
54
+ end
55
+
56
+ it "use the affiliate ID provided for the request" do
57
+ expect(Shipcloud).to receive(:request).
58
+ with(:post, "trackers", valid_attributes, api_key: nil, affiliate_id: "affiliate_id").
59
+ and_return("data" => {})
60
+
61
+ Shipcloud::Tracker.create(valid_attributes, affiliate_id: "affiliate_id")
62
+ end
63
+ end
64
+
65
+ describe ".find" do
66
+ it "makes a new GET request using the correct API endpoint to receive a specific tracker" do
67
+ expect(Shipcloud).to receive(:request).
68
+ with(:get, "trackers/123", {}, api_key: nil, affiliate_id: nil).
69
+ and_return("id" => "123")
70
+
71
+ Shipcloud::Tracker.find("123")
72
+ end
73
+
74
+ it "use the affiliate ID provided for the request" do
75
+ expect(Shipcloud).to receive(:request).
76
+ with(:get, "trackers/123", {}, api_key: nil, affiliate_id: "affiliate_id").
77
+ and_return("id" => "123")
78
+
79
+ Shipcloud::Tracker.find("123", affiliate_id: "affiliate_id")
80
+ end
81
+ end
82
+
83
+ def stub_trackers_requests(affiliate_id: nil)
84
+ allow(Shipcloud).to receive(:request).
85
+ with(:get, "trackers", {}, api_key: nil, affiliate_id: affiliate_id).
86
+ and_return("trackers" => trackers_array)
87
+ end
88
+
89
+ def trackers_array
90
+ [
91
+ {
92
+ "id" => "4a6922e2-09ad-4724-807c-7b4e572d3c6b",
93
+ "carrier_tracking_no" => "723558934169",
94
+ "status" => "registered",
95
+ "created_at" => "2015-07-21T09:35:23+02:00",
96
+ "to" => {
97
+ "company" => nil,
98
+ "first_name" => "Hans",
99
+ "last_name" => "Meier",
100
+ "care_of" => nil,
101
+ "street" => "Semmelweg",
102
+ "street_no" => "1",
103
+ "zip_code" => "12345",
104
+ "city" => "Hamburg",
105
+ "state" => nil,
106
+ "country" => "DE",
107
+ },
108
+ "tracking_status_updated_at" => nil,
109
+ "last_polling_at" => nil,
110
+ "next_polling_at" => "2015-07-21T09:35:23+02:00",
111
+ "shipment_id" => "123456",
112
+ "carrier" => "ups",
113
+ "tracking_events" => [],
114
+ },
115
+ {
116
+ "id" => "7b4e572d3c6b-4a6922e2-09ad-4724-807c",
117
+ "carrier_tracking_no" => "723558934170",
118
+ "status" => "registered",
119
+ "created_at" => "2015-07-20T09:35:23+02:00",
120
+ "to" => {
121
+ "company" => nil,
122
+ "first_name" => "Rita",
123
+ "last_name" => "Rose",
124
+ "care_of" => nil,
125
+ "street" => "Geranienweg",
126
+ "street_no" => "2",
127
+ "zip_code" => "23456",
128
+ "city" => "Berlin",
129
+ "state" => nil,
130
+ "country" => "DE",
131
+ },
132
+ "tracking_status_updated_at" => nil,
133
+ "last_polling_at" => nil,
134
+ "next_polling_at" => "2015-07-21T09:35:23+02:00",
135
+ "shipment_id" => "654321",
136
+ "carrier" => "dpd",
137
+ "tracking_events" => [],
138
+ },
139
+ ]
140
+ end
141
+ end
@@ -2,6 +2,7 @@ require "spec_helper"
2
2
 
3
3
  describe Shipcloud::Webhook do
4
4
  valid_attributes = {
5
+ id: "583cfd8b-77c7-4447-a3a0-1568bb9cc553",
5
6
  url: "https://example.com/webhook",
6
7
  event_types: ["shipment.tracking.delayed", "shipment.tracking.delivered"]
7
8
  }
@@ -11,29 +12,46 @@ describe Shipcloud::Webhook do
11
12
  webhook = Shipcloud::Webhook.new(valid_attributes)
12
13
  expect(webhook.url).to eq "https://example.com/webhook"
13
14
  expect(webhook.event_types).to eq ["shipment.tracking.delayed", "shipment.tracking.delivered"]
15
+ expect(webhook.id).to eq "583cfd8b-77c7-4447-a3a0-1568bb9cc553"
14
16
  end
15
17
  end
16
18
 
17
19
  describe ".create" do
18
20
  it "makes a new POST request using the correct API endpoint" do
19
- Shipcloud.should_receive(:request).
20
- with(:post, "webhooks", valid_attributes).
21
+ expect(Shipcloud).to receive(:request).
22
+ with(:post, "webhooks", valid_attributes, api_key: nil, affiliate_id: nil).
21
23
  and_return("data" => {})
22
24
  Shipcloud::Webhook.create(valid_attributes)
23
25
  end
26
+
27
+ it "use the affiliate ID provided for the request" do
28
+ expect(Shipcloud).to receive(:request).
29
+ with(:post, "webhooks", valid_attributes, api_key: nil, affiliate_id: "affiliate_id").
30
+ and_return("data" => {})
31
+ Shipcloud::Webhook.create(valid_attributes, affiliate_id: "affiliate_id")
32
+ end
24
33
  end
25
34
 
26
35
  describe ".find" do
27
36
  it "makes a new GET request using the correct API endpoint to receive a specific webhook" do
28
- Shipcloud.should_receive(:request).with(:get, "webhooks/123", {}).and_return("id" => "123")
37
+ expect(Shipcloud).to receive(:request).
38
+ with(:get, "webhooks/123", {}, api_key: nil, affiliate_id: nil).
39
+ and_return("id" => "123")
29
40
  Shipcloud::Webhook.find("123")
30
41
  end
42
+
43
+ it "use the affiliate ID provided for the request" do
44
+ expect(Shipcloud).to receive(:request).
45
+ with(:get, "webhooks/123", {}, api_key: nil, affiliate_id: "affiliate_id").
46
+ and_return("id" => "123")
47
+ Shipcloud::Webhook.find("123", affiliate_id: "affiliate_id")
48
+ end
31
49
  end
32
50
 
33
51
  describe ".all" do
34
52
  it "makes a new Get request using the correct API endpoint" do
35
53
  expect(Shipcloud).to receive(:request).
36
- with(:get, "webhooks", {}).
54
+ with(:get, "webhooks", {}, api_key: nil, affiliate_id: nil).
37
55
  and_return("webhooks" => [])
38
56
 
39
57
  Shipcloud::Webhook.all
@@ -48,11 +66,57 @@ describe Shipcloud::Webhook do
48
66
  expect(webhook).to be_a Shipcloud::Webhook
49
67
  end
50
68
  end
69
+
70
+ it "use the affiliate ID provided for the request" do
71
+ expect(Shipcloud).to receive(:request).
72
+ with(:get, "webhooks", {}, api_key: nil, affiliate_id: "affiliate_id").
73
+ and_return("webhooks" => [])
74
+
75
+ Shipcloud::Webhook.all(affiliate_id: "affiliate_id")
76
+ end
77
+ end
78
+
79
+ describe ".delete" do
80
+ it "makes a DELETE request using the correct API endpoint" do
81
+ expect(Shipcloud).to receive(:request).
82
+ with(:delete, "webhooks/123", {}, api_key: nil, affiliate_id: nil).
83
+ and_return(true)
84
+ Shipcloud::Webhook.delete("123")
85
+ end
86
+
87
+ it "use the affiliate ID provided for the request" do
88
+ expect(Shipcloud).to receive(:request).
89
+ with(:delete, "webhooks/123", {}, api_key: nil, affiliate_id: "affiliate_id").
90
+ and_return(true)
91
+ Shipcloud::Webhook.delete("123", affiliate_id: "affiliate_id")
92
+ end
93
+ end
94
+
95
+ describe ".deactivated" do
96
+ let(:id) { "123" }
97
+
98
+ subject { Shipcloud::Webhook.find(id).deactivated }
99
+
100
+ before do
101
+ expect(Shipcloud).to receive(:request).
102
+ with(:get, "webhooks/#{id}", {}, api_key: nil, affiliate_id: nil).
103
+ and_return("id" => id, "deactivated" => deactivated)
104
+ end
105
+
106
+ context "when the webhook is deactivated" do
107
+ let(:deactivated) { true }
108
+ it { is_expected.to be true }
109
+ end
110
+
111
+ context "when the webhook is not deactivated" do
112
+ let(:deactivated) { false }
113
+ it { is_expected.to be false }
114
+ end
51
115
  end
52
116
 
53
- def stub_webhooks_request
117
+ def stub_webhooks_request(affiliate_id: nil)
54
118
  allow(Shipcloud).to receive(:request).
55
- with(:get, "webhooks", {}).
119
+ with(:get, "webhooks", {}, api_key: nil, affiliate_id: affiliate_id).
56
120
  and_return("webhooks" => webhooks_array)
57
121
  end
58
122