From 86f1bc77123a7bcf693603381aa84f4921ce8d0c Mon Sep 17 00:00:00 2001 From: Jared Cosulich Date: Fri, 10 Mar 2017 13:53:07 -0500 Subject: [PATCH] working on twilio integration --- app/controllers/attempts_controller.rb | 54 +++++++++++++++++++ app/models/attempt.rb | 5 +- config/routes.rb | 3 ++ ...09191211_add_twilio_details_to_attempts.rb | 5 ++ db/schema.rb | 3 +- spec/controllers/attempts_controller_spec.rb | 39 ++++++++++++++ 6 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 app/controllers/attempts_controller.rb create mode 100644 db/migrate/20170309191211_add_twilio_details_to_attempts.rb create mode 100644 spec/controllers/attempts_controller_spec.rb diff --git a/app/controllers/attempts_controller.rb b/app/controllers/attempts_controller.rb new file mode 100644 index 00000000..cf21832e --- /dev/null +++ b/app/controllers/attempts_controller.rb @@ -0,0 +1,54 @@ +class AttemptsController < ApplicationController + before_action :set_attempt, only: [:edit, :update] + + def twilio + render plain: params.inspect + end + + # GET /attempts/1/edit + def edit + end + + # PATCH/PUT /attempts/1 + # PATCH/PUT /attempts/1.json + def update + attempt_params = {} + if twilio_params.present? + attempt_params.merge!( + answer_index: twilio_params[:Body].to_i, + twilio_details: twilio_params.to_h.to_yaml + ) + end + + respond_to do |format| + if @attempt.update(attempt_params) + format.html { render plain: 'Thank you!' } + format.json { render :show, status: :ok, location: @attempt } + else + format.html { render :edit } + format.json { render json: @attempt.errors, status: :unprocessable_entity } + end + end + end + + # # DELETE /attempts/1 + # # DELETE /attempts/1.json + # def destroy + # @attempt.destroy + # respond_to do |format| + # format.html { redirect_to attempts_url, notice: 'attempt was successfully destroyed.' } + # format.json { head :no_content } + # end + # end + + private + # Use callbacks to share common setup or constraints between actions. + def set_attempt + @attempt = Attempt.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def twilio_params + params.permit(:MessageSid, :AccountSid, :MessagingServiceSid, :From, :To, :Body, :NumMedia) + end +end diff --git a/app/models/attempt.rb b/app/models/attempt.rb index 2a6857b6..e880ca87 100644 --- a/app/models/attempt.rb +++ b/app/models/attempt.rb @@ -11,12 +11,15 @@ class Attempt < ApplicationRecord twilio_number = ENV['TWILIO_NUMBER'] client = Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN'] - client.messages.create( + message = client.messages.create( from: twilio_number, to: recipient.phone, body: question.text ) + puts message.inspect + puts message.try(:Sid) + update_attributes(sent_at: Time.new) end diff --git a/config/routes.rb b/config/routes.rb index b44b9c29..510ae086 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,10 +15,13 @@ Rails.application.routes.draw do resources :schedules end + resources :attempts, only: [:get, :update] + devise_for :users # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html get '/admin', to: 'admin#index', as: 'admin' + post '/twilio', to: 'attempt#twilio' root to: "welcome#index" end diff --git a/db/migrate/20170309191211_add_twilio_details_to_attempts.rb b/db/migrate/20170309191211_add_twilio_details_to_attempts.rb new file mode 100644 index 00000000..042a1879 --- /dev/null +++ b/db/migrate/20170309191211_add_twilio_details_to_attempts.rb @@ -0,0 +1,5 @@ +class AddTwilioDetailsToAttempts < ActiveRecord::Migration[5.0] + def change + add_column :attempts, :twilio_details, :text + end +end diff --git a/db/schema.rb b/db/schema.rb index 1694c071..9c9404e1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170308160911) do +ActiveRecord::Schema.define(version: 20170309191211) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -27,6 +27,7 @@ ActiveRecord::Schema.define(version: 20170308160911) do t.integer "open_response_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.text "twilio_details" end create_table "categories", force: :cascade do |t| diff --git a/spec/controllers/attempts_controller_spec.rb b/spec/controllers/attempts_controller_spec.rb new file mode 100644 index 00000000..22e6b286 --- /dev/null +++ b/spec/controllers/attempts_controller_spec.rb @@ -0,0 +1,39 @@ +require 'rails_helper' + +RSpec.describe AttemptsController, type: :controller do + + let(:valid_session) { {} } + + let(:schedule) { Schedule.new } + let(:recipient) { Recipient.new } + let(:recipient_schedule) { RecipientSchedule.new } + let(:question) { Question.new } + let!(:attempt) { + Attempt.create( + schedule: schedule, + recipient: recipient, + recipient_schedule: recipient_schedule, + question: question + ) + } + + describe "PUT #update" do + context "with valid params" do + let(:twilio_attributes) { + {'MessageSid' => 'ewuefhwieuhfweiuhfewiuhf','AccountSid' => 'wefiuwhefuwehfuwefinwefw','MessagingServiceSid' => 'efwneufhwuefhweiufhiuewhf','From' => '1112223333','To' => '2223334444','Body' => '3','NumMedia' => '0'} + } + + it "updates the requested question_list" do + put :update, params: twilio_attributes.merge(id: attempt.to_param), session: valid_session + attempt.reload + expect(attempt.answer_index).to eq(3) + expect(attempt.twilio_details).to eq(twilio_attributes.with_indifferent_access.to_yaml) + end + + it "redirects to the question_list" do + put :update, params: twilio_attributes.merge(id: attempt.to_param), session: valid_session + expect(response.body).to eq('Thank you!') + end + end + end +end