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

Add index call for shipments #6

Merged
merged 1 commit into from
Jan 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (Following "[Keep a CHANGELOG](http://keepachangelog.com/)")
- 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
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
9 changes: 6 additions & 3 deletions lib/shipcloud/operations/all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ 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 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
7 changes: 6 additions & 1 deletion lib/shipcloud/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ 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

def self.index_response_root
"#{class_name.downcase}s"
end
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
115 changes: 115 additions & 0 deletions spec/shipcloud/shipment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,119 @@
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("shipments" => [])

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,
}

expect(Shipcloud).to receive(:request).
with(:get, "shipments", filter).
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("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"
},
"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