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 @@
-
Generated
2016-01-19T14:41:30-08:00
+
Generated
2016-01-22T14:43:39-08:00
All Files
- (100.0%
+ (96.0%
covered at
-
- 1.0
+
+ 4.61
hits/line)
4 files in total.
- 6 relevant lines.
- 6 lines covered and
- 0 lines missed
+ 25 relevant lines.
+ 24 lines covered and
+ 1 lines missed
@@ -51,7 +51,7 @@
- app/controllers/application_controller.rb |
+ app/controllers/application_controller.rb |
100.0 % |
5 |
2 |
@@ -61,17 +61,17 @@
- app/controllers/packages_controller.rb |
- 100.0 % |
- 5 |
- 2 |
- 2 |
- 0 |
- 1.0 |
+ app/controllers/packages_controller.rb |
+ 95.24 % |
+ 38 |
+ 21 |
+ 20 |
+ 1 |
+ 5.3 |
- app/helpers/application_helper.rb |
+ app/helpers/application_helper.rb |
100.0 % |
2 |
1 |
@@ -81,7 +81,7 @@
- app/helpers/packages_helper.rb |
+ app/helpers/packages_helper.rb |
100.0 % |
2 |
1 |
@@ -99,20 +99,20 @@
Controllers
- (100.0%
+ (95.65%
covered at
-
- 1.0
+
+ 4.93
hits/line)
2 files in total.
- 4 relevant lines.
- 4 lines covered and
- 0 lines missed
+ 23 relevant lines.
+ 22 lines covered and
+ 1 lines missed
@@ -129,7 +129,7 @@
- app/controllers/application_controller.rb |
+ app/controllers/application_controller.rb |
100.0 % |
5 |
2 |
@@ -139,13 +139,13 @@
- app/controllers/packages_controller.rb |
- 100.0 % |
- 5 |
- 2 |
- 2 |
- 0 |
- 1.0 |
+ app/controllers/packages_controller.rb |
+ 95.24 % |
+ 38 |
+ 21 |
+ 20 |
+ 1 |
+ 5.3 |
@@ -260,7 +260,7 @@
- app/helpers/application_helper.rb |
+ app/helpers/application_helper.rb |
100.0 % |
2 |
1 |
@@ -270,7 +270,7 @@
- app/helpers/packages_helper.rb |
+ app/helpers/packages_helper.rb |
100.0 % |
2 |
1 |
@@ -331,7 +331,7 @@
-
+
-
+
@@ -411,13 +411,211 @@
100.0 % covered
def rates
-
+
+ 6
+
+ 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
+
+
+
+ 1
+
+ return render :json => ["Incorrect or missing parameters for package"], :status => :bad_request
+
+
+
+
+
+ end
+
+
+
+
+
+
+
+
+
+ 5
+
+ 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])
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ 5
+
+ begin
+
+
+
+ 5
+
+ origin = ActiveShipping::Location.new(params[:origin])
+
+
+
+
+
+ rescue
+
+
+
+ 1
+
+ return render :json => ["Incorrect or missing parameters for origin address"], :status => :bad_request
+
+
+
+
+
+ end
+
+
+
+
+
+
+
+
+
+ 4
+
+ begin
+
+
+
+ 4
+
+ destination = ActiveShipping::Location.new(params[:destination])
+
+
+
+
+
+ rescue
+
+
+
+ 1
+
+ return render :json => ["Incorrect or missing parameters for destination address"], :status => :bad_request
+
+
+
+
+
+ end
+
+
+
+
+
+
+
+
+
+ 3
+
+ begin
+
+
+
+ 3
+
+ usps = ActiveShipping::USPS.new(login: ENV["USPS_USERNAME"], password: ENV["USPS_PASSWORD"])
+
+
+
+ 3
+
+ usps_response = usps.find_rates(origin, destination, package)
+
+
+
+ 36
+
+ usps_rates = usps_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
+
+
+
+
+
+
+
+
+
+ 3
+
+ ups = ActiveShipping::UPS.new(login: ENV["UPS_ACCOUNT_NAME"], password: ENV["UPS_ACCOUNT_PASSWORD"], key: ENV["UPS_ACCESS_KEY"])
+
+
+
+ 3
+
+ ups_response = ups.find_rates(origin, destination, package)
+
+
+
+ 21
+
+ ups_rates = ups_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price]}
+
+
+
+
+
+
+
+
+
+ 3
+
+ all_rates = { ups: ups_rates, usps: usps_rates}
+
+
+
+ 3
+
+ render :json => all_rates.as_json
+
+
+
+
+
+ rescue
+
+
+
+
+
+ return render :json => "There has been an error", :status => :no_content
+
+
+
+
+
+ end
+
+
+
end
-
+
end
@@ -427,7 +625,7 @@ 100.0 % covered
-