mirror of
https://github.com/edcommonwealth/sqm-dashboards.git
synced 2026-03-07 21:48:16 -08:00
working on twilio integration
This commit is contained in:
parent
7385fea270
commit
86f1bc7712
6 changed files with 107 additions and 2 deletions
54
app/controllers/attempts_controller.rb
Normal file
54
app/controllers/attempts_controller.rb
Normal file
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
class AddTwilioDetailsToAttempts < ActiveRecord::Migration[5.0]
|
||||
def change
|
||||
add_column :attempts, :twilio_details, :text
|
||||
end
|
||||
end
|
||||
|
|
@ -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|
|
||||
|
|
|
|||
39
spec/controllers/attempts_controller_spec.rb
Normal file
39
spec/controllers/attempts_controller_spec.rb
Normal file
|
|
@ -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…
Add table
Add a link
Reference in a new issue