diff --git a/Gemfile b/Gemfile index dd75501..93bcfb8 100644 --- a/Gemfile +++ b/Gemfile @@ -4,7 +4,7 @@ source 'https://rubygems.org' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.2.5' # Use sqlite3 as the database for Active Record -gem 'sqlite3' + # Use SCSS for stylesheets gem 'sass-rails', '~> 5.0' # Use Uglifier as compressor for JavaScript assets @@ -33,6 +33,7 @@ gem 'sdoc', '~> 0.4.0', group: :doc # gem 'capistrano-rails', group: :development gem 'active_shipping' +gem 'pg' group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console @@ -41,6 +42,8 @@ group :development, :test do gem 'pry' gem 'simplecov' gem 'factory_girl_rails' + gem 'dotenv-rails' + end group :development do @@ -49,6 +52,7 @@ group :development do gem 'better_errors' gem 'binding_of_caller' + gem 'sqlite3' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' diff --git a/Gemfile.lock b/Gemfile.lock index 6f100cb..fc4f957 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -94,6 +98,7 @@ GEM multi_json (1.11.2) nokogiri (1.6.7.1) mini_portile2 (~> 2.0.0.rc2) + pg (0.18.4) pry (0.10.3) coderay (~> 1.1.0) method_source (~> 0.8.1) @@ -196,9 +201,11 @@ DEPENDENCIES binding_of_caller byebug coffee-rails (~> 4.1.0) + dotenv-rails factory_girl_rails jbuilder (~> 2.0) jquery-rails + pg pry rails (= 4.2.5) rspec-rails (~> 3.0) diff --git a/app/controllers/packages_controller.rb b/app/controllers/packages_controller.rb index d240f22..8ca53ef 100644 --- a/app/controllers/packages_controller.rb +++ b/app/controllers/packages_controller.rb @@ -1,15 +1,38 @@ class PackagesController < ApplicationController def rates - package = ActiveShipping::Package.new(100, [10, 20, 30], :units => :metric) - origin = ActiveShipping::Location.new(country: 'US', - state: 'CA', - city: 'Beverly Hills', - zip: '90210') - - destination = ActiveShipping::Location.new(country: 'CA', - province: 'ON', - city: 'Ottawa', - postal_code: 'K1P 1J1') + if params["package"]["width"].to_i == 0 || params["package"]["height"].to_i == 0 || params["package"]["length"].to_i == 0 || params["package"]["weight"].to_i == 0 + return render :json => ["Incorrect or missing parameters for package"], :status => :bad_request + end + + package = ActiveShipping::Package.new(params[:package][:weight].to_i, [params[:package][:length].to_i, params[:package][:width].to_i, params[:package][:height].to_i], :units => params[:package][:units]) + + + begin + origin = ActiveShipping::Location.new(params[:origin]) + rescue + return render :json => ["Incorrect or missing parameters for origin address"], :status => :bad_request + end + + begin + destination = ActiveShipping::Location.new(params[:destination]) + rescue + return render :json => ["Incorrect or missing parameters for destination address"], :status => :bad_request + end + + begin + usps = ActiveShipping::USPS.new(login: ENV["USPS_USERNAME"], password: ENV["USPS_PASSWORD"]) + usps_response = usps.find_rates(origin, destination, package) + usps_rates = usps_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]} + + ups = ActiveShipping::UPS.new(login: ENV["UPS_ACCOUNT_NAME"], password: ENV["UPS_ACCOUNT_PASSWORD"], key: ENV["UPS_ACCESS_KEY"]) + ups_response = ups.find_rates(origin, destination, package) + ups_rates = ups_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]} + + all_rates = { ups: ups_rates, usps: usps_rates} + render :json => all_rates.as_json + rescue + return render :json => "There has been an error", :status => :no_content + end end end diff --git a/config/routes.rb b/config/routes.rb index c87acde..3cac19d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,7 @@ Rails.application.routes.draw do get 'rates' => 'packages#rates' - + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/coverage/.last_run.json b/coverage/.last_run.json index 3cb5e70..c74e61f 100644 --- a/coverage/.last_run.json +++ b/coverage/.last_run.json @@ -1,5 +1,5 @@ { "result": { - "covered_percent": 100.0 + "covered_percent": 96.0 } } diff --git a/coverage/.resultset.json b/coverage/.resultset.json index 746be54..80610d1 100644 --- a/coverage/.resultset.json +++ b/coverage/.resultset.json @@ -1,22 +1,55 @@ { "RSpec": { "coverage": { - "/Users/katherinedefliese/Documents/Ada_homework/project-forks/shipping-service/app/helpers/application_helper.rb": [ + "/Users/jessica/Documents/Ada/project-forks/shipping_service_project/shipping-service/app/helpers/application_helper.rb": [ 1, null ], - "/Users/katherinedefliese/Documents/Ada_homework/project-forks/shipping-service/app/helpers/packages_helper.rb": [ + "/Users/jessica/Documents/Ada/project-forks/shipping_service_project/shipping-service/app/helpers/packages_helper.rb": [ 1, null ], - "/Users/katherinedefliese/Documents/Ada_homework/project-forks/shipping-service/app/controllers/packages_controller.rb": [ + "/Users/jessica/Documents/Ada/project-forks/shipping_service_project/shipping-service/app/controllers/packages_controller.rb": [ 1, null, 1, + 6, + 1, + null, + null, + 5, + null, + null, + 5, + 5, + null, + 1, + null, + null, + 4, + 4, + null, + 1, + null, + null, + 3, + 3, + 3, + 36, + null, + 3, + 3, + 21, + null, + 3, + 3, + null, + 0, + null, null, null ], - "/Users/katherinedefliese/Documents/Ada_homework/project-forks/shipping-service/app/controllers/application_controller.rb": [ + "/Users/jessica/Documents/Ada/project-forks/shipping_service_project/shipping-service/app/controllers/application_controller.rb": [ 1, null, null, @@ -24,6 +57,6 @@ null ] }, - "timestamp": 1453243290 + "timestamp": 1453502619 } } diff --git a/coverage/index.html b/coverage/index.html index ad227ca..cd42d27 100644 --- a/coverage/index.html +++ b/coverage/index.html @@ -14,27 +14,27 @@ loading