Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add shipment quotes #9

Merged
merged 3 commits into from
Jan 18, 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 @@ -8,6 +8,7 @@
- 2.1.7
- 2.2.4
- 2.3.0
- Add ShipmentQuotes class returning the price for a certain shipment (#9)

### Removed
- Dropped support for ruby 1.9.x in order to use the new language features of ruby 2.x. The official support of ruby 1.9.3 already ended on February 23, 2015 (https://www.ruby-lang.org/en/news/2014/01/10/ruby-1-9-3-will-end-on-2015/)
Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,40 @@ shipment = Shipcloud::Shipment.create(...) # parameters ommitted
shipment.tracking_url # -> http://track.shipcloud.io/uzdgu22z3ed12
```

### Get a shipment quote

To get a shipment qoute from the shipclod platform, you need to provide the name of the carrier, the service, to- and from-address, and the package dimensions.
For details, see *[shipcloud API documentation on shipment quotes](https://developers.shipcloud.io/reference/#shipment-quotes)*

```ruby
shipment_quote = Shipcloud::ShipmentQuote.create(
carrier: 'ups',
service: 'standard',
to: {
street: "Receiver street",
street_no: "123",
zip_code: "12345",
city: "Receiver town",
country: "DE"
},
from: {
street: "Sender street",
street_no: "321",
zip_code: "54321",
city: "Sender town",
country: "DE"
},
package: {
weight: 8,
width: 15,
length: 32,
height: 46,
},
)

shipment_quote.price # => 6.2
```

## Contributing

1. Fork it
Expand Down
1 change: 1 addition & 0 deletions lib/shipcloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Shipcloud
autoload :Shipment, "shipcloud/shipment"
autoload :Carrier, "shipcloud/carrier"
autoload :Address, "shipcloud/address"
autoload :ShipmentQuote, "shipcloud/shipment_quote"
autoload :Webhook, "shipcloud/webhook"

module Operations
Expand Down
8 changes: 8 additions & 0 deletions lib/shipcloud/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,13 @@ def self.class_name
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
end
end
end
20 changes: 20 additions & 0 deletions lib/shipcloud/shipment_quote.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Shipcloud
class ShipmentQuote < Base
include Shipcloud::Operations::Create

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to either move this to the Base class so it can be reused or (to keep it simple) just use the string "shipment_quote" in the .base_url method.

end
end
74 changes: 74 additions & 0 deletions spec/shipcloud/shipment_quote_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
require "spec_helper"

describe Shipcloud::ShipmentQuote do
valid_attributes = {
to: {
company: "Beispielfirma",
street: "Beispielstrasse",
street_no: "42",
city: "Berlin",
zip_code: "10000",
},
from: {
company: "shipcloud GmbH",
street: "Musterallee",
street_no: "23",
city: "Hamburg",
zip_code: "20000",
},
service: "standard",
carrier: "dhl",
package: {
weight: 2.5,
length: 40,
width: 20,
height: 20
}
}

describe "#initialize" do
it "initializes all attributes correctly" do
quote = Shipcloud::ShipmentQuote.new(valid_attributes)

expect(quote.to[:company]).to eq "Beispielfirma"
expect(quote.to[:street]).to eq "Beispielstrasse"
expect(quote.to[:street_no]).to eq "42"
expect(quote.to[:city]).to eq "Berlin"
expect(quote.to[:zip_code]).to eq "10000"
expect(quote.from[:company]).to eq "shipcloud GmbH"
expect(quote.from[:street]).to eq "Musterallee"
expect(quote.from[:street_no]).to eq "23"
expect(quote.from[:city]).to eq "Hamburg"
expect(quote.from[:zip_code]).to eq "20000"
expect(quote.carrier).to eq "dhl"
expect(quote.service).to eq "standard"
expect(quote.package[:weight]).to eq 2.5
expect(quote.package[:length]).to eq 40
expect(quote.package[:width]).to eq 20
expect(quote.package[:height]).to eq 20
end
end

describe ".create" do
it "makes a new POST request using the correct API endpoint" do
expect(Shipcloud).to receive(:request).
with(:post, "shipment_quotes", valid_attributes).
and_return("data" => {})

Shipcloud::ShipmentQuote.create(valid_attributes)
end

it "initializes a ShipmentQuote with price" do
allow(Shipcloud).to receive(:request).
and_return(
"shipment_quote" => {
"price" => 42.12
}
)

shipment_quote = Shipcloud::ShipmentQuote.create(valid_attributes)

expect(shipment_quote.price).to eq 42.12
end
end
end