Skip to content

Commit

Permalink
Merge pull request CamfedCode#23 from ankitdhingra/#7_send_sms_using_…
Browse files Browse the repository at this point in the history
…twilio

CamfedCode#7 send sms using twilio -- when import history is run.
  • Loading branch information
Gurpreet committed Feb 11, 2013
2 parents 78f872a + d064a7f commit 5c2f6f7
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 79 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ gem 'will_paginate', '~>3.0.0'

gem 'heroku'
gem 'best_in_place', :path => 'vendor/gems/best_in_place-0.2.3'
gem "moonshado-sms"
gem "twilio-ruby"

group :test, :development do
Expand Down
4 changes: 0 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ GEM
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.18)
moonshado-sms (1.1.1)
json (>= 1.2.0)
rest-client (>= 1.6.0)
multi_json (1.3.4)
multi_xml (0.4.4)
netrc (0.7.1)
Expand Down Expand Up @@ -179,7 +176,6 @@ DEPENDENCIES
devise (~> 1.1.7)
heroku
httparty
moonshado-sms
ocra
page-object
pg
Expand Down
14 changes: 9 additions & 5 deletions app/models/epi_surveyor/survey.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,17 @@ def self.extract_mobile_number(email)
def self.send_sms(mobile_number,histories)
begin
message = "Import Summary. Total surveys imported:#{histories.length}. Success:#{histories.count {|history| history.status=='Success'}}. Incomplete:#{histories.count {|history| history.status=='Incomplete'}}. Failed:#{histories.count {|history| history.status=='Failed'}}"
sms = Moonshado::Sms.new(mobile_number, message)
sms_delivery = sms.deliver_sms
sms_response = SmsResponse.new(:sms_id => sms_delivery.delete(:id), :properties => sms_delivery)
account_sid = TWILIO_CONFIG[Rails.env]["account_sid"]
auth_token = TWILIO_CONFIG[Rails.env]["auth_token"]
from_number = TWILIO_CONFIG[Rails.env]["from_number"]
client = Twilio::REST::Client.new account_sid, auth_token

twilio_response = client.account.sms.messages.create({:from => from_number, :to => mobile_number, :body => message})

sms_response = SmsResponse.new(:sms_id => twilio_response.sms_id, :date_sent => twilio_response.date_sent, :message_body => twilio_response.message_body, :sent_to => twilio_response.sent_to, :price => twilio_response.price)
sms_response.save
rescue Exception => error
properties = {:error => error.message, :mobile_number => mobile_number}
sms_response = SmsResponse.new(:sms_id => "invalid", :properties => properties)
sms_response = SmsResponse.new(:sms_id => "invalid", :message_body => "Error {#{error}} while sending message {#{message}}", :sent_to => mobile_number)
sms_response.save
logger.error "Sending SMS failed with error #{error}"
end
Expand Down
3 changes: 1 addition & 2 deletions app/models/sms_response.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
class SmsResponse < ActiveRecord::Base
serialize :properties, Hash #currently possible keys for this hash are :error, :mobile_number, :stat, :credit
attr_accessible :sms_id, :properties
attr_accessible :sms_id, :date_sent, :message_body, :sent_to, :price
end
6 changes: 0 additions & 6 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,5 @@
#redis
ENV["REDISTOGO_URL"] = 'redis://localhost:6379'

config.after_initialize do
Moonshado::Sms.configure do |config|
config.production_environment = false
end
end

end

5 changes: 0 additions & 5 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,4 @@
:authentication => 'plain',
:enable_starttls_auto => true }

config.after_initialize do
Moonshado::Sms.configure do |config|
config.api_key = ENV['MOONSHADOSMS_URL']
end
end
end
6 changes: 0 additions & 6 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,4 @@
#redis
ENV["REDISTOGO_URL"] = 'redis://localhost:6379'

config.after_initialize do
Moonshado::Sms.configure do |config|
config.production_environment = false
end
end

end
5 changes: 4 additions & 1 deletion db/migrate/20130204095557_create_sms_responses.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ class CreateSmsResponses < ActiveRecord::Migration
def self.up
create_table :sms_responses do |t|
t.string :sms_id
t.text :properties
t.string :date_sent
t.string :message_body
t.string :sent_to
t.string :price

