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

Restore easypost functionality #112

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
57 changes: 39 additions & 18 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,55 @@ orbs:
solidusio_extensions: solidusio/extensions@volatile

jobs:
run-specs-with-postgres:
executor: solidusio_extensions/postgres
run-specs:
parameters:
solidus:
type: string
default: main
db:
type: string
default: "postgres"
ruby:
type: string
default: "3.2"
executor:
name: solidusio_extensions/<< parameters.db >>
ruby_version: << parameters.ruby >>
steps:
- solidusio_extensions/run-tests
run-specs-with-mysql:
executor: solidusio_extensions/mysql
steps:
- solidusio_extensions/run-tests
lint-code:
executor: solidusio_extensions/sqlite-memory
steps:
- solidusio_extensions/lint-code
- checkout
- solidusio_extensions/run-tests-solidus-<< parameters.solidus >>

workflows:
"Run specs on supported Solidus versions":
jobs:
- run-specs-with-postgres
- run-specs-with-mysql
- lint-code
- run-specs:
name: &name "run-specs-solidus-<< matrix.solidus >>-ruby-<< matrix.ruby >>-db-<< matrix.db >>"
matrix:
parameters: { solidus: ["main"], ruby: ["3.2"], db: ["postgres"] }
- run-specs:
name: *name
matrix:
parameters: { solidus: ["current"], ruby: ["3.1"], db: ["mysql"] }
- run-specs:
name: *name
matrix:
parameters: { solidus: ["older"], ruby: ["3.0"], db: ["sqlite"] }

"Weekly run specs against master":
"Weekly run specs against main":
triggers:
- schedule:
cron: "0 0 * * 4" # every Thursday
filters:
branches:
only:
- master
- main
jobs:
- run-specs-with-postgres
- run-specs-with-mysql
- run-specs:
name: *name
matrix:
parameters: { solidus: ["main"], ruby: ["3.2"], db: ["postgres"] }
- run-specs:
name: *name
matrix:
parameters: { solidus: ["current"], ruby: ["3.1"], db: ["mysql"] }

19 changes: 11 additions & 8 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

branch = ENV.fetch('SOLIDUS_BRANCH', 'master')
solidus_git, solidus_frontend_git = if (branch == 'master') || (branch >= 'v3.2')
%w[solidusio/solidus solidusio/solidus_frontend]
else
%w[solidusio/solidus] * 2
end
gem 'solidus', github: solidus_git, branch: branch
gem 'solidus_frontend', github: solidus_frontend_git, branch: branch
solidus_branch = ENV.fetch('SOLIDUS_BRANCH', 'main')
gem 'solidus', github: 'solidusio/solidus', branch: solidus_branch

# The solidus_frontend gem has been pulled out since v3.2
if solidus_branch >= 'v3.2'
gem 'solidus_frontend'
elsif solidus_branch == 'main'
gem 'solidus_frontend', github: 'solidusio/solidus_frontend'
else
gem 'solidus_frontend', github: 'solidusio/solidus', branch: solidus_branch
end

# Needed to help Bundler figure out how to resolve dependencies,
# otherwise it takes forever to resolve them.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def self.prepended(base)
def easypost_shipment
return unless selected_easy_post_shipment_id

@easypost_shipment ||= ::EasyPost::Shipment.retrieve(selected_easy_post_shipment_id)
@easypost_shipment ||= SolidusEasypost.client.shipment.retrieve(selected_easy_post_shipment_id)
end

def easypost_postage_label_url
Expand All @@ -32,12 +32,10 @@ def easypost_postage_label_url
private

def buy_easypost_rate
rate = easypost_shipment.rates.find do |easypost_rate|
easypost_rate.id == selected_easy_post_rate_id
unless easypost_shipment.tracking_code
SolidusEasypost.client.shipment.buy(selected_easy_post_shipment_id, rate: { id: selected_easy_post_rate_id })
end

easypost_shipment.buy(rate)

self.tracking = easypost_shipment.tracking_code
end

Expand Down
2 changes: 1 addition & 1 deletion bin/sandbox
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if [ ! -z $SOLIDUS_BRANCH ]
then
BRANCH=$SOLIDUS_BRANCH
else
BRANCH="master"
BRANCH="main"
fi

extension_name="solidus_easypost"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

EasyPost.api_key = 'YOUR_API_KEY_HERE'

SolidusEasypost.configure do |config|
# API Key
config.api_key = 'YOUR_API_KEY_HERE'

