Skip to content

Commit

Permalink
Carbon offset support (#201)
Browse files Browse the repository at this point in the history
- New carbon_offset.rb class
- Ability to pass `with_carbon_offset` parameter when creating, buying or re-rating a shipment
  • Loading branch information
nwithan8 authored Aug 1, 2022
1 parent 1e3a282 commit 1726ffd
Show file tree
Hide file tree
Showing 45 changed files with 1,722 additions and 1,099 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ inherit_from: easycop.yml
# We are ignoring RSpec/FilePath because Simplecov doesn't play nice with nested spec files
RSpec/FilePath:
Enabled: false
# TODO: Remove this once we start using keyword arguments
Style/OptionalBooleanParameter:
Exclude:
- 'lib/easypost/shipment.rb'
1 change: 1 addition & 0 deletions lib/easypost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
require 'easypost/batch'
require 'easypost/billing'
require 'easypost/brand'
require 'easypost/carbon_offset'
require 'easypost/carrier_account'
require 'easypost/carrier_type'
require 'easypost/customs_info'
Expand Down
5 changes: 5 additions & 0 deletions lib/easypost/carbon_offset.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

# The CarbonOffset object is a summary of carbon offset data for a given rate, including grams, price and currency
class EasyPost::CarbonOffset < EasyPost::Resource
end
20 changes: 18 additions & 2 deletions lib/easypost/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,22 @@
# The workhorse of the EasyPost API, a Shipment is made up of a "to" and "from" Address, the Parcel
# being shipped, and any customs forms required for international deliveries.
class EasyPost::Shipment < EasyPost::Resource
# Create a Shipment.
def self.create(params = {}, api_key = nil, with_carbon_offset = false)
wrapped_params = {
shipment: params,
carbon_offset: with_carbon_offset,
}

response = EasyPost.make_request(:post, url, api_key, wrapped_params)
EasyPost::Util.convert_to_easypost_object(response, api_key)
end

# Regenerate the rates of a Shipment.
def regenerate_rates(params = {})
def regenerate_rates(with_carbon_offset = false)
params = {}
params[:carbon_offset] = with_carbon_offset

response = EasyPost.make_request(:post, "#{url}/rerate", @api_key, params)
refresh_from(response, @api_key)

Expand All @@ -21,13 +35,15 @@ def get_smartrates # rubocop:disable Naming/AccessorMethodName
end

# Buy a Shipment.
def buy(params = {})
def buy(params = {}, with_carbon_offset = false)
if params.instance_of?(EasyPost::Rate)
temp = params.clone
params = {}
params[:rate] = temp
end

params[:carbon_offset] = with_carbon_offset

response = EasyPost.make_request(:post, "#{url}/buy", @api_key, params)
refresh_from(response, @api_key)

Expand Down
1 change: 1 addition & 0 deletions lib/easypost/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module EasyPost::Util
'Address' => EasyPost::Address,
'Batch' => EasyPost::Batch,
'Brand' => EasyPost::Brand,
'CarbonOffset' => EasyPost::CarbonOffset,
'CarrierAccount' => EasyPost::CarrierAccount,
'CustomsInfo' => EasyPost::CustomsInfo,
'CustomsItem' => EasyPost::CustomsItem,
Expand Down

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

78 changes: 39 additions & 39 deletions spec/cassettes/pickup/EasyPost_Pickup_buy_buys_a_pickup.yml

Large diffs are not rendered by default.

106 changes: 54 additions & 52 deletions spec/cassettes/pickup/EasyPost_Pickup_cancel_cancels_a_pickup.yml

Large diffs are not rendered by default.

56 changes: 28 additions & 28 deletions spec/cassettes/pickup/EasyPost_Pickup_create_creates_a_pickup.yml

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

48 changes: 24 additions & 24 deletions spec/cassettes/rate/EasyPost_Rate_retrieve_retrieves_a_rate.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1726ffd

Please sign in to comment.