t.timestamps
end
Expand Down
33 changes: 18 additions & 15 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130114124504) do
ActiveRecord::Schema.define(:version => 20130204095557) do

create_table "add_default_value_to_field_mapppings", :force => true do |t|
t.datetime "created_at"
Expand All @@ -32,15 +32,15 @@
end

create_table "field_mappings", :force => true do |t|
t.integer "object_mapping_id"
t.string "field_name"
t.string "question_name"
t.string "lookup_object_name"
t.string "lookup_condition"
t.string "predefined_value"
t.string "lookup_type"
t.timestamp "created_at"
t.timestamp "updated_at"
t.integer "object_mapping_id"
t.string "field_name"
t.string "question_name"
t.string "lookup_object_name"
t.string "lookup_condition"
t.string "predefined_value"
t.string "lookup_type"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "import_histories", :force => true do |t|
Expand All @@ -59,10 +59,10 @@
end

create_table "object_mappings", :force => true do |t|
t.integer "survey_id"
t.string "salesforce_object_name"
t.timestamp "created_at"
t.timestamp "updated_at"
t.integer "survey_id"
t.string "salesforce_object_name"
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "salesforce_objects", :force => true do |t|
Expand All @@ -75,7 +75,10 @@

create_table "sms_responses", :force => true do |t|
t.string "sms_id"
t.text "properties"
t.string "date_sent"
t.string "message_body"
t.string "sent_to"
t.string "price"
t.datetime "created_at"
t.datetime "updated_at"
end
Expand Down
82 changes: 58 additions & 24 deletions spec/models/epi_surveyor/survey_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,15 @@
surveys = [EpiSurveyor::Survey.new, EpiSurveyor::Survey.new]
surveys.each do |survey|
survey.should_receive(:sync!).and_return([import_history])
survey.notification_email = 'admin@example.com'
survey.notification_email = 'gh0123456789@example.com'
end
EpiSurveyor::Survey.should_receive(:all).and_return(surveys)
sync_email = ''
Notifier.should_receive(:sync_email).with(import_histories, "admin@example.com").and_return(sync_email)
Notifier.should_receive(:sync_email).with(import_histories, "gh0123456789@example.com").and_return(sync_email)
sync_email.should_receive(:deliver)
mock_sms = ""
mock_sms_response = {:stat=>"ok", :id=>"e3debdc7f4719ec0", :credit => 500}
Moonshado::Sms.should_receive(:new).and_return(mock_sms)
mock_sms.should_receive(:deliver_sms).and_return(mock_sms_response)

EpiSurveyor::Survey.should_receive(:send_sms).with("233123456789",import_histories)

EpiSurveyor::Survey.sync_and_notify!.should == import_histories
end

Expand All @@ -203,8 +202,8 @@

survey_1 = EpiSurveyor::Survey.new
survey_2 = EpiSurveyor::Survey.new
survey_1.notification_email = 'survey1@example.com'
survey_2.notification_email = 'survey2@example.com'
survey_1.notification_email = 'gh0123456789@example.com'
survey_2.notification_email = 'gh0987654321@example.com'

surveys = [survey_1, survey_2]
surveys.each do |survey|
Expand All @@ -214,21 +213,30 @@
EpiSurveyor::Survey.should_receive(:all).and_return(surveys)
sync_email_1 = ''
sync_email_2 = ''
Notifier.should_receive(:sync_email).with([import_history], "survey1@example.com").and_return(sync_email_1)
Notifier.should_receive(:sync_email).with([import_history], "survey2@example.com").and_return(sync_email_2)
Notifier.should_receive(:sync_email).with([import_history], "gh0123456789@example.com").and_return(sync_email_1)
Notifier.should_receive(:sync_email).with([import_history], "gh0987654321@example.com").and_return(sync_email_2)
sync_email_1.should_receive(:deliver)
sync_email_2.should_receive(:deliver)
mock_sms = ""
mock_sms_response = {:stat=>"ok", :id=>"e3debdc7f4719ec0", :credit => 500}
Moonshado::Sms.should_receive(:new).exactly(2).times().and_return(mock_sms)
mock_sms.should_receive(:deliver_sms).exactly(2).times().and_return(mock_sms_response)
EpiSurveyor::Survey.should_receive(:send_sms).exactly(2).times

