Skip to content

Commit

Permalink
Add index call for shipments
Browse files Browse the repository at this point in the history
Add filtering to all shipments call
  • Loading branch information
Dorothee Laugwitz authored and dorotheelaugwitz committed Jan 15, 2016
1 parent 84c64e9 commit 04d32a2
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- This CHANGELOG file to hopefully serve as an evolving example of a standardized open source project CHANGELOG.
- Create, find, update and index operations for address resource. (#4)
- Services attribute to carriers call (#5)
- Index operation for shipment resources with optional filter parameters. (#6)
- Create, find and index operations for webhook resource. (#7)
- Added the following ruby versions to travis-ci test runs:
- 2.1.7
Expand Down
10 changes: 7 additions & 3 deletions lib/shipcloud/operations/all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ module Operations
module All
module ClassMethods
# Loads all Objects of the resource
def all
response = Shipcloud.request(:get, base_url, {})
response.map {|hash| self.new(hash) }
def all(filter = {})
response = Shipcloud.request(:get, base_url, filter)
if filter.empty?
response.map { |hash| new(hash) }
else
new(response)
end
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/shipcloud/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ module Shipcloud
class Shipment < Base
include Shipcloud::Operations::Delete
include Shipcloud::Operations::Update
include Shipcloud::Operations::All

attr_accessor :from, :to, :carrier, :package, :reference_number
attr_reader :id, :created_at, :carrier_tracking_no, :tracking_url, :label_url, :packages, :price
end
end
end
117 changes: 117 additions & 0 deletions spec/shipcloud/shipment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,121 @@
Shipcloud::Shipment.delete("123")
end
end

describe ".all" do
it "makes a new Get request using the correct API endpoint" do
expect(Shipcloud).to receive(:request).with(:get, "shipments", {}).and_return([])
Shipcloud::Shipment.all
end

it "returns a list of Shipment objects" do
stub_shipments_requests

shipments = Shipcloud::Shipment.all

shipments.each do |shipment|
expect(shipment).to be_a Shipcloud::Shipment
end
end

it "returns a filtered list of Shipment objects when using filter parameters" do
filter = {
"carrier" => "dhl",
"service" => "returns",
"reference_number" => "ref123456",
"carrier_tracking_no" => "43128000105",
"tracking_status" => "out_for_delivery",
"page" => 2,
"per_page" => 25 }

Shipcloud.should_receive(:request).
with(:get, "shipments", filter).
and_return(
"id" => "86afb143f9c9c0cfd4eb7a7c26a5c616585a6271",
"carrier_tracking_no" => "43128000105",
"carrier" => "dhl",
"service" => "returns",
"created_at" => "2014-11-12T14:03:45+01:00",
"price" => 3.5,
"tracking_url" => "http://track.shipcloud.dev/de/86afb143f9",
"reference_number" => "ref123456",
"tracking_status" => "out_for_delivery"
)

Shipcloud::Shipment.all(filter)
end
end

def stub_shipments_requests
allow(Shipcloud).to receive(:request).with(:get, "shipments", {}).and_return(
[
{ "id" => "86afb143f9c9c0cfd4eb7a7c26a5c616585a6271",
"carrier_tracking_no" => "43128000105",
"carrier" => "hermes",
"service" => "standard",
"created_at" => "2014-11-12T14:03:45+01:00",
"price" => 3.5,
"tracking_url" => "http://track.shipcloud.dev/de/86afb143f9",
"to" => {
"first_name" => "Hans",
"last_name" => "Meier",
"street" => "Semmelweg",
"street_no" => "1",
"zip_code" => "12345",
"city" => "Hamburg",
"country" => "DE"
},
"from" => {
"company" => "webionate GmbH",
"last_name" => "Fahlbusch",
"street" => "Lüdmoor",
"street_no" => "35a",
"zip_code" => "22175",
"city" => "Hamburg",
"country" => "DE"
},
"packages" => {
"id" => "be81573799958587ae891b983aabf9c4089fc462",
"length" => 10.0,
"width" => 10.0,
"height" => 10.0,
"weight" => 1.5
}
},
{ "id" => "be81573799958587ae891b983aabf9c4089fc462",
"carrier_tracking_no" => "1Z12345E1305277940",
"carrier" => "ups",
"service" => "standard",
"created_at" => "2014-11-12T14:03:45+01:00",
"price" => 3.0,
"tracking_url" => "http://track.shipcloud.dev/de/be598a2fd2",
"to" => {
"first_name" => "Test",
"last_name" => "Kunde",
"street" => "Gluckstr.",
"street_no" => "57",
"zip_code" => "22081",
"city" => "Hamburg",
"country" => "DE"
},
"from" => {
"company" => "webionate GmbH",
"last_name" => "Fahlbusch",
"street" => "Lüdmoor",
"street_no" => "35a",
"zip_code" => "22175",
"city" => "Hamburg",
"country" => "DE"
},
"packages" => {
"id" => "74d4f1fc193d8a7ca542d1ee4e2021f3ddb82242",
"length" => 15.0,
"width" => 20.0,
"height" => 10.0,
"weight" => 2.0
}
}
]
)
end
end

0 comments on commit 04d32a2

Please sign in to comment.