Skip to content

Commit

Permalink
Merge pull request #50 from goshippo/v3-updates
Browse files Browse the repository at this point in the history
V3 updates
  • Loading branch information
daria-lamberson authored Mar 31, 2017
2 parents 01249c9 + dcea188 commit 2486419
Show file tree
Hide file tree
Showing 33 changed files with 1,540 additions and 317 deletions.
11 changes: 8 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
### 2.0.8 release, Feb 15th, 2017
#### 3.0.0 release, Mar 31st, 2017
- Version 3 changes
- Object fields removed/updated
- Credentials inserted for tests

#### 2.0.8 release, Feb 15th, 2017
- Add Batch API with example code
- Creation
- Retrieval
Expand All @@ -11,12 +16,12 @@
- Add mock tests using VCR (for Batch and Track API)
- https://github.com/vcr/vcr

### 2.0.7 release, Jan 2nd, 2017
#### 2.0.7 release, Jan 2nd, 2017
- Fixed bug preventing address validation
- Removed trailing slash from base URL, added spec test to ensure this configuration
- Added address validation example
- Updated basic shipment example to include how to access the Shippo object id
### 2.0.6 release, Nov 22nd, 2016
#### 2.0.6 release, Nov 22nd, 2016
- Fixed bug to send request with correct API version header

#### 2.0.5 release, Oct 24th, 2016
Expand Down
148 changes: 75 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ require 'shippo'
Shippo::API.token = 'aff988f77afa0fdfdfadf' # not an actual valid token

