forked from AdaGold/shipping-service
-
Notifications
You must be signed in to change notification settings - Fork 14
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
Kelly/Meighan #17
Open
knaydee
wants to merge
18
commits into
Ada-C4:fiona/master
Choose a base branch
from
knaydee:fiona/master
base: fiona/master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Kelly/Meighan #17
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
901b3ff
install rspec
kedevlin 1d71f5d
add active_shipping to gemfile
kedevlin 8a5370c
generate estimates controller and modify routes
kedevlin 8175e48
create first draft estimate method for UPS
kedevlin dfaef82
require 'dotenv' in config/application.rb
kedevlin 6b8ef7f
complete json response for a UPS estimate request
kedevlin 82acb5f
update controller#estimate method so that it will return both UPS and…
kedevlin d2355e1
Add spaces
kedevlin bbfafb7
add vcr gem and configuration
kedevlin 53cd989
Add get_usps_estimates to estimates_controller
knaydee 76ccf7a
Fix merge conflicts
knaydee 7cd9be9
add methods to convert ups service codes to service names
kedevlin fdb44f7
Tests
knaydee 6fcc76f
Merge conflict fix
knaydee 68d1eea
98% test coverage
knaydee 501cd89
remove dotenv from application.rb
knaydee 69e3e77
Add rails_12factor gem
knaydee 2a4ebbc
begin, rescue logic
knaydee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,5 @@ | |
*.DS_Store | ||
*.keep | ||
coverage | ||
|
||
/spec/vcr/*.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--color | ||
--require spec_helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Place all the behaviors and hooks related to the matching controller here. | ||
# All this logic will automatically be available in application.js. | ||
# You can use CoffeeScript in this file: http://coffeescript.org/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Place all the styles related to the estimates controller here. | ||
// They will automatically be included in application.css. | ||
// You can use Sass (SCSS) here: http://sass-lang.com/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
require 'active_shipping' | ||
require './lib/ups_services' | ||
|
||
class EstimatesController < ApplicationController | ||
# create constants for origin object, package grams_or_ounces and package dimensions, and country | ||
# also create carrier constants which is a ups carrier and a fedex carrier? | ||
UPS = ActiveShipping::UPS.new(login: ENV['UPS_LOGIN'], password: ENV['UPS_PASSWORD'], key: ENV['UPS_KEY']) | ||
|
||
USPS = ActiveShipping::USPS.new(login: ENV['USPS_LOGIN']) | ||
|
||
# assume all packages are being sent within the US | ||
COUNTRY = "US" | ||
# assume all packages are originating from Ada's betsy distribution center | ||
ORIGIN = ActiveShipping::Location.new(country: COUNTRY, state: 'WA', city: 'Seattle', zip: '98101') | ||
# assume all packages have the same ounces and dimensions, for now | ||
OUNCES = 120 | ||
DIMENSIONS = [15, 10, 4.5] | ||
|
||
def estimate | ||
# only one package for all products in order, for now | ||
package = [ActiveShipping::Package.new(OUNCES, DIMENSIONS, units: :imperial, value: params[:value])] | ||
# destination address info comes from query params provided from betsy app's API call | ||
destination = ActiveShipping::Location.new(country: COUNTRY, state: params[:destination][:state], city: params[:destination][:city], zip: params[:destination][:zip]) | ||
# method call | ||
begin | ||
ups_estimates = UpsServices.transform_codes_into_names(get_ups_estimates(ORIGIN, destination, package)) | ||
rescue | ||
return render :json => [], :status => :bad_request | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be nice to log the errors so that bad requests can easily be fixed. |
||
end | ||
# method call | ||
begin | ||
usps_estimates = get_usps_estimates(ORIGIN, destination, package) | ||
rescue | ||
return render :json => [], :status => :bad_request | ||
end | ||
# response includes rates and dates from both UPS and USPS | ||
response = {"UPS Service Options" => ups_estimates, "USPS Service Options" => usps_estimates } | ||
render :json => response.as_json, :status => :ok | ||
end | ||
|
||
private | ||
|
||
def get_ups_estimates(origin, destination, package) | ||
rates_response = UPS.find_rates(ORIGIN, destination, package) | ||
delivery_dates_response = UPS.get_delivery_date_estimates(ORIGIN, destination, package) | ||
# shipping_estimate will be a hash made up of service_code keys | ||
# each key points to a hash which contains the cost and delivery date estimate | ||
# for the corresponding service type. | ||
shipping_estimate = Hash.new | ||
rates_response.rates.each do |rate| | ||
# skip this rate if service code is nil | ||
next if rate.service_code.nil? | ||
# otherwise, create a key for the corresponding service code | ||
# the value is a hash containing the corresponding cost | ||
shipping_estimate["#{rate.service_code}"] = { "cost" => "#{rate.total_price}" } | ||
end | ||
delivery_dates_response.delivery_estimates.each do |estimate| | ||
# skip this estimate if service code is nil | ||
next if estimate.service_code.nil? | ||
# otherwise check to see if the service code already exists in the hash | ||
if shipping_estimate["#{estimate.service_code}"].nil? | ||
# if the service code doesn't yet exist in the hash, create a key for it | ||
# with the value being a hash corresponding to the date | ||
shipping_estimate["#{estimate.service_code}"] = { "date" => "#{estimate.date}" } | ||
else | ||
# if it already exists, merge the date corresponding to this estimate into the the | ||
# hash associated with this service code key | ||
shipping_estimate["#{estimate.service_code}"].merge!({ "date" => "#{estimate.date}"}) | ||
end | ||
end | ||
return shipping_estimate | ||
end | ||
|
||
def get_usps_estimates(origin, destination, package) | ||
rates_response = USPS.find_rates(ORIGIN, destination, package) | ||
usps_rates = rates_response.rates | ||
shipping_estimate = Hash.new | ||
usps_rates.each do |rate| | ||
next if rate.service_name.nil? | ||
shipping_estimate["#{rate.service_name}"] = { "cost" => "#{rate.price}", "date" => "#{rate.delivery_date}"} | ||
end | ||
return shipping_estimate | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module EstimatesHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# encoding: UTF-8 | ||
# This file is auto-generated from the current state of the database. Instead | ||
# of editing this file, please use the migrations feature of Active Record to | ||
# incrementally modify your database, and then regenerate this schema definition. | ||
# | ||
# Note that this schema.rb definition is the authoritative source for your | ||
# database schema. If you need to create the application database on another | ||
# system, you should be using db:schema:load, not running all the migrations | ||
# from scratch. The latter is a flawed and unsustainable approach (the more migrations | ||
# you'll amass, the slower it'll run and the greater likelihood for issues). | ||
# | ||
# It's strongly recommended that you check this file into your version control system. | ||
|
||
ActiveRecord::Schema.define(version: 0) do | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class UpsServices | ||
SERVICES = { | ||
"01" => "UPS Next Day Air", | ||
"02" => "UPS Second Day Air", | ||
"03" => "UPS Ground", | ||
"07" => "UPS Worldwide Express", | ||
"08" => "UPS Worldwide Expedited", | ||
"11" => "UPS Standard", | ||
"12" => "UPS Three-Day Select", | ||
"13" => "UPS Next Day Air Saver", | ||
"14" => "UPS Next Day Air Early A.M.", | ||
"54" => "UPS Worldwide Express Plus", | ||
"59" => "UPS Second Day Air A.M.", | ||
"65" => "UPS Saver", | ||
"82" => "UPS Today Standard", | ||
"83" => "UPS Today Dedicated Courier", | ||
"84" => "UPS Today Intercity", | ||
"85" => "UPS Today Express", | ||
"86" => "UPS Today Express Saver" | ||
} | ||
|
||
def self.transform_codes_into_names(hash) | ||
mappings = Hash.new | ||
hash.keys.each do |key| | ||
mappings[key] = SERVICES[key] | ||
end | ||
Hash[hash.map {|k, v| [mappings[k], v] }] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'rails_helper' | ||
require 'support/vcr_setup' | ||
|
||
RSpec.describe EstimatesController, type: :controller do | ||
let!(:package) do | ||
{ | ||
value: 2000 | ||
} | ||
end | ||
|
||
let!(:destination) do | ||
{ | ||
destination: { | ||
state: "CA", | ||
city: "San Francisco", | ||
zip: 94104 | ||
} | ||
} | ||
end | ||
|
||
describe "GET 'estimate'" do | ||
|
||
it "is successful", :vcr do | ||
get :estimate, destination.merge(package) | ||
expect(response.response_code).to eq 200 | ||
end | ||
|
||
# describe 'get_usps_estimates' do | ||
# it 'returns a hash with service names and cost' do | ||
# expect(response.body).to eq "" | ||
# end | ||
# end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'rails_helper' | ||
|
||
# Specs in this file have access to a helper object that includes | ||
# the EstimatesHelper. For example: | ||
# | ||
# describe EstimatesHelper do | ||
# describe "string concat" do | ||
# it "concats two strings with spaces" do | ||
# expect(helper.concat_strings("this","that")).to eq("this that") | ||
# end | ||
# end | ||
# end | ||
RSpec.describe EstimatesHelper, type: :helper do | ||
# pending "add some examples to (or delete) #{__FILE__}" | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It helps readability to put each on its own line.