-
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
Elseabay #23
base: elseabay
Are you sure you want to change the base?
Elseabay #23
Changes from all commits
df8e6c5
8e0306c
0d2fbfb
4ddf54e
22dc695
7c7a195
141e2f4
6fbfcb6
5a946b0
add2164
bccca29
a64f3ea
a6d47d2
8f6aa76
3ad9f6f
bdbce0d
849b8e2
4244d55
2d4b8b2
f018d58
f53e1b4
db0d73d
85deb62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,3 +72,4 @@ bower.json | |
# Ignore pow environment settings | ||
.powenv | ||
coverage | ||
.env |
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/ |
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/ |
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 |
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 | ||
end | ||
end | ||
|
||
private | ||
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. 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 | ||
|
||
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. 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module ShipmentsHelper | ||
end | ||
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. this file is not serving any purpose so it should be removed |
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 |
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 |
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, | ||
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. 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 | ||
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. last end should line up with beginning of describe block. Solid post request test! |
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 |
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's not a bad idea to let users know what 'valid date' is by giving them examples in the message.
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.
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