From 19defeac3f6dc0d17660e298639e23e86da4cf21 Mon Sep 17 00:00:00 2001 From: Rishabh Jain Date: Mon, 6 May 2013 18:25:09 +0530 Subject: [PATCH] Improved specs and minor modifications to TwilioAgent --- app/models/agents/twilio_agent.rb | 22 ++++++++++------ spec/models/agents/trigger_agent_spec.rb | 2 +- spec/models/agents/twilio_agent_spec.rb | 32 +++++++++++++++++++++++- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/app/models/agents/twilio_agent.rb b/app/models/agents/twilio_agent.rb index e70b6c94..d73c362c 100644 --- a/app/models/agents/twilio_agent.rb +++ b/app/models/agents/twilio_agent.rb @@ -7,23 +7,23 @@ module Agents default_schedule "every_10m" description <<-MD - The TwilioAgent receives and collects events and send them via text message to cellphone when scheduled. Right now, it is assumed that events have `:message` key, the value of which is sent as the content of the text message. - Set `:receiverscell` to the number on which you would like to receive text messages. - `:expected_receive_period_in_days` is maximum days that you would expect to pass between events being received by this agent. + The TwilioAgent receives and collects events and send them via text message to cellphone when scheduled.It is assumed that events have `:message`,`:text` or `:sms` key, the value of which is sent as the content of the text message. + Set `receiver_cell` to the number on which you would like to receive text messages. + `expected_receive_period_in_days` is maximum days that you would expect to pass between events being received by this agent. MD def default_options { :account_sid => "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", :auth_token => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", - :senderscell => "xxxxxxxxxx", - :receiverscell => "xxxxxxxxxx", + :sender_cell => "xxxxxxxxxx", + :receiver_cell => "xxxxxxxxxx", :expected_receive_period_in_days => "x" } end def validate_options - errors.add(:base, "account_sid,auth_token,senderscell,receiverscell,expected_receive_period_in_days are all required") unless options[:account_sid].present? && options[:auth_token].present? && options[:senderscell].present? && options[:receiverscell].present? && options[:expected_receive_period_in_days].present? + errors.add(:base, "account_sid,auth_token,sender_cell,receiver_cell,expected_receive_period_in_days are all required") unless options[:account_sid].present? && options[:auth_token].present? && options[:sender_cell].present? && options[:receiver_cell].present? && options[:expected_receive_period_in_days].present? end def receive(incoming_events) @@ -37,11 +37,19 @@ module Agents last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago end + def send_message(client,message) + client.account.sms.messages.create(:from=>options[:sender_cell],:to=>options[:receiver_cell],:body=>message) + end + def check if self.memory[:queue] && self.memory[:queue].length > 0 @client = Twilio::REST::Client.new options[:account_sid],options[:auth_token] self.memory[:queue].each do |text| - @client.account.sms.messages.create(:from=>options[:senderscell],:to=>options[:receiverscell],:body=>"#{text[:message]}") + message = text[:message] || text[:text] || text[:sms] + if message + message.slice! 160, message.length + send_message @client, message + end end self.memory[:queue] = [] end diff --git a/spec/models/agents/trigger_agent_spec.rb b/spec/models/agents/trigger_agent_spec.rb index 661e9553..4a24b6e9 100644 --- a/spec/models/agents/trigger_agent_spec.rb +++ b/spec/models/agents/trigger_agent_spec.rb @@ -51,7 +51,7 @@ describe Agents::TriggerAgent do @checker.should_not be_working # no events have ever been received Agents::TriggerAgent.async_receive(@checker.id, [@event.id]) - @checker.reload.should be_working # no events have ever been received + @checker.reload.should be_working # Events received three_days_from_now = 3.days.from_now stub(Time).now { three_days_from_now } @checker.reload.should_not be_working # too much time has passed diff --git a/spec/models/agents/twilio_agent_spec.rb b/spec/models/agents/twilio_agent_spec.rb index daefab7a..178ea8b9 100644 --- a/spec/models/agents/twilio_agent_spec.rb +++ b/spec/models/agents/twilio_agent_spec.rb @@ -2,9 +2,16 @@ require 'spec_helper' describe Agents::TwilioAgent do before do - @checker = Agents::TwilioAgent.new(:name => "somename", :options => {:account_sid => "x",:auth_token => "x",:senderscell => "x", :receiverscell => "x", :expected_receive_period_in_days => "x"}) + @checker = Agents::TwilioAgent.new(:name => "somename", :options => {:account_sid => "x",:auth_token => "x",:sender_cell => "x", :receiver_cell => "x", :expected_receive_period_in_days => "1"}) @checker.user = users(:bob) @checker.save! + + @event = Event.new + @event.agent = agents(:bob_weather_agent) + @event.payload = {:message => "Gonna rain.."} + @event.save! + + stub.any_instance_of(Agents::TwilioAgent).send_message {} end describe "#receive" do @@ -23,4 +30,27 @@ describe Agents::TwilioAgent do @checker.reload.memory[:queue].should == ["Some payload", "More payload"] end end + + describe "#working?" do + it "checks if events have been received within expected receive period" do + @checker.should_not be_working # No events received + Agents::TwilioAgent.async_receive @checker.id, [@event.id] + @checker.reload.should be_working # Just received events + two_days_from_now = 2.days.from_now + stub(Time).now { two_days_from_now } + @checker.reload.should_not be_working # More time have passed than expected receive period without sign of any new event + end + end + + describe "#check" do + before do + Agents::TwilioAgent.async_receive @checker.id, [@event.id] + end + it "should send text message and Memory should be empty after that" do + @checker.reload.memory[:queue].should == [{:message => "Gonna rain.."}] + Agents::TwilioAgent.async_check(@checker.id) + @checker.reload.memory[:queue].should == [] + + end + end end \ No newline at end of file