From c8946a02f4b5a933c2c702fd98a047ba49aa2b9a Mon Sep 17 00:00:00 2001 From: Jared Cosulich Date: Fri, 10 Mar 2017 14:31:14 -0500 Subject: [PATCH] working on twilio integration --- app/controllers/attempts_controller.rb | 7 ++++++- app/models/attempt.rb | 6 +++--- db/migrate/20170310191800_add_twilio_sid_to_attempt.rb | 6 ++++++ db/schema.rb | 4 +++- spec/models/attempt_spec.rb | 2 +- spec/spec_helper.rb | 1 + 6 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20170310191800_add_twilio_sid_to_attempt.rb diff --git a/app/controllers/attempts_controller.rb b/app/controllers/attempts_controller.rb index 874fb3c8..82f0cb6a 100644 --- a/app/controllers/attempts_controller.rb +++ b/app/controllers/attempts_controller.rb @@ -3,7 +3,12 @@ class AttemptsController < ApplicationController protect_from_forgery :except => [:twilio] def twilio - render plain: params.inspect + attempt = Attempt.where(twilio_sid: params[:MessageSid]).first + attempt.update_attributes( + answer_index: params[:Body].to_i, + twilio_details: params.to_h.to_yaml + ) + render plain: attempt.inspect end # GET /attempts/1/edit diff --git a/app/models/attempt.rb b/app/models/attempt.rb index e880ca87..9d87ac7a 100644 --- a/app/models/attempt.rb +++ b/app/models/attempt.rb @@ -14,13 +14,13 @@ class Attempt < ApplicationRecord message = client.messages.create( from: twilio_number, to: recipient.phone, - body: question.text + body: "#{question.text}%0a%0a#{question.option1}: Reply 1%0a#{question.option2}: Reply 2%0a#{question.option3}: Reply 3%0a#{question.option4}: Reply 4%0a#{question.option5}: Reply 5" ) puts message.inspect - puts message.try(:Sid) + puts message.try(:path) - update_attributes(sent_at: Time.new) + update_attributes(sent_at: Time.new, twilio_sid: message.path.split('/').last) end end diff --git a/db/migrate/20170310191800_add_twilio_sid_to_attempt.rb b/db/migrate/20170310191800_add_twilio_sid_to_attempt.rb new file mode 100644 index 00000000..fd2b672f --- /dev/null +++ b/db/migrate/20170310191800_add_twilio_sid_to_attempt.rb @@ -0,0 +1,6 @@ +class AddTwilioSidToAttempt < ActiveRecord::Migration[5.0] + def change + add_column :attempts, :twilio_sid, :string + add_index :attempts, :twilio_sid + end +end diff --git a/db/schema.rb b/db/schema.rb index 9c9404e1..69adf845 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: 20170309191211) do +ActiveRecord::Schema.define(version: 20170310191800) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -28,6 +28,8 @@ ActiveRecord::Schema.define(version: 20170309191211) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.text "twilio_details" + t.string "twilio_sid" + t.index ["twilio_sid"], name: "index_attempts_on_twilio_sid", using: :btree end create_table "categories", force: :cascade do |t| diff --git a/spec/models/attempt_spec.rb b/spec/models/attempt_spec.rb index 189da831..ee1dd1e1 100644 --- a/spec/models/attempt_spec.rb +++ b/spec/models/attempt_spec.rb @@ -43,7 +43,7 @@ RSpec.describe Attempt, type: :model do it 'should contact the Twilio API' do expect(FakeSMS.messages.length).to eq(1) - expect(FakeSMS.messages.first.body).to eq(question.text) + expect(FakeSMS.messages.first.body).to eq('Question 0:1%0a%0aOption 0:1 A: Reply 1%0aOption 0:1 B: Reply 2%0aOption 0:1 C: Reply 3%0aOption 0:1 D: Reply 4%0aOption 0:1 E: Reply 5') expect(FakeSMS.messages.first.to).to eq(recipient.phone) end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 673e3dd2..e7981701 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -119,6 +119,7 @@ class FakeSMS def create(from:, to:, body:) self.class.messages << Message.new(from, to, body) + return Struct.new(:path).new('/path') end end