mirror of
https://github.com/edcommonwealth/sqm-dashboards.git
synced 2026-03-08 23:18:18 -07:00
working on twilio integration
This commit is contained in:
parent
44ddd0fba9
commit
6f611eed39
3 changed files with 49 additions and 39 deletions
|
|
@ -1,41 +1,41 @@
|
|||
class AttemptsController < ApplicationController
|
||||
before_action :set_attempt, only: [:edit, :update]
|
||||
# before_action :set_attempt, only: [:edit, :update]
|
||||
protect_from_forgery :except => [:twilio]
|
||||
|
||||
def twilio
|
||||
attempt = Recipient.where(phone: params['From']).first.attempts.last
|
||||
attempt = Recipient.where(phone: twilio_params['From']).first.attempts.last
|
||||
attempt.update_attributes(
|
||||
answer_index: params[:Body].to_i,
|
||||
twilio_details: params.to_h.to_yaml
|
||||
answer_index: twilio_params[:Body].to_i,
|
||||
twilio_details: twilio_params.to_h.to_yaml
|
||||
)
|
||||
render plain: attempt.inspect
|
||||
render plain: 'Thank you!'
|
||||
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
|
||||
# # 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
|
||||
|
|
@ -55,6 +55,7 @@ class AttemptsController < ApplicationController
|
|||
|
||||
# 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)
|
||||
{"Body"=>"5", ""=>"US", "To"=>"+16172023890", "ToZip"=>"02135", "NumSegments"=>"1", "MessageSid"=>"SMe37977e625b7f0b429339e752dddefef", "AccountSid"=>"AC57dc8a5a6d75addb9528e730e92f66b2", "From"=>"+16502693205", "ApiVersion"=>"2010-04-01"}
|
||||
params.permit(:FromCountry, :FromState, :FromZip, :FromCity, :ToCountry, :ToState, :SmsStatus, :SmsSid, :SmsMessageSid, :MessageSid, :AccountSid, :MessagingServiceSid, :From, :To, :Body, :NumMedia)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ Rails.application.routes.draw do
|
|||
resources :schedules
|
||||
end
|
||||
|
||||
resources :attempts, only: [:get, :update]
|
||||
# resources :attempts, only: [:get, :update]
|
||||
|
||||
devise_for :users
|
||||
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
|
||||
|
|
|
|||
|
|
@ -5,9 +5,17 @@ RSpec.describe AttemptsController, type: :controller do
|
|||
let(:valid_session) { {} }
|
||||
|
||||
let(:schedule) { Schedule.new }
|
||||
let(:recipient) { Recipient.new }
|
||||
let(:recipient) { Recipient.create(name: 'Recipient', phone: '+11231231234') }
|
||||
let(:recipient_schedule) { RecipientSchedule.new }
|
||||
let(:question) { Question.new }
|
||||
let!(:first_attempt) {
|
||||
Attempt.create(
|
||||
schedule: schedule,
|
||||
recipient: recipient,
|
||||
recipient_schedule: recipient_schedule,
|
||||
question: question
|
||||
)
|
||||
}
|
||||
let!(:attempt) {
|
||||
Attempt.create(
|
||||
schedule: schedule,
|
||||
|
|
@ -17,21 +25,22 @@ RSpec.describe AttemptsController, type: :controller do
|
|||
)
|
||||
}
|
||||
|
||||
describe "PUT #update" do
|
||||
|
||||
describe "POS #twilio" do
|
||||
context "with valid params" do
|
||||
let(:twilio_attributes) {
|
||||
{'MessageSid' => 'ewuefhwieuhfweiuhfewiuhf','AccountSid' => 'wefiuwhefuwehfuwefinwefw','MessagingServiceSid' => 'efwneufhwuefhweiufhiuewhf','From' => '1112223333','To' => '2223334444','Body' => '3','NumMedia' => '0'}
|
||||
{'MessageSid' => 'ewuefhwieuhfweiuhfewiuhf','AccountSid' => 'wefiuwhefuwehfuwefinwefw','MessagingServiceSid' => 'efwneufhwuefhweiufhiuewhf','From' => '+11231231234','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
|
||||
it "updates the last attempt by recipient phone number" do
|
||||
post :twilio, params: twilio_attributes
|
||||
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
|
||||
it "sends back a message" do
|
||||
post :twilio, params: twilio_attributes
|
||||
expect(response.body).to eq('Thank you!')
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue