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

Elseabay #23

Open
wants to merge 23 commits into
base: elseabay
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
df8e6c5
add Shipment resource
Jan 19, 2016
8e0306c
add .env file to ignore
Jan 19, 2016
0d2fbfb
dotenv gem
Jan 19, 2016
4ddf54e
mocked out methods for getting in data and sending out to USPS and UP…
Jan 19, 2016
22dc695
mocked out methods for getting in data and sending out to USPS and UP…
Jan 20, 2016
7c7a195
made helper for shipments controller in controllers concenrs
Jan 20, 2016
141e2f4
changed routes and other minor things
Jan 20, 2016
6fbfcb6
nothing important
emgord Jan 20, 2016
5a946b0
Merge branch 'elseabay' of github.com:lgranger/shipping-service into …
emgord Jan 20, 2016
add2164
some let things for specs
Jan 20, 2016
bccca29
Merge branch 'elseabay' of github.com:lgranger/shipping-service into …
emgord Jan 20, 2016
a64f3ea
working on estimate method
emgord Jan 21, 2016
a6d47d2
add origin and destination private params methods
Jan 21, 2016
8f6aa76
moving dotenv-rails around
Jan 21, 2016
3ad9f6f
api calls to ups and usps working with curl test
Jan 21, 2016
bdbce0d
combining usps and ups returned json into one json object for returning
Jan 21, 2016
849b8e2
cleaning up unused files and code
Jan 21, 2016
4244d55
remove shipment model and related schema
Jan 21, 2016
2d4b8b2
100% coverages
Jan 21, 2016
f018d58
bad data to API results in return of generic bad data error
Jan 22, 2016
f53e1b4
trying to add delivery estimate to shipping controller
emgord Jan 22, 2016
db0d73d
fix merge conflicts
emgord Jan 22, 2016
85deb62
estimate returns a hash instead of array
emgord Jan 22, 2016
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,4 @@ bower.json
# Ignore pow environment settings
.powenv
coverage
.env
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ gem 'sdoc', '~> 0.4.0', group: :doc
# Use active_shipping for shipping amounts
gem 'active_shipping'

#use dotenv for secrets
gem 'dotenv-rails'

gem 'responders', '~> 2.0'

# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

Expand All @@ -41,6 +46,7 @@ group :development, :test do
gem 'rspec-rails'
gem 'simplecov', require: false
gem 'factory_girl_rails'

end

group :development do
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ GEM
debug_inspector (0.0.2)
diff-lcs (1.2.5)
docile (1.1.5)
dotenv (2.1.0)
dotenv-rails (2.1.0)
dotenv (= 2.1.0)
railties (>= 4.0, < 5.1)
erubis (2.7.0)
execjs (2.6.0)
factory_girl (4.5.0)
Expand Down Expand Up @@ -131,6 +135,8 @@ GEM
rake (10.5.0)
rdoc (4.2.1)
json (~> 1.4)
responders (2.1.1)
railties (>= 4.2.0, < 5.1)
rspec-core (3.4.1)
rspec-support (~> 3.4.0)
rspec-expectations (3.4.0)
Expand Down Expand Up @@ -198,11 +204,13 @@ DEPENDENCIES
binding_of_caller
byebug
coffee-rails (~> 4.1.0)
dotenv-rails
factory_girl_rails
jbuilder (~> 2.0)
jquery-rails
pry-rails
rails (= 4.2.5)
responders (~> 2.0)
rspec-rails
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/shipments.coffee
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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/shipments.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Shipments controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception

end
59 changes: 59 additions & 0 deletions app/controllers/shipments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class ShipmentsController < ApplicationController
respond_to :json

def estimate
begin
weight = (package_params[:weight]).to_i
dimensions = [(package_params[:length]).to_i, (package_params[:width]).to_i, (package_params[:height]).to_i]
package = ActiveShipping::Package.new(weight, dimensions)
packages = [package]
origin = ActiveShipping::Location.new(origin_params)
destination = ActiveShipping::Location.new(destination_params)

ups = ups_rates(origin, destination, packages)
usps = usps_rates(origin, destination, packages)
rates = ups.merge(usps)
render :json => rates.as_json, :status => :ok
rescue
render :json => { error: :bad_data, message: "You must provide valid data for package size, package weight, origin location and destination location." }, status: :bad_request

Choose a reason for hiding this comment

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

It's not a bad idea to let users know what 'valid date' is by giving them examples in the message.

Choose a reason for hiding this comment

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

Most Ruby style guides recommend a line length of 80 characters. You can wrap your code by using indentations to show the continuance of the previous line. Here is a StackOverflow example

end
end

private

Choose a reason for hiding this comment

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

great use of private methods / separating the logic by its use/user base


def package_params
params.require(:package).permit(:weight, :length, :width, :height)
end

def origin_params
params.require(:origin).permit(:country, :state, :province, :city, :zip, :postal_code)
end

def destination_params
params.require(:destination).permit(:country, :state, :province, :city, :zip, :postal_code)
end

Choose a reason for hiding this comment

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

remove extra line


def ups_rates(origin, destination, packages)
ups = ActiveShipping::UPS.new(login: ENV['UPS_LOGIN'], password: ENV['UPS_PASSWORD'], key: ENV['UPS_KEY'])
response = ups.find_rates(origin, destination, packages)

ups_rate_response = {}
response.rates.sort_by(&:price).each do |rate|
ups_rate_response[rate.service_name] = rate.price
end
return ups_rate_response
end

def usps_rates(origin, destination, packages)
usps = ActiveShipping::USPS.new(login: ENV['USPS_USERNAME'])
response = usps.find_rates(origin, destination, packages)

usps_rate_response = {}
response.rates.sort_by(&:price).each do |rate|
usps_rate_response[rate.service_name] = rate.price
end
return usps_rate_response
end

end
2 changes: 2 additions & 0 deletions app/helpers/shipments_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ShipmentsHelper
end

Choose a reason for hiding this comment

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

this file is not serving any purpose so it should be removed

55 changes: 1 addition & 54 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,56 +1,3 @@
Rails.application.routes.draw do
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

# You can have the root of your site routed with "root"
# root 'welcome#index'

# Example of regular route:
# get 'products/:id' => 'catalog#view'

# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products

# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end

# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end

# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end

# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable

# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
post 'estimate' => 'shipments#estimate'
end
16 changes: 16 additions & 0 deletions db/schema.rb
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
29 changes: 29 additions & 0 deletions spec/controllers/shipments_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'rails_helper'
require 'pry'

RSpec.describe ShipmentsController, type: :controller do
describe "POST 'estimate'" do
let(:params) do
{origin: {country: 'US',
state: 'CA',
city: 'Beverly Hills',
zip: '90210'},

destination: {country: 'CA',
province: 'ON',
city: 'Ottawa',
postal_code: 'K1P 1J1'},
package: { weight: 100,
length: 93,

Choose a reason for hiding this comment

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

Indent this and the following lines one more space to line up with weight. On that note I like how you have space out destination, it is very easy to read.

width: 10,
height: 10}
}
end

it "is successful" do
post :estimate, params
expect(response.response_code).to eq 200
end
end

end

Choose a reason for hiding this comment

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

last end should line up with beginning of describe block. Solid post request test!

15 changes: 15 additions & 0 deletions spec/helpers/shipments_helper_spec.rb
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 ShipmentsHelper. For example:
#
# describe ShipmentsHelper 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 ShipmentsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end