# Purchase labels from EasyPost when shipping shipments in Solidus?
# config.purchase_labels = true

Expand Down
3 changes: 3 additions & 0 deletions lib/solidus_easypost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@

module SolidusEasypost
class << self
attr_reader :client

def configuration
@configuration ||= Configuration.new
end

def configure
yield configuration
@client = ::EasyPost::Client.new(api_key: @configuration.api_key)
end
end
end
4 changes: 2 additions & 2 deletions lib/solidus_easypost/address_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def from_address(address, options = {})
attributes[:state] = address.state ? address.state.abbr : address.state_name
attributes[:country] = address.country&.iso

::EasyPost::Address.create attributes.merge(options)
SolidusEasypost.client.address.create attributes.merge(options)
end

def from_stock_location(stock_location, options = {})
Expand All @@ -40,7 +40,7 @@ def from_stock_location(stock_location, options = {})
attributes[:state] = stock_location.state ? stock_location.state.abbr : stock_location.state_name
attributes[:country] = stock_location.country&.iso

::EasyPost::Address.create attributes.merge(options)
SolidusEasypost.client.address.create attributes.merge(options)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/solidus_easypost/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module SolidusEasypost
class Configuration
attr_accessor :purchase_labels, :track_all_cartons
attr_accessor :api_key, :purchase_labels, :track_all_cartons
attr_writer :shipping_rate_calculator_class, :shipping_method_selector_class, :parcel_dimension_calculator_class,
:webhook_handler_class

Expand Down
4 changes: 2 additions & 2 deletions lib/solidus_easypost/parcel_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def from_package(package)
.new
.compute(package)

::EasyPost::Parcel.create(parcel_dimension.to_h)
SolidusEasypost.client.parcel.create(parcel_dimension.to_h)
end

def from_return_authorization(return_authorization)
Expand All @@ -20,7 +20,7 @@ def from_return_authorization(return_authorization)
.new
.compute(return_authorization)

