parent
7385fea270
commit
86f1bc7712
@ -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
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
class AddTwilioDetailsToAttempts < ActiveRecord::Migration[5.0]
|
||||||
|
def change
|
||||||
|
add_column :attempts, :twilio_details, :text
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -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
|
||||||
Loading…
Reference in new issue