EpiSurveyor::Survey.sync_and_notify!.should == import_histories
sms_response = SmsResponse.where(:sms_id => "e3debdc7f4719ec0").first
sms_response.properties.should == {:stat=>"ok", :credit => 500}
end
end

describe 'send sms for sync_and_notify' do

def create_mock_twilio_message
mock_client = ""
mock_account = ""
mock_sms = ""
mock_messages = ""
Twilio::REST::Client.should_receive(:new).and_return(mock_client)
mock_client.should_receive(:account).and_return(mock_account)
mock_account.should_receive(:sms).and_return(mock_sms)
mock_sms.should_receive(:messages).and_return(mock_messages)
mock_messages
end

it "should extract phone number from email" do
EpiSurveyor::Survey.extract_mobile_number("[email protected]<").should == "233542208979"
end
Expand All @@ -237,21 +245,47 @@
EpiSurveyor::Survey.extract_mobile_number("abcde1!@[email protected]<").should be_nil
end

it "should fail and log the sms response if the mobile number is not valid" do
EpiSurveyor::Survey.send_sms("invalid_mobile_number",{})
it "should fail and log the sms response if error occurs while trying to send sms" do
import_histories = import_histories_mock_data
mobile_number = "233542208979"
message = "Import Summary. Total surveys imported:3. Success:1. Incomplete:0. Failed:2"
from_number = TWILIO_CONFIG[Rails.env]["from_number"]
mock_twilio_message = create_mock_twilio_message()
expected_error_message = "Some exception occurred"
mock_twilio_message.should_receive(:create).with({:from => from_number, :to => mobile_number, :body => message}).and_raise(expected_error_message)

EpiSurveyor::Survey.send_sms(mobile_number,import_histories)

sms_response = SmsResponse.where(:sms_id => "invalid").first
sms_response.properties[:error].should == "Phone number (invalid_mobile_number) is not formatted correctly"
sms_response.properties[:mobile_number].should == "invalid_mobile_number"
sms_response.message_body.should == "Error {Some exception occurred} while sending message {#{message}}"
sms_response.sent_to.should == mobile_number
end
it "should be able to send sms" do

it "should be able to send sms with import histories" do
import_histories = import_histories_mock_data
mobile_number = "233542208979"
message = "Import Summary. Total surveys imported:3. Success:1. Incomplete:0. Failed:2"
sms_instance = ""
Moonshado::Sms.should_receive(:new).with(mobile_number,message).and_return(sms_instance)
sms_instance.should_receive(:deliver_sms)
from_number = TWILIO_CONFIG[Rails.env]["from_number"]
mock_twilio_message = create_mock_twilio_message()
mock_response = ""
mock_response.stub(:sms_id) {"valid test sms id"}
mock_response.stub(:date_sent) {"some date"}
mock_response.stub(:message_body) {"test message body"}
mock_response.stub(:sent_to) {"to number"}
mock_response.stub(:price) {"some price"}
mock_twilio_message.should_receive(:create).with({:from => from_number, :to => mobile_number, :body => message}).and_return(mock_response)

EpiSurveyor::Survey.send_sms(mobile_number,import_histories)

sms_response = SmsResponse.where(:sms_id => "valid test sms id").first
sms_response.should_not be_nil
sms_response.date_sent.should == "some date"
sms_response.message_body.should == "test message body"
sms_response.sent_to.should == "to number"
sms_response.price.should == "some price"
end
end

describe 'delete_old_surveys' do
it 'should delete surveys with mapping > 3 years' do
surveys = [EpiSurveyor::Survey.new, EpiSurveyor::Survey.new]
Expand Down
10 changes: 0 additions & 10 deletions spec/models/sms_response_spec.rb

This file was deleted.

0 comments on commit 5c2f6f7

Please sign in to comment.