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
@@ -24,7 +24,7 @@ describe Shipcloud::Carrier do
24
24
  describe '.all' do
25
25
  it 'makes a new Get request using the correct API endpoint' do
26
26
  expect(Shipcloud).to receive(:request).
27
- with(:get, 'carriers', {}).and_return([])
27
+ with(:get, "carriers", {}, api_key: nil, affiliate_id: nil).and_return([])
28
28
 
29
29
  Shipcloud::Carrier.all
30
30
  end
@@ -66,12 +66,19 @@ describe Shipcloud::Carrier do
66
66
  )
67
67
  )
68
68
  end
69
+
70
+ it "use the affiliate ID provided for the request" do
71
+ expect(Shipcloud).to receive(:request).
72
+ with(:get, "carriers", {}, api_key: nil, affiliate_id: "affiliate_id").and_return([])
73
+
74
+ Shipcloud::Carrier.all(affiliate_id: "affiliate_id")
75
+ end
69
76
  end
70
77
 
71
- def stub_carriers_request
72
- allow(Shipcloud).to receive(:request)
73
- .with(:get, 'carriers', {})
74
- .and_return(
78
+ def stub_carriers_request(affiliate_id: nil)
79
+ allow(Shipcloud).to receive(:request).
80
+ with(:get, "carriers", {}, api_key: nil, affiliate_id: affiliate_id).
81
+ and_return(
75
82
  [
76
83
  {
77
84
  'name' => 'carrier_1',
@@ -0,0 +1,136 @@
1
+ require "spec_helper"
2
+
3
+ describe Shipcloud::PickupRequest do
4
+ valid_attributes = {
5
+ carrier: "dpd",
6
+ pickup_time: {
7
+ earliest: "2015-09-15T09:00:00+02:00",
8
+ latest: "2015-09-15T18:00:00+02:00"
9
+ },
10
+ shipments: [
11
+ { id: "abc_123" }
12
+ ]
13
+ }
14
+
15
+ valid_attributes_with_pickup_address = {
16
+ carrier: "dpd",
17
+ pickup_time: {
18
+ earliest: "2015-09-15T09:00:00+02:00",
19
+ latest: "2015-09-15T18:00:00+02:00"
20
+ },
21
+ pickup_address: {
22
+ company: "Muster-Company",
23
+ first_name: "Max",
24
+ last_name: "Mustermann",
25
+ street: "Musterstraße",
26
+ street_no: "42",
27
+ zip_code: "54321",
28
+ city: "Musterstadt",
29
+ country: "DE",
30
+ phone: "555-555"
31
+ },
32
+ shipments: [
33
+ { id: "abc_123" }
34
+ ]
35
+ }
36
+
37
+ describe "#initialize" do
38
+ it "initializes all attributes correctly" do
39
+ pickup_request = Shipcloud::PickupRequest.new(valid_attributes_with_pickup_address)
40
+
41
+ expect(pickup_request.carrier).to eq "dpd"
42
+ expect(pickup_request.pickup_time[:earliest]).to eq "2015-09-15T09:00:00+02:00"
43
+ expect(pickup_request.pickup_time[:latest]).to eq "2015-09-15T18:00:00+02:00"
44
+ expect(pickup_request.pickup_address[:company]).to eq "Muster-Company"
45
+ expect(pickup_request.pickup_address[:first_name]).to eq "Max"
46
+ expect(pickup_request.pickup_address[:last_name]).to eq "Mustermann"
47
+ expect(pickup_request.pickup_address[:street]).to eq "Musterstraße"
48
+ expect(pickup_request.pickup_address[:street_no]).to eq "42"
49
+ expect(pickup_request.pickup_address[:zip_code]).to eq "54321"
50
+ expect(pickup_request.pickup_address[:country]).to eq "DE"
51
+ expect(pickup_request.pickup_address[:phone]).to eq "555-555"
52
+ expect(pickup_request.shipments).to eq [{ id: "abc_123" }]
53
+ end
54
+ end
55
+
56
+ describe ".create" do
57
+ it "makes a new POST request using the correct API endpoint" do
58
+ expect(Shipcloud).to receive(:request).
59
+ with(:post, "pickup_requests", valid_attributes, api_key: nil, affiliate_id: nil).
60
+ and_return("data" => {})
61
+
62
+ Shipcloud::PickupRequest.create(valid_attributes)
63
+ end
64
+
65
+ it "returns pickup request id" do
66
+ allow(Shipcloud).to receive(:request).
67
+ and_return(
68
+ "id" => "123456",
69
+ "carrier_pickup_number" => "abcdef",
70
+ "carrier" => "dpd",
71
+ "pickup_time" => {
72
+ "earliest" => "2015-09-15T09:00:00+02:00",
73
+ "latest" => "2015-09-15T18:00:00+02:00"
74
+ }
75
+ )
76
+
77
+ pickup_request = Shipcloud::PickupRequest.create(valid_attributes)
78
+
79
+ expect(pickup_request.id).to eq "123456"
80
+ expect(pickup_request.carrier_pickup_number).to eq "abcdef"
81
+ end
82
+
83
+ it "returns a pickup_address" do
84
+ allow(Shipcloud).to receive(:request).
85
+ and_return(
86
+ "id" => "123456",
87
+ "carrier_pickup_number" => "abcdef",
88
+ "carrier" => "dpd",
89
+ "pickup_time" => {
90
+ "earliest" => "2015-09-15T09:00:00+02:00",
91
+ "latest" => "2015-09-15T18:00:00+02:00"
92
+ },
93
+ "pickup_address" => {
94
+ "id" => "522a7cb1-d6c8-418c-ac26-011127ab5bbe",
95
+ "company" => "Muster-Company",
96
+ "first_name" => "Max",
97
+ "last_name" => "Mustermann",
98
+ "street" => "Musterstraße",
99
+ "street_no" => "42",
100
+ "zip_code" => "54321",
101
+ "city" => "Musterstadt",
102
+ "country" => "DE",
103
+ "phone" => "555-555"
104
+ }
105
+ )
106
+
107
+ pickup_request = Shipcloud::PickupRequest.create(valid_attributes_with_pickup_address)
108
+
109
+ expect(pickup_request.pickup_address).to eq(
110
+ "id" => "522a7cb1-d6c8-418c-ac26-011127ab5bbe",
111
+ "company" => "Muster-Company",
112
+ "first_name" => "Max",
113
+ "last_name" => "Mustermann",
114
+ "street" => "Musterstraße",
115
+ "street_no" => "42",
116
+ "zip_code" => "54321",
117
+ "city" => "Musterstadt",
118
+ "country" => "DE",
119
+ "phone" => "555-555"
120
+ )
121
+ end
122
+
123
+ it "use the affiliate ID provided for the request" do
124
+ expect(Shipcloud).to receive(:request).
125
+ with(
126
+ :post,
127
+ "pickup_requests",
128
+ valid_attributes,
129
+ api_key: nil,
130
+ affiliate_id: "affiliate_id",
131
+ ).and_return("data" => {})
132
+
133
+ Shipcloud::PickupRequest.create(valid_attributes, affiliate_id: "affiliate_id")
134
+ end
135
+ end
136
+ end
@@ -3,25 +3,63 @@ require "spec_helper"
3
3
  describe Shipcloud::Request::Base do
4
4
  context "#perform" do
5
5
  it "checks for an api key" do
6
- Shipcloud.stub(:api_key).and_return(nil)
6
+ info = Shipcloud::Request::Info.new(:get, "shipments", nil, {}, "affiliate_id")
7
7
 
8
8
  expect{
9
- Shipcloud::Request::Base.new(nil).perform
9
+ Shipcloud::Request::Base.new(info).perform
10
10
  }.to raise_error Shipcloud::AuthenticationError
11
11
  end
12
12
 
13
- it "performs an https request" do
14
- Shipcloud.stub(:api_key).and_return("some key")
15
- connection = stub
16
- validator = stub
17
- Shipcloud::Request::Connection.stub(:new).and_return(connection)
18
- Shipcloud::Request::Validator.stub(:new).and_return(validator)
13
+ it "performs an https request and returns a response hash" do
14
+ connection = double
15
+ expect(Shipcloud::Request::Connection).to receive(:new).and_return(connection)
16
+ expect(connection).to receive(:setup_https)
17
+ response = double(code: "200", body: { id: 1 }.to_json)
18
+ expect(connection).to receive(:request).and_return(response)
19
+ info = Shipcloud::Request::Info.new(:get, "shipments", "api_key", {}, "affiliate_id")
19
20
 
20
- connection.should_receive(:setup_https)
21
- connection.should_receive(:request)
22
- validator.should_receive(:validated_data_for)
21
+ data = Shipcloud::Request::Base.new(info).perform
23
22
 
24
- Shipcloud::Request::Base.new(nil).perform
23
+ expect(data).to eq("id" => 1)
24
+ end
25
+
26
+ it "performs an https request and raises an Shipcloud::ClientError if the response "\
27
+ "has a 400 status code" do
28
+ connection = double
29
+ expect(Shipcloud::Request::Connection).to receive(:new).and_return(connection)
30
+ expect(connection).to receive(:setup_https)
31
+ response = double(code: "400", body: { id: 1 }.to_json)
32
+ expect(connection).to receive(:request).and_return(response)
33
+ info = Shipcloud::Request::Info.new(:get, "shipments", "api_key", {}, "affiliate_id")
34
+
35
+ expect { Shipcloud::Request::Base.new(info).perform }.
36
+ to raise_error(Shipcloud::ClientError)
37
+ end
38
+
39
+ it "performs an https request and raises an Shipcloud::ServerError if the response "\
40
+ "has a 500 status code" do
41
+ connection = double
42
+ expect(Shipcloud::Request::Connection).to receive(:new).and_return(connection)
43
+ expect(connection).to receive(:setup_https)
44
+ response = double(code: "500", body: { id: 1 }.to_json)
45
+ expect(connection).to receive(:request).and_return(response)
46
+ info = Shipcloud::Request::Info.new(:get, "shipments", "api_key", {}, "affiliate_id")
47
+
48
+ expect { Shipcloud::Request::Base.new(info).perform }.
49
+ to raise_error(Shipcloud::ServerError)
50
+ end
51
+
52
+ it "performs an https request and raises an Shipcloud::ShipcloudError if the body of the "\
53
+ "response is not in JSON" do
54
+ connection = double
55
+ expect(Shipcloud::Request::Connection).to receive(:new).and_return(connection)
56
+ expect(connection).to receive(:setup_https)
57
+ response = double(code: "200", body: "no json")
58
+ expect(connection).to receive(:request).and_return(response)
59
+ info = Shipcloud::Request::Info.new(:get, "shipments", "api_key", {}, "affiliate_id")
60
+
61
+ expect { Shipcloud::Request::Base.new(info).perform }.
62
+ to raise_error(Shipcloud::ShipcloudError)
25
63
  end
26
64
  end
27
- end
65
+ end
@@ -7,7 +7,7 @@ describe Shipcloud::Request::Connection do
7
7
 
8
8
  connection.setup_https
9
9
 
10
- connection.https.should_not be_nil
10
+ expect(connection.https).to_not be_nil
11
11
  end
12
12
  end
13
13
 
@@ -15,9 +15,9 @@ describe Shipcloud::Request::Connection do
15
15
  it "performs the actual request" do
16
16
  connection = Shipcloud::Request::Connection.new(nil)
17
17
  connection.setup_https
18
- connection.stub(:https_request)
18
+ allow(connection).to receive(:https_request)
19
19
 
20
- connection.https.should_receive(:request)
20
+ expect(connection.https).to receive(:request)
21
21
 
22
22
  connection.request
23
23
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+ require "spec_helper"
3
+
4
+ describe Shipcloud::Request::Info do
5
+ describe "#url" do
6
+ it "returns the correct url" do
7
+ info = Shipcloud::Request::Info.new(:get, "shipments", "api_key", {}, "affiliate_id")
8
+
9
+ expect(info.url).to eq "/v1/shipments"
10
+ end
11
+ end
12
+
13
+ describe "#path_with_params" do
14
+ it "returns just the path if there aren't any params" do
15
+ info = Shipcloud::Request::Info.new(:get, "shipments", "api_key", {}, "affiliate_id")
16
+ path = info.path_with_params(info.url, info.data)
17
+
18
+ expect(path).to eq "/v1/shipments"
19
+ end
20
+
21
+ it "returns the path and given params" do
22
+ info = Shipcloud::Request::Info.new(
23
+ :get,
24
+ "shipments",
25
+ "api_key",
26
+ { foo: "bar" },
27
+ "affiliate_id",
28
+ )
29
+ path = info.path_with_params(info.url, info.data)
30
+
31
+ expect(path).to eq "/v1/shipments?foo=bar"
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,125 @@
1
+ require "spec_helper"
2
+
3
+ describe Shipcloud::ShipcloudError do
4
+ describe ".from_response" do
5
+ context "with a response with status code 400" do
6
+ it "returns a Shipcloud::InvalidRequestError" do
7
+ expect(Shipcloud::ShipcloudError.from_response(build_response(status_code: 400))).to be_a(
8
+ Shipcloud::InvalidRequestError
9
+ )
10
+ end
11
+ end
12
+
13
+ context "with a response with status code 422" do
14
+ it "returns a Shipcloud::InvalidRequestError" do
15
+ expect(Shipcloud::ShipcloudError.from_response(build_response(status_code: 422))).to be_a(
16
+ Shipcloud::InvalidRequestError
17
+ )
18
+ end
19
+ end
20
+
21
+ context "with a response with status code 401" do
22
+ it "returns a Shipcloud::AuthenticationError" do
23
+ expect(Shipcloud::ShipcloudError.from_response(build_response(status_code: 401))).to be_a(
24
+ Shipcloud::AuthenticationError
25
+ )
26
+ end
27
+ end
28
+
29
+ context "with a response with status code 402" do
30
+ it "returns a Shipcloud::TooManyRequests" do
31
+ expect(Shipcloud::ShipcloudError.from_response(build_response(status_code: 402))).to be_a(
32
+ Shipcloud::TooManyRequests
33
+ )
34
+ end
35
+ end
36
+
37
+ context "with a response with status code 404" do
38
+ it "returns a Shipcloud::NotFoundError" do
39
+ expect(Shipcloud::ShipcloudError.from_response(build_response(status_code: 404))).to be_a(
40
+ Shipcloud::NotFoundError
41
+ )
42
+ end
43
+ end
44
+
45
+ context "with a response with a 4xx status code" do
46
+ it "returns a Shipcloud::ClientError" do
47
+ expect(Shipcloud::ShipcloudError.from_response(build_response(status_code: 400))).to be_a(
48
+ Shipcloud::ClientError
49
+ )
50
+ end
51
+ end
52
+
53
+ context "with a response with a 5xx status code" do
54
+ it "returns a Shipcloud::ClientError" do
55
+ expect(Shipcloud::ShipcloudError.from_response(build_response(status_code: 500))).to be_a(
56
+ Shipcloud::ServerError
57
+ )
58
+ end
59
+ end
60
+
61
+ context "with a 200 response" do
62
+ it "returns nil" do
63
+ expect(Shipcloud::ShipcloudError.from_response(build_response(status_code: 200))).to be_nil
64
+ end
65
+ end
66
+ end
67
+
68
+ describe "#message" do
69
+ context "with an errors node in the response body" do
70
+ it "returns the errors formated as one string" do
71
+ errors = ["error 1", "error 2"]
72
+ response = build_response(body: { errors: errors }.to_json)
73
+ error = Shipcloud::ShipcloudError.new(response)
74
+
75
+ expect(error.message).to eq '["error 1", "error 2"]'
76
+ end
77
+ end
78
+ context "without an errors node in the response body" do
79
+ it "returns the body of the response" do
80
+ error = Shipcloud::ShipcloudError.new(build_response(body: "test"))
81
+
82
+ expect(error.message).to eq "test"
83
+ end
84
+ end
85
+ end
86
+
87
+ describe "#errors" do
88
+ it "returns the errors node of the response" do
89
+ errors = ["error 1", "error 2"]
90
+ response = build_response(body: { errors: errors }.to_json)
91
+ error = Shipcloud::ShipcloudError.new(response)
92
+
93
+ expect(error.errors).to eq errors
94
+ end
95
+ context "with a response that has no errors node" do
96
+ it "returns an empty list" do
97
+ response = build_response(body: {}.to_json)
98
+ error = Shipcloud::ShipcloudError.new(response)
99
+
100
+ expect(error.errors).to eq []
101
+ end
102
+ end
103
+ context "with a response that has no JSON formated body" do
104
+ it "returns an empty list" do
105
+ response = build_response(body: "error")
106
+ error = Shipcloud::ShipcloudError.new(response)
107
+
108
+ expect(error.errors).to eq []
109
+ end
110
+ end
111
+ end
112
+
113
+ describe "#response" do
114
+ it "returns the response" do
115
+ response = build_response
116
+ error = Shipcloud::ShipcloudError.new(response)
117
+
118
+ expect(error.response).to eq response
119
+ end
120
+ end
121
+
122
+ def build_response(status_code: 400, body: nil)
123
+ double(code: status_code.to_s, body: body)
124
+ end
125
+ end
@@ -52,7 +52,7 @@ describe Shipcloud::ShipmentQuote do
52
52
  describe ".create" do
53
53
  it "makes a new POST request using the correct API endpoint" do
54
54
  expect(Shipcloud).to receive(:request).
55
- with(:post, "shipment_quotes", valid_attributes).
55
+ with(:post, "shipment_quotes", valid_attributes, api_key: nil, affiliate_id: nil).
56
56
  and_return("data" => {})
57
57
 
58
58
  Shipcloud::ShipmentQuote.create(valid_attributes)
@@ -70,5 +70,18 @@ describe Shipcloud::ShipmentQuote do
70
70
 
71
71
  expect(shipment_quote.price).to eq 42.12
72
72
  end
73
+
74
+ it "use the affiliate ID provided for the request" do
75
+ expect(Shipcloud).to receive(:request).
76
+ with(
77
+ :post,
78
+ "shipment_quotes",
79
+ valid_attributes,
80
+ api_key: nil,
81
+ affiliate_id: "affiliate_id",
82
+ ).and_return("data" => {})
83
+
84
+ Shipcloud::ShipmentQuote.create(valid_attributes, affiliate_id: "affiliate_id")
85
+ end
73
86
  end
74
87
  end