From c79ccdb9c36f4feb90bb516d728edd5ac94fbf72 Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Thu, 21 Jan 2016 11:48:46 -0800 Subject: [PATCH 01/20] created packages controller and moved rspec gem --- Gemfile | 4 ++-- app/assets/javascripts/packages.coffee | 3 +++ app/assets/stylesheets/packages.scss | 3 +++ app/controllers/packages_controller.rb | 2 ++ app/helpers/packages_helper.rb | 2 ++ spec/controllers/packages_controller_spec.rb | 0 spec/helpers/packages_helper_spec.rb | 0 7 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 app/assets/javascripts/packages.coffee create mode 100644 app/assets/stylesheets/packages.scss create mode 100644 app/controllers/packages_controller.rb create mode 100644 app/helpers/packages_helper.rb create mode 100644 spec/controllers/packages_controller_spec.rb create mode 100644 spec/helpers/packages_helper_spec.rb diff --git a/Gemfile b/Gemfile index 32f232a..97711d5 100644 --- a/Gemfile +++ b/Gemfile @@ -34,6 +34,8 @@ gem 'httparty' group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' + gem 'rspec-rails' + gem 'simplecov', require: false end group :development do @@ -50,9 +52,7 @@ group :development do end group :test do - gem 'rspec-rails' gem 'factory_girl_rails', '~> 4.0' - gem 'simplecov', require: false end group :production do diff --git a/app/assets/javascripts/packages.coffee b/app/assets/javascripts/packages.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/packages.coffee @@ -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/ diff --git a/app/assets/stylesheets/packages.scss b/app/assets/stylesheets/packages.scss new file mode 100644 index 0000000..2412a5a --- /dev/null +++ b/app/assets/stylesheets/packages.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the packages controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/packages_controller.rb b/app/controllers/packages_controller.rb new file mode 100644 index 0000000..64d9e30 --- /dev/null +++ b/app/controllers/packages_controller.rb @@ -0,0 +1,2 @@ +class PackagesController < ApplicationController +end diff --git a/app/helpers/packages_helper.rb b/app/helpers/packages_helper.rb new file mode 100644 index 0000000..bb6b3e9 --- /dev/null +++ b/app/helpers/packages_helper.rb @@ -0,0 +1,2 @@ +module PackagesHelper +end diff --git a/spec/controllers/packages_controller_spec.rb b/spec/controllers/packages_controller_spec.rb new file mode 100644 index 0000000..e69de29 diff --git a/spec/helpers/packages_helper_spec.rb b/spec/helpers/packages_helper_spec.rb new file mode 100644 index 0000000..e69de29 From 387d907eef4f60d499fc900a8bfb59489089ddf2 Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Thu, 21 Jan 2016 11:57:38 -0800 Subject: [PATCH 02/20] added more empty files --- app/controllers/packages_controller.rb | 1 + config/routes.rb | 1 + spec/controllers/packages_controller_spec.rb | 4 ++++ spec/models/packages.rb | 4 ++++ 4 files changed, 10 insertions(+) create mode 100644 spec/models/packages.rb diff --git a/app/controllers/packages_controller.rb b/app/controllers/packages_controller.rb index 64d9e30..2616c30 100644 --- a/app/controllers/packages_controller.rb +++ b/app/controllers/packages_controller.rb @@ -1,2 +1,3 @@ class PackagesController < ApplicationController + end diff --git a/config/routes.rb b/config/routes.rb index 3f66539..4d7e1fe 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + get 'packages' => 'packages#index' # 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/spec/controllers/packages_controller_spec.rb b/spec/controllers/packages_controller_spec.rb index e69de29..67d233e 100644 --- a/spec/controllers/packages_controller_spec.rb +++ b/spec/controllers/packages_controller_spec.rb @@ -0,0 +1,4 @@ +require 'rails_helper' + +RSpec.describe PackagesController, type: :controller do +end diff --git a/spec/models/packages.rb b/spec/models/packages.rb new file mode 100644 index 0000000..46bec20 --- /dev/null +++ b/spec/models/packages.rb @@ -0,0 +1,4 @@ +require 'rails_helper' + +RSpec.describe Package, type: :model do +end From d64262ce60a4ff72bd247552a72ca167c8ceb871 Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Thu, 21 Jan 2016 14:26:31 -0800 Subject: [PATCH 03/20] nothing --- app/controllers/packages_controller.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/controllers/packages_controller.rb b/app/controllers/packages_controller.rb index 2616c30..cf9fa57 100644 --- a/app/controllers/packages_controller.rb +++ b/app/controllers/packages_controller.rb @@ -1,3 +1,6 @@ +require 'httparty' + class PackagesController < ApplicationController + end From 5c45507971698ee089632b0e3c395c7fceff43de Mon Sep 17 00:00:00 2001 From: Tammy Date: Thu, 21 Jan 2016 14:29:30 -0800 Subject: [PATCH 04/20] added service_shipping gem --- Gemfile | 2 +- Gemfile.lock | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 32f232a..a2b4285 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ source 'https://rubygems.org' - +gem 'active_shipping' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.2.5' # Use SCSS for stylesheets diff --git a/Gemfile.lock b/Gemfile.lock index b7f6c31..c0812f2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -20,6 +20,14 @@ GEM erubis (~> 2.7.0) rails-dom-testing (~> 1.0, >= 1.0.5) rails-html-sanitizer (~> 1.0, >= 1.0.2) + active_shipping (1.6.5) + active_utils (~> 3.2.0) + activesupport (>= 3.2, < 5.0.0) + nokogiri (>= 1.6) + quantified (~> 1.0.1) + active_utils (3.2.0) + activesupport (>= 3.2) + i18n activejob (4.2.5) activesupport (= 4.2.5) globalid (>= 0.3.0) @@ -97,6 +105,7 @@ GEM slop (~> 3.4) pry-rails (0.3.4) pry (>= 0.9.10) + quantified (1.0.1) rack (1.6.4) rack-test (0.6.3) rack (>= 1.0) @@ -189,6 +198,7 @@ PLATFORMS ruby DEPENDENCIES + active_shipping better_errors binding_of_caller byebug From 7dd78f57997ae04b67214d9c935094c0d31dd800 Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Thu, 21 Jan 2016 14:53:05 -0800 Subject: [PATCH 05/20] changed route --- config/routes.rb | 2 +- spec/models/packages.rb | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) delete mode 100644 spec/models/packages.rb diff --git a/config/routes.rb b/config/routes.rb index 4d7e1fe..7bb78d4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - get 'packages' => 'packages#index' + get 'packages' => 'packages#receive_info' # 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/spec/models/packages.rb b/spec/models/packages.rb deleted file mode 100644 index 46bec20..0000000 --- a/spec/models/packages.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'rails_helper' - -RSpec.describe Package, type: :model do -end From b4d0d0f0d51f69d256ece8a8a935f7294f63717e Mon Sep 17 00:00:00 2001 From: Tammy Date: Thu, 21 Jan 2016 14:53:13 -0800 Subject: [PATCH 06/20] added method to the pachage controller and 2 methods for the model --- app/controllers/packages_controller.rb | 22 ++++++++++++++++++++ app/models/package.rb | 21 +++++++++++++++++++ db/migrate/20160121223301_create_packages.rb | 8 +++++++ db/schema.rb | 16 ++++++++++++++ spec/models/{packages.rb => package_spec.rb} | 1 + 5 files changed, 68 insertions(+) create mode 100644 app/models/package.rb create mode 100644 db/migrate/20160121223301_create_packages.rb create mode 100644 db/schema.rb rename spec/models/{packages.rb => package_spec.rb} (54%) diff --git a/app/controllers/packages_controller.rb b/app/controllers/packages_controller.rb index cf9fa57..22e474f 100644 --- a/app/controllers/packages_controller.rb +++ b/app/controllers/packages_controller.rb @@ -2,5 +2,27 @@ class PackagesController < ApplicationController + def receive_info + @hash = + end + def ups_price_estimate + + + end + + def usps_price_estimate + + end + + def ups_delivery_time + + end + + def usps_delivery_time + + end + + + end diff --git a/app/models/package.rb b/app/models/package.rb new file mode 100644 index 0000000..aec90f9 --- /dev/null +++ b/app/models/package.rb @@ -0,0 +1,21 @@ +class Package < ActiveRecord::Base + + def get_package + package = [ + ActiveShipping::Package.new(100, + [100,100], + valule: 10000) + ] + + return package + end + + def get_origin + origin = ActiveShipping::UPS.new(country: 'US', + state: 'WA', + city: 'Seattle', + zip: 98122) + return origin + end + +end diff --git a/db/migrate/20160121223301_create_packages.rb b/db/migrate/20160121223301_create_packages.rb new file mode 100644 index 0000000..a643419 --- /dev/null +++ b/db/migrate/20160121223301_create_packages.rb @@ -0,0 +1,8 @@ +class CreatePackages < ActiveRecord::Migration + def change + create_table :packages do |t| + + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..4dfbb16 --- /dev/null +++ b/db/schema.rb @@ -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 diff --git a/spec/models/packages.rb b/spec/models/package_spec.rb similarity index 54% rename from spec/models/packages.rb rename to spec/models/package_spec.rb index 46bec20..5c8bcbd 100644 --- a/spec/models/packages.rb +++ b/spec/models/package_spec.rb @@ -1,4 +1,5 @@ require 'rails_helper' RSpec.describe Package, type: :model do + pending "add some examples to (or delete) #{__FILE__}" end From c19181bfcfefed71717aa70b4aaa6ab1916624d9 Mon Sep 17 00:00:00 2001 From: Tammy Date: Thu, 21 Jan 2016 16:08:13 -0800 Subject: [PATCH 07/20] added env file --- .gitignore | 1 + app/controllers/packages_controller.rb | 35 +++++++++++++------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 14e3ed5..2c7219f 100644 --- a/.gitignore +++ b/.gitignore @@ -68,6 +68,7 @@ config/secrets.yml /vendor/assets/bower_components *.bowerrc bower.json +/.env # Ignore pow environment settings .powenv diff --git a/app/controllers/packages_controller.rb b/app/controllers/packages_controller.rb index 22e474f..ef5fbc4 100644 --- a/app/controllers/packages_controller.rb +++ b/app/controllers/packages_controller.rb @@ -3,24 +3,25 @@ class PackagesController < ApplicationController def receive_info - @hash = - end - def ups_price_estimate - - - end - - def usps_price_estimate - - end - - def ups_delivery_time - - end - - def usps_delivery_time - + hash = params + raise end + # + # def ups_price_estimate + # + # end + # + # def usps_price_estimate + # + # end + # + # def ups_delivery_time + # + # end + # + # def usps_delivery_time + # + # end From 00b20aa6f6ec113008f1eb152793dad9f8d9e18d Mon Sep 17 00:00:00 2001 From: Tammy Date: Thu, 21 Jan 2016 20:22:39 -0800 Subject: [PATCH 08/20] added timeout --- Gemfile | 1 + app/controllers/packages_controller.rb | 35 +++++++++++++++++++++++--- app/models/package.rb | 9 +++++++ config/application.rb | 1 + 4 files changed, 43 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index bf675f0..92491f7 100644 --- a/Gemfile +++ b/Gemfile @@ -21,6 +21,7 @@ gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc gem 'httparty' +gem 'dotenv-rails' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' diff --git a/app/controllers/packages_controller.rb b/app/controllers/packages_controller.rb index ef5fbc4..fe6d154 100644 --- a/app/controllers/packages_controller.rb +++ b/app/controllers/packages_controller.rb @@ -1,10 +1,39 @@ require 'httparty' class PackagesController < ApplicationController + include Timeout - def receive_info - hash = params - raise + def index + origin = get_origin + package = get_package + destination = get_destination(params) + + ups = ActiveShipping::UPS.new(login: ENV["UPS_LOGIN"], password: ENV["UPS_PASSWORD"], key: ENV["UPS_KEY"]) + usps = ActiveShipping::USPS.new(login: ENV["USPS_LOGIN"]) + + ups_response = ups.find_rates(origin, destination, package) + ups_rates = ups_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price, rate.delivery_date]} + + usps_response = usps.find_rates(origin, destination, packages) + usps_rates = usps_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price, rate.delivery_date]} + + begin + Timeout::timeout(10) do + if ups_rates && usps_rates + all_rates = ups_rates.concat(usps_rates) + render :json => all_rates.as_json, :status => :ok + elsif ups_rates + render :json => ups_rates.as_json, :status => :partial_content + elsif usps_rates + render :json => usps_rates.as_json, :status => :partial_content + else + render :json => [], :status => :no_content + end + end + rescue Timeout::Error + render :json => [], :status => :gateway_timeout + end + end end # # def ups_price_estimate diff --git a/app/models/package.rb b/app/models/package.rb index aec90f9..f1617b7 100644 --- a/app/models/package.rb +++ b/app/models/package.rb @@ -18,4 +18,13 @@ def get_origin return origin end + def get_destination(hash) + destination = ActiveShipping::UPS.new( + country: 'US', + state: hash[:state], + city: hash[:city], + zip: hash[:zip]) + + return destination + end end diff --git a/config/application.rb b/config/application.rb index 84b564d..5450faf 100644 --- a/config/application.rb +++ b/config/application.rb @@ -9,6 +9,7 @@ require "action_mailer/railtie" require "action_view/railtie" require "sprockets/railtie" +require "dotenv-rails" # require "rails/test_unit/railtie" # Require the gems listed in Gemfile, including any gems From 86e5ab2fdca1d0126a7cf6af311ac80fe7c60b83 Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Thu, 21 Jan 2016 21:37:17 -0800 Subject: [PATCH 09/20] fixed merge conflict --- app/controllers/packages_controller.rb | 2 +- db/schema.rb | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/controllers/packages_controller.rb b/app/controllers/packages_controller.rb index fe6d154..6295c28 100644 --- a/app/controllers/packages_controller.rb +++ b/app/controllers/packages_controller.rb @@ -34,7 +34,7 @@ def index render :json => [], :status => :gateway_timeout end end - end + # # def ups_price_estimate # diff --git a/db/schema.rb b/db/schema.rb index 4dfbb16..d8b6766 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,6 +11,11 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 0) do +ActiveRecord::Schema.define(version: 20160121223301) do + + create_table "packages", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end end From f3766deb83739f0d5d908aa923da8575332fb272 Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Thu, 21 Jan 2016 23:27:23 -0800 Subject: [PATCH 10/20] corrected require factory_girl syntax --- Gemfile | 4 +- Gemfile.lock | 5 + spec/controllers/packages_controller_spec.rb | 134 +++++++++++++++++++ spec/spec_helper.rb | 2 +- 4 files changed, 142 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index 92491f7..442bd36 100644 --- a/Gemfile +++ b/Gemfile @@ -21,7 +21,6 @@ gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. gem 'sdoc', '~> 0.4.0', group: :doc gem 'httparty' -gem 'dotenv-rails' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' @@ -37,6 +36,8 @@ group :development, :test do gem 'byebug' gem 'rspec-rails' gem 'simplecov', require: false + gem 'dotenv-rails' + gem 'factory_girl_rails', '~> 4.0' end group :development do @@ -53,7 +54,6 @@ group :development do end group :test do - gem 'factory_girl_rails', '~> 4.0' end group :production do diff --git a/Gemfile.lock b/Gemfile.lock index c0812f2..7682fbd 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) @@ -203,6 +207,7 @@ DEPENDENCIES binding_of_caller byebug coffee-rails (~> 4.1.0) + dotenv-rails factory_girl_rails (~> 4.0) httparty jbuilder (~> 2.0) diff --git a/spec/controllers/packages_controller_spec.rb b/spec/controllers/packages_controller_spec.rb index 67d233e..2b58a3a 100644 --- a/spec/controllers/packages_controller_spec.rb +++ b/spec/controllers/packages_controller_spec.rb @@ -1,4 +1,138 @@ require 'rails_helper' RSpec.describe PackagesController, type: :controller do + let(:rosa) { Pet.create(name: "Rosalita", human: "<3Jeremy!", age: 7) } + let(:raquel) { Pet.create(name: "Raquel", human: "<3Jeremy!", age: 12) } + let(:keys) { ["age", "human", "id", "name"] } + + describe "GET 'index'" do + it "is successful" do + get :index + expect(response.response_code).to eq 200 + end + + it "returns json" do + get :index + expect(response.header['Content-Type']).to include 'application/json' + end + + context "the returned json object" do + before :each do + rosa + raquel + get :index + @response = JSON.parse response.body + end + + it "is an array of pet objects" do + expect(@response).to be_an_instance_of Array + expect(@response.length).to eq 2 + end + + it "includes only the id, name, human, and age keys" do + # a way to specify that you want to map on a certian field. Put a binding.pry to find out more. + expect(@response.map(&:keys).flatten.uniq.sort).to eq keys + end + end + end + + describe "GET 'show'" do + it "is successful" do + get :show, id: rosa.id + expect(response.response_code).to eq 200 + end + + it "returns json" do + get :show, id: rosa.id + expect(response.header['Content-Type']).to include 'application/json' + end + + context "the returned json object" do + before :each do + get :show, id: rosa.id + @response = JSON.parse(response.body) + end + + it "has the right keys" do + expect(@response.keys.sort).to eq keys + end + + it "has all of Rosa's info" do + keys.each do |key| + expect(@response[key]).to eq rosa[key] + end + end + end + + + context "no pets found" do + before :each do + get :show, id: 1000 + end + + it "is successful" do + expect(response).to be_successful + end + + it "returns a 204 (no content)" do + expect(response.response_code).to eq 204 + end + + it "expects the response body to be an empty array" do + expect(response.body).to eq "[]" + end + end + end + + describe "GET 'search'" do + it "is successful" do + get :search, name: rosa.name + expect(response.response_code).to eq 200 + end + + it "returns json" do + get :search, name: rosa.name + expect(response.header['Content-Type']).to include 'application/json' + end + + it "fuzzy searches" do + get :search, name: "Ros" + expect(response.response_code).to eq 200 + end + + context "return json object" do + before :each do + get :search, name: rosa.name + @response = JSON.parse(response.body) + end + + it "has the right keys" do + expect(@response.keys.sort).to eq keys + end + + it "has all of Rosa's info" do + keys.each do |key| + expect(@response[key]).to eq rosa[key] + end + end + end + + context "unsuccessful search" do + before(:each) do + get :search, name: "bubbles" + end + it "is successful" do + expect(response).to be_successful + end + + it "returns a 204 (no content)" do + expect(response.response_code).to eq 204 + end + + it "expects the response body to be an empty array" do + expect(response.body).to eq "[]" + end + end + end + end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 17dc73b..dc0cc89 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,4 +1,4 @@ -require 'factory-girl' +require 'factory_girl' require 'simplecov' SimpleCov.start 'rails' From 94fb043fcae415afd44815a44731b3abbf4c3a4a Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Fri, 22 Jan 2016 09:54:31 -0800 Subject: [PATCH 11/20] moved model methods to the controller. all (3) failing tests --- app/controllers/packages_controller.rb | 32 ++- app/models/package.rb | 27 -- config/routes.rb | 2 +- spec/controllers/packages_controller_spec.rb | 263 ++++++++++--------- spec/models/package_spec.rb | 15 +- 5 files changed, 187 insertions(+), 152 deletions(-) diff --git a/app/controllers/packages_controller.rb b/app/controllers/packages_controller.rb index 6295c28..3a6b88b 100644 --- a/app/controllers/packages_controller.rb +++ b/app/controllers/packages_controller.rb @@ -7,7 +7,6 @@ def index origin = get_origin package = get_package destination = get_destination(params) - ups = ActiveShipping::UPS.new(login: ENV["UPS_LOGIN"], password: ENV["UPS_PASSWORD"], key: ENV["UPS_KEY"]) usps = ActiveShipping::USPS.new(login: ENV["USPS_LOGIN"]) @@ -34,7 +33,36 @@ def index render :json => [], :status => :gateway_timeout end end - + + def get_package + package = [ + ActiveShipping::Package.new(100, + [40,40], + valule: 10000) + ] + + return package + end + + def get_origin + origin = ActiveShipping::Location.new(country: 'US', + state: 'WA', + city: 'Seattle', + zip: 98122) + return origin + end + + def get_destination(hash) + destination = ActiveShipping::Location.new( + country: 'US', + state: hash[:state], + city: hash[:city], + zip: hash[:zip]) + + return destination + end + + # # def ups_price_estimate # diff --git a/app/models/package.rb b/app/models/package.rb index f1617b7..e583a7f 100644 --- a/app/models/package.rb +++ b/app/models/package.rb @@ -1,30 +1,3 @@ class Package < ActiveRecord::Base - def get_package - package = [ - ActiveShipping::Package.new(100, - [100,100], - valule: 10000) - ] - - return package - end - - def get_origin - origin = ActiveShipping::UPS.new(country: 'US', - state: 'WA', - city: 'Seattle', - zip: 98122) - return origin - end - - def get_destination(hash) - destination = ActiveShipping::UPS.new( - country: 'US', - state: hash[:state], - city: hash[:city], - zip: hash[:zip]) - - return destination - end end diff --git a/config/routes.rb b/config/routes.rb index 7bb78d4..4d7e1fe 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - get 'packages' => 'packages#receive_info' + get 'packages' => 'packages#index' # 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/spec/controllers/packages_controller_spec.rb b/spec/controllers/packages_controller_spec.rb index 2b58a3a..cb64c34 100644 --- a/spec/controllers/packages_controller_spec.rb +++ b/spec/controllers/packages_controller_spec.rb @@ -1,138 +1,159 @@ require 'rails_helper' RSpec.describe PackagesController, type: :controller do - let(:rosa) { Pet.create(name: "Rosalita", human: "<3Jeremy!", age: 7) } - let(:raquel) { Pet.create(name: "Raquel", human: "<3Jeremy!", age: 12) } - let(:keys) { ["age", "human", "id", "name"] } + let(:origin) { ActiveShipping::Location.new( + country: 'US', + state: 'WA', + city: 'Seattle', + zip: 98122) + } + + let(:package) { ActiveShipping::Package.new(100, + [100,100], + valule: 10000) + } + + let(:destination) { ActiveShipping::Location.new( + country: 'US', + state: 'GA', + city: 'Atlanta', + zip: 30307) + } + + # let(:ups) { ActiveShipping::UPS.new(login: ENV["UPS_LOGIN"], password: ENV["UPS_PASSWORD"], key: ENV["UPS_KEY"]) + # } + # + # let(:usps) { ActiveShipping::USPS.new(login: ENV["USPS_LOGIN"]) + # } describe "GET 'index'" do it "is successful" do - get :index + get :index, origin, package, destination expect(response.response_code).to eq 200 end it "returns json" do - get :index + get :index, origin, package, destination, ups, usps expect(response.header['Content-Type']).to include 'application/json' end - context "the returned json object" do - before :each do - rosa - raquel - get :index - @response = JSON.parse response.body - end - - it "is an array of pet objects" do - expect(@response).to be_an_instance_of Array - expect(@response.length).to eq 2 - end - - it "includes only the id, name, human, and age keys" do - # a way to specify that you want to map on a certian field. Put a binding.pry to find out more. - expect(@response.map(&:keys).flatten.uniq.sort).to eq keys - end - end + # context "the returned json object" do + # before :each do + # rosa + # raquel + # get :index + # @response = JSON.parse response.body + # end + # + # it "is an array of pet objects" do + # expect(@response).to be_an_instance_of Array + # expect(@response.length).to eq 2 + # end + # + # it "includes only the id, name, human, and age keys" do + # # a way to specify that you want to map on a certian field. Put a binding.pry to find out more. + # expect(@response.map(&:keys).flatten.uniq.sort).to eq keys + # end + # end end - describe "GET 'show'" do - it "is successful" do - get :show, id: rosa.id - expect(response.response_code).to eq 200 - end - - it "returns json" do - get :show, id: rosa.id - expect(response.header['Content-Type']).to include 'application/json' - end - - context "the returned json object" do - before :each do - get :show, id: rosa.id - @response = JSON.parse(response.body) - end - - it "has the right keys" do - expect(@response.keys.sort).to eq keys - end - - it "has all of Rosa's info" do - keys.each do |key| - expect(@response[key]).to eq rosa[key] - end - end - end - - - context "no pets found" do - before :each do - get :show, id: 1000 - end - - it "is successful" do - expect(response).to be_successful - end - - it "returns a 204 (no content)" do - expect(response.response_code).to eq 204 - end - - it "expects the response body to be an empty array" do - expect(response.body).to eq "[]" - end - end - end - - describe "GET 'search'" do - it "is successful" do - get :search, name: rosa.name - expect(response.response_code).to eq 200 - end - - it "returns json" do - get :search, name: rosa.name - expect(response.header['Content-Type']).to include 'application/json' - end - - it "fuzzy searches" do - get :search, name: "Ros" - expect(response.response_code).to eq 200 - end - - context "return json object" do - before :each do - get :search, name: rosa.name - @response = JSON.parse(response.body) - end - - it "has the right keys" do - expect(@response.keys.sort).to eq keys - end - - it "has all of Rosa's info" do - keys.each do |key| - expect(@response[key]).to eq rosa[key] - end - end - end - - context "unsuccessful search" do - before(:each) do - get :search, name: "bubbles" - end - it "is successful" do - expect(response).to be_successful - end - - it "returns a 204 (no content)" do - expect(response.response_code).to eq 204 - end - - it "expects the response body to be an empty array" do - expect(response.body).to eq "[]" - end - end - end + # describe "GET 'show'" do + # it "is successful" do + # get :show, id: rosa.id + # expect(response.response_code).to eq 200 + # end + # + # it "returns json" do + # get :show, id: rosa.id + # expect(response.header['Content-Type']).to include 'application/json' + # end + # + # context "the returned json object" do + # before :each do + # get :show, id: rosa.id + # @response = JSON.parse(response.body) + # end + # + # it "has the right keys" do + # expect(@response.keys.sort).to eq keys + # end + # + # it "has all of Rosa's info" do + # keys.each do |key| + # expect(@response[key]).to eq rosa[key] + # end + # end + # end + # + # + # context "no pets found" do + # before :each do + # get :show, id: 1000 + # end + # + # it "is successful" do + # expect(response).to be_successful + # end + # + # it "returns a 204 (no content)" do + # expect(response.response_code).to eq 204 + # end + # + # it "expects the response body to be an empty array" do + # expect(response.body).to eq "[]" + # end + # end + # end + # + # describe "GET 'search'" do + # it "is successful" do + # get :search, name: rosa.name + # expect(response.response_code).to eq 200 + # end + # + # it "returns json" do + # get :search, name: rosa.name + # expect(response.header['Content-Type']).to include 'application/json' + # end + # + # it "fuzzy searches" do + # get :search, name: "Ros" + # expect(response.response_code).to eq 200 + # end + # + # context "return json object" do + # before :each do + # get :search, name: rosa.name + # @response = JSON.parse(response.body) + # end + # + # it "has the right keys" do + # expect(@response.keys.sort).to eq keys + # end + # + # it "has all of Rosa's info" do + # keys.each do |key| + # expect(@response[key]).to eq rosa[key] + # end + # end + # end + # + # context "unsuccessful search" do + # before(:each) do + # get :search, name: "bubbles" + # end + # it "is successful" do + # expect(response).to be_successful + # end + # + # it "returns a 204 (no content)" do + # expect(response.response_code).to eq 204 + # end + # + # it "expects the response body to be an empty array" do + # expect(response.body).to eq "[]" + # end + # end + # end end diff --git a/spec/models/package_spec.rb b/spec/models/package_spec.rb index 5c8bcbd..99abc77 100644 --- a/spec/models/package_spec.rb +++ b/spec/models/package_spec.rb @@ -1,5 +1,18 @@ require 'rails_helper' RSpec.describe Package, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + let(:package){ ActiveShipping::Package.new( + 100, + [100,100], + value: 10000 + ) + } + + describe "#get_package" do + it "creates a package object" do + expect(package).to be_an_instance_of Package + end + end + + end From 2d3c744923d9944462c6a1a3332417cd9536975d Mon Sep 17 00:00:00 2001 From: Tammy Date: Fri, 22 Jan 2016 10:43:03 -0800 Subject: [PATCH 12/20] we got an API working and getting shipping details. wohhho --- 98034, | 0 Gemfile.lock | 5 ++ Seattle, | 0 WA | 0 app/controllers/packages_controller.rb | 91 +++++++++++++++----------- app/models/package.rb | 2 +- config/routes.rb | 2 +- db/schema.rb | 7 +- 8 files changed, 66 insertions(+), 41 deletions(-) create mode 100644 98034, create mode 100644 Seattle, create mode 100644 WA diff --git a/98034, b/98034, new file mode 100644 index 0000000..e69de29 diff --git a/Gemfile.lock b/Gemfile.lock index c0812f2..7682fbd 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) @@ -203,6 +207,7 @@ DEPENDENCIES binding_of_caller byebug coffee-rails (~> 4.1.0) + dotenv-rails factory_girl_rails (~> 4.0) httparty jbuilder (~> 2.0) diff --git a/Seattle, b/Seattle, new file mode 100644 index 0000000..e69de29 diff --git a/WA b/WA new file mode 100644 index 0000000..e69de29 diff --git a/app/controllers/packages_controller.rb b/app/controllers/packages_controller.rb index fe6d154..159b883 100644 --- a/app/controllers/packages_controller.rb +++ b/app/controllers/packages_controller.rb @@ -1,56 +1,71 @@ require 'httparty' class PackagesController < ApplicationController - include Timeout + # include Timeout - def index + def rates origin = get_origin package = get_package destination = get_destination(params) ups = ActiveShipping::UPS.new(login: ENV["UPS_LOGIN"], password: ENV["UPS_PASSWORD"], key: ENV["UPS_KEY"]) - usps = ActiveShipping::USPS.new(login: ENV["USPS_LOGIN"]) + # usps = ActiveShipping::USPS.new(login: ENV["USPS_LOGIN"]) ups_response = ups.find_rates(origin, destination, package) ups_rates = ups_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price, rate.delivery_date]} - usps_response = usps.find_rates(origin, destination, packages) - usps_rates = usps_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price, rate.delivery_date]} - - begin - Timeout::timeout(10) do - if ups_rates && usps_rates - all_rates = ups_rates.concat(usps_rates) - render :json => all_rates.as_json, :status => :ok - elsif ups_rates - render :json => ups_rates.as_json, :status => :partial_content - elsif usps_rates - render :json => usps_rates.as_json, :status => :partial_content - else - render :json => [], :status => :no_content - end - end - rescue Timeout::Error - render :json => [], :status => :gateway_timeout - end + # usps_response = usps.find_rates(origin, destination, packages) + # usps_rates = usps_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price, rate.delivery_date]} + # + # all_rates = { ups: ups_rates, usps: usps_rates } + render :json => ups_rates.as_json, :status => :partial_content + + # begin + # Timeout::timeout(10) do + # if ups_rates && usps_rates + # render :json => all_rates.as_json, :status => :ok + # elsif ups_rates + # render :json => ups_rates.as_json, :status => :partial_content + # elsif usps_rates + # render :json => usps_rates.as_json, :status => :partial_content + # else + # render json: { error: :missing_destination_state, message: "You must provide a valid State for the shipping destination." }, status: :bad_request + # end + # end + # rescue Timeout::Error + # render :json => [], :status => :gateway_timeout + # end + + end + private + + def get_package + package = [ + ActiveShipping::Package.new(100, + [20,20], + value: 1000) + ] + + return package end + + def get_origin + origin = ActiveShipping::Location.new(country: 'US', + state: 'WA', + city: 'Seattle', + zip: 98122) + return origin + end + + def get_destination(hash) + destination = ActiveShipping::Location.new( + country: 'US', + state: hash[:state], + city: hash[:city], + zip: hash[:zip]) + + return destination end - # - # def ups_price_estimate - # - # end - # - # def usps_price_estimate - # - # end - # - # def ups_delivery_time - # - # end - # - # def usps_delivery_time - # - # end diff --git a/app/models/package.rb b/app/models/package.rb index f1617b7..9529271 100644 --- a/app/models/package.rb +++ b/app/models/package.rb @@ -4,7 +4,7 @@ def get_package package = [ ActiveShipping::Package.new(100, [100,100], - valule: 10000) + value: 10000) ] return package diff --git a/config/routes.rb b/config/routes.rb index 7bb78d4..f64f647 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - get 'packages' => 'packages#receive_info' + 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/db/schema.rb b/db/schema.rb index 4dfbb16..d8b6766 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,6 +11,11 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 0) do +ActiveRecord::Schema.define(version: 20160121223301) do + + create_table "packages", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end end From 807a15197aba5dab199a8d21708420e6117f22d5 Mon Sep 17 00:00:00 2001 From: Tammy Date: Fri, 22 Jan 2016 10:49:34 -0800 Subject: [PATCH 13/20] we have a respond from USPS --- app/controllers/packages_controller.rb | 32 +++++++++++++------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/app/controllers/packages_controller.rb b/app/controllers/packages_controller.rb index 159b883..8aef836 100644 --- a/app/controllers/packages_controller.rb +++ b/app/controllers/packages_controller.rb @@ -9,29 +9,29 @@ def rates destination = get_destination(params) ups = ActiveShipping::UPS.new(login: ENV["UPS_LOGIN"], password: ENV["UPS_PASSWORD"], key: ENV["UPS_KEY"]) - # usps = ActiveShipping::USPS.new(login: ENV["USPS_LOGIN"]) + usps = ActiveShipping::USPS.new(login: ENV["USPS_LOGIN"]) ups_response = ups.find_rates(origin, destination, package) ups_rates = ups_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price, rate.delivery_date]} - # usps_response = usps.find_rates(origin, destination, packages) - # usps_rates = usps_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price, rate.delivery_date]} - # - # all_rates = { ups: ups_rates, usps: usps_rates } - render :json => ups_rates.as_json, :status => :partial_content + usps_response = usps.find_rates(origin, destination, package) + usps_rates = usps_response.rates.sort_by(&:price).collect {|rate| [rate.service_name, rate.price, rate.delivery_date]} + + all_rates = { ups: ups_rates, usps: usps_rates } + # begin # Timeout::timeout(10) do - # if ups_rates && usps_rates - # render :json => all_rates.as_json, :status => :ok - # elsif ups_rates - # render :json => ups_rates.as_json, :status => :partial_content - # elsif usps_rates - # render :json => usps_rates.as_json, :status => :partial_content - # else - # render json: { error: :missing_destination_state, message: "You must provide a valid State for the shipping destination." }, status: :bad_request - # end - # end + if ups_rates && usps_rates + render :json => all_rates.as_json, :status => :ok + elsif ups_rates + render :json => ups_rates.as_json, :status => :partial_content + elsif usps_rates + render :json => usps_rates.as_json, :status => :partial_content + else + render json: { error: :missing_destination_state, message: "You must provide a valid State for the shipping destination." }, status: :bad_request + end + # end # rescue Timeout::Error # render :json => [], :status => :gateway_timeout # end From 6949cbdb11cc2c073714b9b3546adf4374fd004f Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Fri, 22 Jan 2016 11:03:01 -0800 Subject: [PATCH 14/20] unhelpful tests --- spec/controllers/packages_controller_spec.rb | 33 ++++++++++++++++---- spec/models/package_spec.rb | 17 ---------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/spec/controllers/packages_controller_spec.rb b/spec/controllers/packages_controller_spec.rb index cb64c34..0f14138 100644 --- a/spec/controllers/packages_controller_spec.rb +++ b/spec/controllers/packages_controller_spec.rb @@ -20,15 +20,15 @@ zip: 30307) } - # let(:ups) { ActiveShipping::UPS.new(login: ENV["UPS_LOGIN"], password: ENV["UPS_PASSWORD"], key: ENV["UPS_KEY"]) - # } - # - # let(:usps) { ActiveShipping::USPS.new(login: ENV["USPS_LOGIN"]) - # } + let(:ups) { ActiveShipping::UPS.new(login: ENV["UPS_LOGIN"], password: ENV["UPS_PASSWORD"], key: ENV["UPS_KEY"]) + } + + let(:usps) { ActiveShipping::USPS.new(login: ENV["USPS_LOGIN"]) + } describe "GET 'index'" do it "is successful" do - get :index, origin, package, destination + get :index, origin, package, destination, ups, usps expect(response.response_code).to eq 200 end @@ -57,6 +57,27 @@ # end end + describe "#get_package" do + it "creates a package object" do + + expect(response.status).to eq 200 + expect(package).to be_an_instance_of Hash + end + end + + describe "#get_origin" do + it "creates a location object" do + expect(origin).to be_an Object + end + end + + describe "#get_destination" do + it "creates a location object" do + expect(destination).to be_an Object + end + end + + # describe "GET 'show'" do # it "is successful" do # get :show, id: rosa.id diff --git a/spec/models/package_spec.rb b/spec/models/package_spec.rb index 99abc77..63f198b 100644 --- a/spec/models/package_spec.rb +++ b/spec/models/package_spec.rb @@ -1,18 +1 @@ require 'rails_helper' - -RSpec.describe Package, type: :model do - let(:package){ ActiveShipping::Package.new( - 100, - [100,100], - value: 10000 - ) - } - - describe "#get_package" do - it "creates a package object" do - expect(package).to be_an_instance_of Package - end - end - - -end From 07aa844446d45954543fa851a2e253fc40491984 Mon Sep 17 00:00:00 2001 From: Tammy Date: Fri, 22 Jan 2016 11:03:28 -0800 Subject: [PATCH 15/20] commit before pulling --- app/models/package.rb | 26 -------------------------- 1 file changed, 26 deletions(-) diff --git a/app/models/package.rb b/app/models/package.rb index 9529271..cf0dc03 100644 --- a/app/models/package.rb +++ b/app/models/package.rb @@ -1,30 +1,4 @@ class Package < ActiveRecord::Base - def get_package - package = [ - ActiveShipping::Package.new(100, - [100,100], - value: 10000) - ] - return package - end - - def get_origin - origin = ActiveShipping::UPS.new(country: 'US', - state: 'WA', - city: 'Seattle', - zip: 98122) - return origin - end - - def get_destination(hash) - destination = ActiveShipping::UPS.new( - country: 'US', - state: hash[:state], - city: hash[:city], - zip: hash[:zip]) - - return destination - end end From 8d0abb5dd1ff9306360c6935156aa8e78b8600e9 Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Fri, 22 Jan 2016 11:04:06 -0800 Subject: [PATCH 16/20] gemfile.lock --- Gemfile.lock | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Gemfile.lock b/Gemfile.lock index c0812f2..7682fbd 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) @@ -203,6 +207,7 @@ DEPENDENCIES binding_of_caller byebug coffee-rails (~> 4.1.0) + dotenv-rails factory_girl_rails (~> 4.0) httparty jbuilder (~> 2.0) From 5a2d551668d6ed4721a0a52e8d6a08f3ed1742a7 Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Fri, 22 Jan 2016 12:28:59 -0800 Subject: [PATCH 17/20] more failing tests --- spec/controllers/packages_controller_spec.rb | 74 ++++++++++++-------- 1 file changed, 46 insertions(+), 28 deletions(-) diff --git a/spec/controllers/packages_controller_spec.rb b/spec/controllers/packages_controller_spec.rb index 0f14138..468dff3 100644 --- a/spec/controllers/packages_controller_spec.rb +++ b/spec/controllers/packages_controller_spec.rb @@ -8,16 +8,16 @@ zip: 98122) } - let(:package) { ActiveShipping::Package.new(100, - [100,100], - valule: 10000) + let(:package) { ActiveShipping::Package.new(30, + [30,30], + valule: 100) } - let(:destination) { ActiveShipping::Location.new( + let(:destination) { ActiveShipping::Location.new( country: 'US', - state: 'GA', - city: 'Atlanta', - zip: 30307) + state: 'WA', + city: 'Seattle', + zip: 98122) } let(:ups) { ActiveShipping::UPS.new(login: ENV["UPS_LOGIN"], password: ENV["UPS_PASSWORD"], key: ENV["UPS_KEY"]) @@ -26,35 +26,53 @@ let(:usps) { ActiveShipping::USPS.new(login: ENV["USPS_LOGIN"]) } - describe "GET 'index'" do + describe "GET 'rates'" do it "is successful" do - get :index, origin, package, destination, ups, usps + get :rates, origin, package, destination, ups, usps expect(response.response_code).to eq 200 end it "returns json" do - get :index, origin, package, destination, ups, usps + get :rates, origin, package, destination, ups, usps expect(response.header['Content-Type']).to include 'application/json' end - # context "the returned json object" do - # before :each do - # rosa - # raquel - # get :index - # @response = JSON.parse response.body - # end - # - # it "is an array of pet objects" do - # expect(@response).to be_an_instance_of Array - # expect(@response.length).to eq 2 - # end - # - # it "includes only the id, name, human, and age keys" do - # # a way to specify that you want to map on a certian field. Put a binding.pry to find out more. - # expect(@response.map(&:keys).flatten.uniq.sort).to eq keys - # end - # end + context "the returned json object" do + before :each do + origin + package + destination#.to_query + get :rates + @response = JSON.parse response.body + end + + it "is an array of shipping objects" do + expect(@response).to be_an_instance_of Array + expect(@response.length).to eq 2 + end + + it "includes only the id, name, human, and age keys" do + # a way to specify that you want to map on a certian field. Put a binding.pry to find out more. + expect(@response.map(&:keys).flatten.uniq.sort).to eq keys + end + end + + context "ups response" do + it "returns an array of prices" do + ups + get :rates + expect(ups_rates).to be_an_instance_of Array + end + end + + context "usps resoponse" do + it "returns an array of prices" do + usps + get :rates + expect(usps_rates).to be_an_instance_of Array + end + end + end describe "#get_package" do From e738c47e02f2b7a6184e489119be1ae97b084562 Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Fri, 22 Jan 2016 14:02:56 -0800 Subject: [PATCH 18/20] 6 passing tests, yall --- app/controllers/packages_controller.rb | 10 +- app/models/package.rb | 3 - spec/controllers/packages_controller_spec.rb | 173 ++----------------- 3 files changed, 17 insertions(+), 169 deletions(-) diff --git a/app/controllers/packages_controller.rb b/app/controllers/packages_controller.rb index 8aef836..f6d52c5 100644 --- a/app/controllers/packages_controller.rb +++ b/app/controllers/packages_controller.rb @@ -6,7 +6,7 @@ class PackagesController < ApplicationController def rates origin = get_origin package = get_package - destination = get_destination(params) + destination = get_destination ups = ActiveShipping::UPS.new(login: ENV["UPS_LOGIN"], password: ENV["UPS_PASSWORD"], key: ENV["UPS_KEY"]) usps = ActiveShipping::USPS.new(login: ENV["USPS_LOGIN"]) @@ -57,12 +57,12 @@ def get_origin return origin end - def get_destination(hash) + def get_destination destination = ActiveShipping::Location.new( country: 'US', - state: hash[:state], - city: hash[:city], - zip: hash[:zip]) + state: params[:state], + city: params[:city], + zip: params[:zip]) return destination end diff --git a/app/models/package.rb b/app/models/package.rb index e583a7f..e69de29 100644 --- a/app/models/package.rb +++ b/app/models/package.rb @@ -1,3 +0,0 @@ -class Package < ActiveRecord::Base - -end diff --git a/spec/controllers/packages_controller_spec.rb b/spec/controllers/packages_controller_spec.rb index 468dff3..50d0b6f 100644 --- a/spec/controllers/packages_controller_spec.rb +++ b/spec/controllers/packages_controller_spec.rb @@ -1,198 +1,49 @@ require 'rails_helper' RSpec.describe PackagesController, type: :controller do - let(:origin) { ActiveShipping::Location.new( - country: 'US', - state: 'WA', - city: 'Seattle', - zip: 98122) - } - - let(:package) { ActiveShipping::Package.new(30, - [30,30], - valule: 100) - } - - let(:destination) { ActiveShipping::Location.new( - country: 'US', - state: 'WA', - city: 'Seattle', - zip: 98122) - } - - let(:ups) { ActiveShipping::UPS.new(login: ENV["UPS_LOGIN"], password: ENV["UPS_PASSWORD"], key: ENV["UPS_KEY"]) - } - - let(:usps) { ActiveShipping::USPS.new(login: ENV["USPS_LOGIN"]) - } describe "GET 'rates'" do it "is successful" do - get :rates, origin, package, destination, ups, usps + get :rates, :state => "GA", :city => "Atlanta", :zip => 30307 expect(response.response_code).to eq 200 end it "returns json" do - get :rates, origin, package, destination, ups, usps + get :rates, :state => "GA", :city => "Atlanta", :zip => 30307 expect(response.header['Content-Type']).to include 'application/json' end context "the returned json object" do before :each do - origin - package - destination#.to_query - get :rates + get :rates, :state => "GA", :city => "Atlanta", :zip => 30307 @response = JSON.parse response.body end it "is an array of shipping objects" do - expect(@response).to be_an_instance_of Array + expect(@response).to be_an_instance_of Hash expect(@response.length).to eq 2 end - it "includes only the id, name, human, and age keys" do - # a way to specify that you want to map on a certian field. Put a binding.pry to find out more. - expect(@response.map(&:keys).flatten.uniq.sort).to eq keys + it "includes the appropriate keys" do + expect(@response.keys).to eq ["ups", "usps"] end end context "ups response" do it "returns an array of prices" do - ups - get :rates - expect(ups_rates).to be_an_instance_of Array + get :rates, :state => "GA", :city => "Atlanta", :zip => 30307 + response_json = JSON.parse response.body + expect(response_json["ups"]).to be_an_instance_of Array end end context "usps resoponse" do it "returns an array of prices" do - usps - get :rates - expect(usps_rates).to be_an_instance_of Array + get :rates, :state => "GA", :city => "Atlanta", :zip => 30307 + response_json = JSON.parse response.body + expect(response_json["usps"]).to be_an_instance_of Array end end end - - describe "#get_package" do - it "creates a package object" do - - expect(response.status).to eq 200 - expect(package).to be_an_instance_of Hash - end - end - - describe "#get_origin" do - it "creates a location object" do - expect(origin).to be_an Object - end - end - - describe "#get_destination" do - it "creates a location object" do - expect(destination).to be_an Object - end - end - - - # describe "GET 'show'" do - # it "is successful" do - # get :show, id: rosa.id - # expect(response.response_code).to eq 200 - # end - # - # it "returns json" do - # get :show, id: rosa.id - # expect(response.header['Content-Type']).to include 'application/json' - # end - # - # context "the returned json object" do - # before :each do - # get :show, id: rosa.id - # @response = JSON.parse(response.body) - # end - # - # it "has the right keys" do - # expect(@response.keys.sort).to eq keys - # end - # - # it "has all of Rosa's info" do - # keys.each do |key| - # expect(@response[key]).to eq rosa[key] - # end - # end - # end - # - # - # context "no pets found" do - # before :each do - # get :show, id: 1000 - # end - # - # it "is successful" do - # expect(response).to be_successful - # end - # - # it "returns a 204 (no content)" do - # expect(response.response_code).to eq 204 - # end - # - # it "expects the response body to be an empty array" do - # expect(response.body).to eq "[]" - # end - # end - # end - # - # describe "GET 'search'" do - # it "is successful" do - # get :search, name: rosa.name - # expect(response.response_code).to eq 200 - # end - # - # it "returns json" do - # get :search, name: rosa.name - # expect(response.header['Content-Type']).to include 'application/json' - # end - # - # it "fuzzy searches" do - # get :search, name: "Ros" - # expect(response.response_code).to eq 200 - # end - # - # context "return json object" do - # before :each do - # get :search, name: rosa.name - # @response = JSON.parse(response.body) - # end - # - # it "has the right keys" do - # expect(@response.keys.sort).to eq keys - # end - # - # it "has all of Rosa's info" do - # keys.each do |key| - # expect(@response[key]).to eq rosa[key] - # end - # end - # end - # - # context "unsuccessful search" do - # before(:each) do - # get :search, name: "bubbles" - # end - # it "is successful" do - # expect(response).to be_successful - # end - # - # it "returns a 204 (no content)" do - # expect(response.response_code).to eq 204 - # end - # - # it "expects the response body to be an empty array" do - # expect(response.body).to eq "[]" - # end - # end - # end - end From 68bc369862b4eae4b8f53fe12a96df46fce30089 Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Fri, 22 Jan 2016 15:26:30 -0800 Subject: [PATCH 19/20] fixed one bug --- config/application.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/config/application.rb b/config/application.rb index 5450faf..84b564d 100644 --- a/config/application.rb +++ b/config/application.rb @@ -9,7 +9,6 @@ require "action_mailer/railtie" require "action_view/railtie" require "sprockets/railtie" -require "dotenv-rails" # require "rails/test_unit/railtie" # Require the gems listed in Gemfile, including any gems From 8ceb837266cf10e4f933bbb128224536eb836caa Mon Sep 17 00:00:00 2001 From: Rebecca Tolmach Date: Fri, 22 Jan 2016 15:43:07 -0800 Subject: [PATCH 20/20] updated read me --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index f1667f4..518bf9e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,12 @@ +# Shipping API by Tammy and Becca +We used the Active Shipping gem to connect our app with mail carriers. We chose UPS and USPS. Right now, users of the bEtsy app can see shipping options (rates and estimated shipping if available). Currently working on localhost (not not on heroku). localhost:3000/rates?{destination_address_hash} will return JSON with all of the info. Shipping API app: https://quartzy-shipping.herokuapp.com/ +Our version of bEtsy: https://shipping-quartzy.herokuapp.com/ +Trello Board: https://trello.com/b/9DpQ4ypX/shipping-service-api +See Trello Board for bugs and future features. Submit a issue if you're interested in helping out! +_Thank you for reading our code and for the helpful comments!_ + + + # Shipping Service API Build a stand-alone shipping service API that calculates estimated shipping cost for an order from another team's bEtsy application.