# Setup query parameter hash
params = { object_purpose: 'PURCHASE',
async: false,
params = { async: false,
address_from: {
object_purpose: 'PURCHASE',
name: 'Mr Hippo',
company: 'Shippo',
street1: '215 Clayton St.',
Expand All @@ -57,10 +55,8 @@ params = { object_purpose: 'PURCHASE',
state: 'CA',
zip: '94117',
country: 'US',
phone: '+1 555 341 9393',
email: 'support@goshippo.com' },
phone: '+1 555 341 9393' },
address_to: {
object_purpose: 'PURCHASE',
name: 'Mrs Hippo"',
company: 'San Diego Zoo',
street1: '2920 Zoo Drive',
Expand All @@ -70,7 +66,7 @@ params = { object_purpose: 'PURCHASE',
country: 'US',
phone: '+1 555 341 9393',
email: 'hippo@goshippo.com' },
parcel: {
parcels: {
length: 5,
width: 2,
height: 5,
Expand All @@ -83,12 +79,8 @@ params = { object_purpose: 'PURCHASE',
@shipment = Shippo::Shipment.create(params)
@shipment.success?
# => true
@shipment.object.status
# => 'SUCCESS'
@shipment.status # forwarded to #object
@shipment.status
# => 'SUCCESS'
@shipment.state
# => 'VALID'
```

Let's take a quick look at what the `Shipment` object looks like:
Expand All @@ -97,63 +89,76 @@ Let's take a quick look at what the `Shipment` object looks like:
require 'awesome_print'
ap @shipment
# {
# "carrier_accounts" => [],
# "address_from" => "a704eada7494bb1be6184ef64b1646db",
# "address_to" => "92b43fbfa3641644beb32996042eb57a",
# "address_return" => "a1f64ba14b7e41b86a0446de4ebbd769",
# "parcel" => "92df4baac73ea6131940c0d315d70a7d",
# "submission_date" => "2016-07-06T20:33:02.211Z",
# "return_of" => nil,
# "customs_declaration" => nil,
# "insurance_amount" => "0",
# "insurance_currency" => nil,
# "extra" => {},
# "reference_1" => "",
# "reference_2" => "",
# "rates_url" => "https://api.goshippo.com/v1/shipments/a336daf87a8e442992a68daa6622758f/rates/",
# "messages" => [ ] # ommitted for brevity,
# "rates_list" => [ ] # ommitted for brevity.
# "carrier_accounts" => [],
# "address_from" => {
# "name" => "Mr Hippo",
# "company" => "Shippo",
# "street1" => "215 Clayton St.",
# "street2" => "",
# "city" => "San Francisco",
# "state" => "CA",
# "zip" => "94117",
# "country" => "US",
# "phone" => "+1 555 341 9393"
# },
# "address_to" => { ... }, # omitted for brevity
# "address_return" => { ... },
# "parcels" => [{
# "length" => 5,
# "width" => 2,
# "height" => 5,
# "distance_unit" => in,
# "weight" => 2,
# "mass_unit" => lb,
# }],
# "shipment_date" => "2016-07-06T20:33:02.211Z",
# "customs_declaration" => nil,
# "extra" => {
# "insurance" => {
# "amount" => 10,
# "currency" => "USD",
# "content" => "",
# "provider" => "FEDEX"
# },
# "is_return" => false,
# "reference_1" => "",
# "reference_2" => "",
# },
# "messages" => [ ... ],
# "rates" => [ ... ]
# }
```

#### List Handling

In the case when the API returns a hash with one of the hash values being an array of entities, and if the corresponding key can be mapped into one of the existing API models, then each of the members of the array is coerced from a hash into an object of the model's type.

In the example below we are showing the result of such transformation where the `rates_list` contains a list of fully constructed objects of type `Shippo::Rate` after being coerced from a hash.
In the example below we are showing the result of such transformation where `rates` contains a list of fully constructed objects of type `Shippo::Rate` after being coerced from a hash.

```ruby
ap @shipment.rates_list.first
ap @shipment.rates.first
# =>
# {
# "shipment" => "20f25e44b16b4051b6dd910cb66fd27b",
# "available_shippo" => true,
# "attributes" => [],
# "amount" => "8.51",
# "currency" => "USD",
# "amount_local" => "8.51",
# "currency_local" => "USD",
# "provider" => "FedEx",
# "provider_image_75" => "https://shippo-static.s3.amazonaws.com/providers/75/FedEx.png",
# "provider_image_200" => "https://shippo-static.s3.amazonaws.com/providers/200/FedEx.png",
# "servicelevel_name" => "Ground",
# "servicelevel_token" => "fedex_ground",
# "servicelevel_terms" => "",
# "days" => 2,
# "arrives_by" => nil,
# "duration_terms" => "",
# "trackable" => true,
# "insurance" => false,
# "insurance_amount_local" => "0.00",
# "insurance_currency_local" => nil,
# "insurance_amount" => "0.00",
# "insurance_currency" => nil,
# "delivery_attempts" => nil,
# "outbound_endpoint" => "door",
# "inbound_endpoint" => "door",
# "messages" => [],
# "carrier_account" => "4b1940bc69524163b669asd361842db",
# "test" => true
# {
# "shipment" => "20f25e44b16b4051b6dd910cb66fd27b",
# "attributes" => [],
# "amount" => "8.51",
# "currency" => "USD",
# "amount_local" => "8.51",
# "currency_local" => "USD",
# "provider" => "FedEx",
# "provider_image_75" => "https://shippo-static.s3.amazonaws.com/providers/75/FedEx.png",
# "provider_image_200" => "https://shippo-static.s3.amazonaws.com/providers/200/FedEx.png",
# "servicelevel" => {
# "name" => "Ground",
# "token" => "fedex_ground",
# "terms" => ""
# }
# "days" => 2,
# "arrives_by" => nil,
# "duration_terms" => "",
# "messages" => [],
# "carrier_account" => "4b1940bc69524163b669asd361842db",
# "test" => true
# }
@shipment.rates.first.owner
# ⤷ unittest@gmail.com
Expand All @@ -170,10 +175,10 @@ You can retrieve a list of objects for many endpoints (e.g. Transactions, Shipme
@transactions
# =>
# {
# "count" => 3055,
# "next" => "https://api.goshippo.com/v1/transactions/?page=2",
# "previous" => nil,
# "results" => [ ... list of Transaction objects ... ]
# "count" => 3055,
# "next" => "https://api.goshippo.com/v1/transactions/?page=2",
# "previous" => nil,
# "results" => [ ... list of Transaction objects ... ]
# }
```

Expand Down Expand Up @@ -213,18 +218,15 @@ Finally, here is how we access the rest of the `object_` fields:
# ⤷ valued_customer@gmail.com
```

Here is the fully construted `ApiObject` instance, attached to our `@shipment`:
Here is the fully constructed `ApiObject` instance, attached to our `@shipment`:

```ruby
ap @shipment.object
# {
# :created => 2016-07-06 20:44:47 UTC,
# :updated => 2016-07-06 20:44:47 UTC,
# :owner => "valued_customer@gmail.com",
# :state => #<Shippo::API::Category::State:0x007fd88be8aa38 @name=:state, @value=:valid>,
# :status => #<Shippo::API::Category::Status:0x007fd88be82e28 @name=:status, @value=:success>,
# :purpose => #<Shippo::API::Category::Purpose:0x007fd88be985e8 @name=:purpose, @value=:purchase>,
# :id => "20f25e44b16b4051b6dd910cb66fd27b"
# :created => 2016-07-06 20:44:47 UTC,
# :updated => 2016-07-06 20:44:47 UTC,
# :owner => "valued_customer@gmail.com",
# :id => "20f25e44b16b4051b6dd910cb66fd27b"
# }
```

Expand All @@ -247,9 +249,9 @@ NOTE: this environment variable is only used by the included `bin/example` scrip

## Gem Versioning Notes

Version 2 and up of this library works with Ruby 2.2 and later, and is not backwards compatible. __Version 1.0.4__ of this library is the last version supporting ruby 1.8 and 1.9.
Version 3 and up of this library works with Ruby 2.2 and later, and is not backwards compatible. __Version 1.0.4__ of this library is the last version supporting ruby 1.8 and 1.9.

__Warning:__ Version 2 brings potential backwards incompatibility issues. Please be prepared to update your usages (if necessary) when you migrate.
__Warning:__ Version 3 brings potential backwards incompatibility issues. Please be prepared to update your usages (if necessary) when you migrate.

### If you are still using Ruby 1.8 or 1.9

Expand Down
1 change: 0 additions & 1 deletion bin/address_validation_example
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ end

# Create address object
address = {
:object_purpose => 'PURCHASE',
:name => 'Mr Hippo',
:company => 'Shippo',
:street1 => '215 Clayton St.',
Expand Down
7 changes: 2 additions & 5 deletions bin/basic_shipment_example
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ end

# Create address_from object
address_from = {
:object_purpose => 'PURCHASE',
:name => 'Mr Hippo',
:company => 'Shippo',
:street1 => '215 Clayton St.',
Expand All @@ -66,7 +65,6 @@ address_from = {

# Create address_to object
address_to = {
:object_purpose => 'PURCHASE',
:name => 'Mrs Hippo"',
:company => 'San Diego Zoo',
:street1 => '2920 Zoo Drive',
Expand All @@ -86,8 +84,7 @@ parcel = {
:weight => 2,
:mass_unit => :lb }

hash = { :object_purpose => 'PURCHASE',
:address_from => address_from,
hash = { :address_from => address_from,
:address_to => address_to,
:parcel => parcel,
:async => false }
Expand Down Expand Up @@ -120,7 +117,7 @@ end

# Get the desired rate according to your business logic
# We select the first rate in this example
rate = shipment.rates_list.first
rate = shipment.rates.first
ExampleHelper.dump_object(rate, 'First rate returned')

transaction = ExampleHelper.log_operation 'Creating a label with the first rate in the list... ' do
Expand Down
6 changes: 1 addition & 5 deletions bin/batch_example
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ end

# Create address_from object
address_from = {
:object_purpose => 'PURCHASE',
:name => 'Mr Hippo',
:company => 'Shippo',
:street1 => '215 Clayton St.',
Expand All @@ -79,7 +78,6 @@ address_from = {

# Create address_to object
address_to = {
:object_purpose => 'PURCHASE',
:name => 'Mrs Hippo"',
:company => 'San Diego Zoo',
:street1 => '2920 Zoo Drive',
Expand All @@ -106,7 +104,6 @@ hash = { :default_carrier_account => DEFAULT_CARRIER_ACCOUNT,
:batch_shipments => [
{
:shipment => {
:object_purpose => 'PURCHASE',
:address_from => address_from,
:address_to => address_to,
:parcel => parcel,
Expand All @@ -116,8 +113,7 @@ hash = { :default_carrier_account => DEFAULT_CARRIER_ACCOUNT,
]
}

shipment_params = { :object_purpose => 'PURCHASE',
:address_from => address_from,
shipment_params = { :address_from => address_from,
:address_to => address_to,
:parcel => parcel,
:async => false }
Expand Down
5 changes: 1 addition & 4 deletions bin/track_example
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ end

# Create address_from object
address_from = {
:object_purpose => 'PURCHASE',
:name => 'Mr Hippo',
:company => 'Shippo',
:street1 => '215 Clayton St.',
Expand All @@ -63,7 +62,6 @@ address_from = {

# Create address_to object
address_to = {
:object_purpose => 'PURCHASE',
:name => 'Mrs Hippo"',
:company => 'San Diego Zoo',
:street1 => '2920 Zoo Drive',
Expand All @@ -83,8 +81,7 @@ parcel = {
:weight => 2,
:mass_unit => :lb }

hash = { :object_purpose => 'PURCHASE',
:address_from => address_from,
hash = { :address_from => address_from,
:address_to => address_to,
:parcel => parcel,
:async => false }
Expand Down
2 changes: 1 addition & 1 deletion lib/shippo/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
module Shippo
module API
@base = 'https://api.goshippo.com'
@version = nil
@version = ''
@token = ''
@debug = Integer(ENV['SHIPPO_DEBUG'] || 0) > 0 ? true : false
@warnings = true
Expand Down
8 changes: 2 additions & 6 deletions lib/shippo/api/api_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ module API
#
# ```ruby
# response = {
# "object_state" => "VALID",
# "object_purpose" => "PURCHASE",
# "object_source" => "FULLY_ENTERED",
# "object_state" => "VALID", # not available for address, shipment or rates
# "object_created" => "2014-07-16T23:20:31.089Z",
# "object_updated" => "2014-07-16T23:20:31.089Z",
# "object_id" => "747207de2ba64443b645d08388d0309c",
Expand All @@ -49,8 +47,6 @@ module API
# # ⤷
# {
# :state => #<Shippo::API::Category::State:0x007fd374b4d0d0 @name=:state, @value=:valid>,
# :purpose => #<Shippo::API::Category::Purpose:0x007fd373df2070 @name=:purpose, @value=:purchase>,
# :source => #<Shippo::API::Category::Source:0x007fd374b4fbf0 @name=:source, @value=:fully_entered>,
# :created => 2014-07-16 23:20:31 UTC,
# :updated => 2014-07-16 23:20:31 UTC,
# :id => "747207de2ba64443b645d08388d0309c",
Expand Down Expand Up @@ -78,7 +74,7 @@ def mk_opts(property)

# list of allowed properties, of a given type.
PROPS_ID = %i(id).freeze
PROPS_CATEG = %i(state purpose source status results).freeze
PROPS_CATEG = %i(state status results).freeze
PROPS_EMAIL = %i(owner).freeze
PROPS_TIMED = %i(created updated).freeze

Expand Down
2 changes: 1 addition & 1 deletion lib/shippo/api/transformers/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class List
# mapped to an array of hashes, into a word that represents an existing model.
#
# Each matcher receives a key as a parameter, and (if matches) it extracts the candidate word to be
# attempted to +constantize+. For example, +rates_list+ matcher will return +rates+ as output.
# attempted to +constantize+. For example, +rates+ matcher will return +rates+ as output.

MATCHERS = [
->(key) {
Expand Down
Loading

0 comments on commit 2486419

Please sign in to comment.