Skip to content
This repository was archived by the owner on Aug 14, 2024. It is now read-only.

Commit e2632d8

Browse files
author
Christian Petersen
authored
Add Tracker resource with create, find, and index operations (#25)
Adds the new `Tracker` resource the gem. The operations create, find, and index are supported. Further Information on shipcloud's Trackers https://developers.shipcloud.io/reference/#trackers
1 parent 1eb58c8 commit e2632d8

4 files changed

Lines changed: 131 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Add attribute ```pickup_address``` to class ```Shipcloud::PickupRequest``` to submit an alternative address for pickup request to shipcloud
66
- Add attribute ```deactivated``` to class ```Shipcloud::Webhook```
77
- Add ```affiliate_id``` to ```Shipcloud::Configuration``` and submit it (or a default affiliate id) via API headers to shipcloud
8+
- Add class ```Shipcloud::Tracker``` with create, find, and index operations
89

910
### Removed
1011

lib/shipcloud.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module Shipcloud
2121
autoload :Address, "shipcloud/address"
2222
autoload :PickupRequest, "shipcloud/pickup_request"
2323
autoload :ShipmentQuote, "shipcloud/shipment_quote"
24+
autoload :Tracker, "shipcloud/tracker"
2425
autoload :Webhook, "shipcloud/webhook"
2526

2627
module Operations

lib/shipcloud/tracker.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
module Shipcloud
3+
class Tracker < Base
4+
include Shipcloud::Operations::All
5+
6+
attr_accessor :carrier, :carrier_tracking_no
7+
attr_reader :id
8+
9+
def self.index_response_root
10+
"#{class_name.downcase}s"
11+
end
12+
end
13+
end

spec/shipcloud/tracker_spec.rb

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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).
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+
end
38+
39+
describe ".create" do
40+
it "makes a new POST request using the correct API endpoint" do
41+
expect(Shipcloud).to receive(:request).
42+
with(:post, "trackers", valid_attributes, api_key: nil).
43+
and_return("data" => {})
44+
45+
Shipcloud::Tracker.create(valid_attributes)
46+
end
47+
end
48+
49+
describe ".find" do
50+
it "makes a new GET request using the correct API endpoint to receive a specific tracker" do
51+
expect(Shipcloud).to receive(:request).with(:get, "trackers/123", {}, api_key: nil).
52+
and_return("id" => "123")
53+
54+
Shipcloud::Tracker.find("123")
55+
end
56+
end
57+
58+
def stub_trackers_requests
59+
allow(Shipcloud).to receive(:request).
60+
with(:get, "trackers", {}, api_key: nil).
61+
and_return("trackers" => trackers_array)
62+
end
63+
64+
def trackers_array
65+
[
66+
{
67+
"id" => "4a6922e2-09ad-4724-807c-7b4e572d3c6b",
68+
"carrier_tracking_no" => "723558934169",
69+
"status" => "registered",
70+
"created_at" => "2015-07-21T09:35:23+02:00",
71+
"to" => {
72+
"company" => nil,
73+
"first_name" => "Hans",
74+
"last_name" => "Meier",
75+
"care_of" => nil,
76+
"street" => "Semmelweg",
77+
"street_no" => "1",
78+
"zip_code" => "12345",
79+
"city" => "Hamburg",
80+
"state" => nil,
81+
"country" => "DE",
82+
},
83+
"tracking_status_updated_at" => nil,
84+
"last_polling_at" => nil,
85+
"next_polling_at" => "2015-07-21T09:35:23+02:00",
86+
"shipment_id" => "123456",
87+
"carrier" => "ups",
88+
"tracking_events" => [],
89+
},
90+
{
91+
"id" => "7b4e572d3c6b-4a6922e2-09ad-4724-807c",
92+
"carrier_tracking_no" => "723558934170",
93+
"status" => "registered",
94+
"created_at" => "2015-07-20T09:35:23+02:00",
95+
"to" => {
96+
"company" => nil,
97+
"first_name" => "Rita",
98+
"last_name" => "Rose",
99+
"care_of" => nil,
100+
"street" => "Geranienweg",
101+
"street_no" => "2",
102+
"zip_code" => "23456",
103+
"city" => "Berlin",
104+
"state" => nil,
105+
"country" => "DE",
106+
},
107+
"tracking_status_updated_at" => nil,
108+
"last_polling_at" => nil,
109+
"next_polling_at" => "2015-07-21T09:35:23+02:00",
110+
"shipment_id" => "654321",
111+
"carrier" => "dpd",
112+
"tracking_events" => [],
113+
},
114+
]
115+
end
116+
end

0 commit comments

Comments
 (0)