Skip to content

Commit

Permalink
refactored shipment filter integration
Browse files Browse the repository at this point in the history
  • Loading branch information
sfroehler committed Jan 20, 2016
1 parent 5847ab8 commit a442b76
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 100 deletions.
18 changes: 12 additions & 6 deletions lib/shipcloud/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def set_attributes(attributes)
end
end

def self.camel_to_snakecase(string)
string.gsub(/::/, "/").
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
gsub(/([a-z\d])([A-Z])/, '\1_\2').
tr("-", "_").
downcase
end

def self.class_name
self.name.split("::").last
end
Expand All @@ -27,12 +35,10 @@ def self.base_url
"#{class_name.downcase}s"
end

def self.camel_to_snakecase(string)
string.gsub(/::/, "/").
gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
gsub(/([a-z\d])([A-Z])/, '\1_\2').
tr("-", "_").
downcase
def self.create_response_root
end

def self.index_response_root
end
end
end
7 changes: 3 additions & 4 deletions lib/shipcloud/operations/all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ module ClassMethods
# Loads all Objects of the resource
def all(filter = {})
response = Shipcloud.request(:get, base_url, filter)
if filter.empty?
response.map { |hash| new(hash) }
else
new(response)
if index_response_root
response = response.fetch(index_response_root, [])
end
response.map { |hash| new(hash) }
end
end

Expand Down
3 changes: 3 additions & 0 deletions lib/shipcloud/operations/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ module ClassMethods
# @param [Hash] attributes The attributes of the created object
def create(attributes)
response = Shipcloud.request(:post, base_url, attributes)
if create_response_root
response = response.fetch(create_response_root, {})
end
self.new(response)
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/shipcloud/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@ class Shipment < Base

attr_accessor :from, :to, :carrier, :package, :reference_number
attr_reader :id, :created_at, :carrier_tracking_no, :tracking_url, :label_url, :packages, :price

def self.index_response_root
"#{class_name.downcase}"
end
end
end
12 changes: 4 additions & 8 deletions lib/shipcloud/shipment_quote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ class ShipmentQuote < Base
attr_accessor :from, :to, :carrier, :package, :service
attr_reader :price

# Creates a new object
#
# @param [Hash] attributes The attributes of the created object
def self.create(attributes)
response = Shipcloud.request(:post, base_url, attributes)
new(response.fetch("shipment_quote", {}))
end

def self.base_url
"shipment_quotes"
end

def self.create_response_root
"shipment_quote"
end
end
end
162 changes: 80 additions & 82 deletions spec/shipcloud/shipment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@

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([])
expect(Shipcloud).to receive(:request).
with(:get, "shipments", {}).
and_return("shipments" => [])

Shipcloud::Shipment.all
end

Expand All @@ -97,96 +100,91 @@
"carrier_tracking_no" => "43128000105",
"tracking_status" => "out_for_delivery",
"page" => 2,
"per_page" => 25 }
"per_page" => 25,
}

Shipcloud.should_receive(:request).
expect(Shipcloud).to 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"
)
and_return("shipments" => shipments_array)

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
}
allow(Shipcloud).to receive(:request).
with(:get, "shipments", {}).
and_return("shipments" => shipments_array)
end

def shipments_array
[
{ "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"
},
{ "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
}
"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 a442b76

Please sign in to comment.