::EasyPost::Parcel.create(parcel_dimension.to_h)
SolidusEasypost.client.parcel.create(parcel_dimension.to_h)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/solidus_easypost/shipment_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ShipmentBuilder
class << self
def from_package(package, options = {})
from_address, to_address = address_options(options)
::EasyPost::Shipment.create(
SolidusEasypost.client.shipment.create(
to_address: AddressBuilder.from_address(package.order.ship_address, to_address || {}),
from_address: AddressBuilder.from_stock_location(package.stock_location, from_address || {}),
parcel: ParcelBuilder.from_package(package),
Expand All @@ -15,7 +15,7 @@ def from_package(package, options = {})

def from_shipment(shipment, options = {})
from_address, to_address = address_options(options)
::EasyPost::Shipment.create(
SolidusEasypost.client.shipment.create(
to_address: AddressBuilder.from_address(shipment.order.ship_address, to_address || {}),
from_address: AddressBuilder.from_stock_location(shipment.stock_location, from_address || {}),
parcel: ParcelBuilder.from_package(shipment.to_package),
Expand All @@ -25,7 +25,7 @@ def from_shipment(shipment, options = {})

def from_return_authorization(return_authorization, options = {})
from_address, to_address = address_options(options)
::EasyPost::Shipment.create(
SolidusEasypost.client.shipment.create(
from_address: AddressBuilder.from_stock_location(return_authorization.stock_location, from_address || {}),
to_address: AddressBuilder.from_address(return_authorization.order.ship_address, to_address || {}),
parcel: ParcelBuilder.from_return_authorization(return_authorization),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

FactoryBot.modify do
factory :address do
FactoryBot.define do
factory :address, class: 'Spree::Address' do
address1 { '215 N 7th Ave' }
city { 'Manville' }
association(:state, name: 'New Jersey', abbr: 'NJ')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

FactoryBot.modify do
factory :base_product do
FactoryBot.define do
factory :base_product, class: 'Spree::Product' do
weight { 10.0 }
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

FactoryBot.modify do
factory :shipment do
FactoryBot.define do
factory :shipment, class: 'Spree::Shipment' do
transient do
inventory_units { 1 }
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

FactoryBot.define do
factory :shipping_category, class: 'Spree::ShippingCategory' do
name { 'normal' }
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

FactoryBot.modify do
factory :shipping_method do
FactoryBot.define do
factory :shipping_method, class: 'Spree::ShippingMethod' do
admin_name { 'Stuff' }
available_to_users { true }
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

FactoryBot.modify do
factory :stock_location do
FactoryBot.define do
factory :stock_location, class: 'Spree::StockLocation'do
address1 { '131 S 8th Ave' }
city { 'Manville' }
association(:state, name: 'New Jersey', abbr: 'NJ')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

FactoryBot.modify do
factory :variant do
FactoryBot.define do
factory :variant, class: 'Spree::Variant' do
weight { 10.0 }
end
end
12 changes: 6 additions & 6 deletions spec/solidus_easypost/parcel_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

allow(parcel_dimension_calculator).to receive(:compute).and_return(parcel_dimension)
allow(parcel_dimension).to receive(:to_h).and_return(dimension_hash)
allow(EasyPost::Parcel).to receive(:create).and_call_original
allow(SolidusEasypost.client.parcel).to receive(:create).and_call_original
end

context 'when there is only the weight set' do
Expand All @@ -27,7 +27,7 @@
.to have_received(:compute)
.with(package)

expect(EasyPost::Parcel)
expect(SolidusEasypost.client.parcel)
.to have_received(:create)
.with({ weight: 10.to_f })

Expand All @@ -47,7 +47,7 @@
.to have_received(:compute)
.with(package)

expect(EasyPost::Parcel)
expect(SolidusEasypost.client.parcel)
.to have_received(:create)
.with({ weight: 10.to_f, height: 2.to_f, width: 3.to_f, depth: 4.to_f })

Expand All @@ -69,7 +69,7 @@

allow(parcel_dimension_calculator).to receive(:compute).and_return(parcel_dimension)
allow(parcel_dimension).to receive(:to_h).and_return(dimension_hash)
allow(EasyPost::Parcel).to receive(:create).and_call_original
allow(SolidusEasypost.client.parcel).to receive(:create).and_call_original
end

context 'when there is only the weight set' do
Expand All @@ -82,7 +82,7 @@
.to have_received(:compute)
.with(return_authorization)

expect(EasyPost::Parcel)
expect(SolidusEasypost.client.parcel)
.to have_received(:create)
.with({ weight: 10.to_f })

Expand All @@ -102,7 +102,7 @@
.to have_received(:compute)
.with(return_authorization)

expect(EasyPost::Parcel)
expect(SolidusEasypost.client.parcel)
.to have_received(:create)
.with({ weight: 10.to_f, height: 2.to_f, width: 3.to_f, depth: 4.to_f })

Expand Down
4 changes: 2 additions & 2 deletions spec/solidus_easypost/shipping_method_selector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
context 'when a shipping method for the given carrier and service exists' do
it 'returns the existing shipping method' do
shipping_method = create(:shipping_method, carrier: 'USPS', service_level: 'Express')
easypost_rate = EasyPost::Rate.construct_from('carrier' => 'USPS', 'service' => 'Express')
easypost_rate = SolidusEasypost.client.rate.construct_from('carrier' => 'USPS', 'service' => 'Express')

selector = described_class.new
selected_shipping_method = selector.shipping_method_for(easypost_rate)
Expand All @@ -15,7 +15,7 @@
context 'when a shipping method for the given carrier and service does not exist' do
it 'creates a new shipping method' do
shipping_category = create(:shipping_category)
easypost_rate = EasyPost::Rate.construct_from('carrier' => 'USPS', 'service' => 'Express')
easypost_rate = SolidusEasypost.client.rate.construct_from('carrier' => 'USPS', 'service' => 'Express')

selector = described_class.new
selected_shipping_method = selector.shipping_method_for(easypost_rate)
Expand Down
2 changes: 1 addition & 1 deletion spec/solidus_easypost/shipping_rate_calculator_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RSpec.describe SolidusEasypost::ShippingRateCalculator do
describe '#compute' do
it 'returns the amount on the EasyPost rate' do
easypost_rate = EasyPost::Rate.construct_from('rate' => 25.0)
easypost_rate = SolidusEasypost.client.rate.construct_from('rate' => 25.0)

calculator = described_class.new
computed_rate = calculator.compute(easypost_rate)
Expand Down
6 changes: 5 additions & 1 deletion spec/support/easypost.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# frozen_string_literal: true

RSpec.configure do |config|
config.before { EasyPost.api_key = 'CvzYtuda6KRI9JjG7SAHbA' }
config.before {
SolidusEasypost.configure do |config|
config.api_key = 'CvzYtuda6KRI9JjG7SAHbA'
end
}
end
Loading