Improved specs and minor modifications to TwilioAgent

Rishabh Jain 11 lat temu
rodzic
commit
19defeac3f

+ 15 - 7
app/models/agents/twilio_agent.rb

@@ -7,23 +7,23 @@ module Agents
7 7
         default_schedule "every_10m"
8 8
 
9 9
         description <<-MD
10
-            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.
11
-            Set `:receiverscell` to the number on which you would like to receive text messages.
12
-            `:expected_receive_period_in_days` is maximum days that you would expect to pass between events being received by this agent. 
10
+            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.
11
+            Set `receiver_cell` to the number on which you would like to receive text messages.
12
+            `expected_receive_period_in_days` is maximum days that you would expect to pass between events being received by this agent. 
13 13
         MD
14 14
 
15 15
         def default_options
16 16
             {
17 17
                 :account_sid   => "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
18 18
                 :auth_token    => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
19
-                :senderscell   => "xxxxxxxxxx",
20
-                :receiverscell => "xxxxxxxxxx",
19
+                :sender_cell   => "xxxxxxxxxx",
20
+                :receiver_cell => "xxxxxxxxxx",
21 21
                 :expected_receive_period_in_days => "x"
22 22
             }
23 23
         end
24 24
 
25 25
         def validate_options
26
-            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?
26
+            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?
27 27
         end
28 28
 
29 29
         def receive(incoming_events)
@@ -37,11 +37,19 @@ module Agents
37 37
             last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago
38 38
         end
39 39
 
40
+        def send_message(client,message)
41
+            client.account.sms.messages.create(:from=>options[:sender_cell],:to=>options[:receiver_cell],:body=>message)
42
+        end
43
+
40 44
         def check
41 45
             if self.memory[:queue] && self.memory[:queue].length > 0
42 46
                 @client = Twilio::REST::Client.new options[:account_sid],options[:auth_token]
43 47
                 self.memory[:queue].each do |text|
44
-                    @client.account.sms.messages.create(:from=>options[:senderscell],:to=>options[:receiverscell],:body=>"#{text[:message]}")
48
+                    message = text[:message] || text[:text] || text[:sms]
49
+                    if message
50
+                        message.slice! 160, message.length
51
+                        send_message @client, message
52
+                    end
45 53
                 end
46 54
                 self.memory[:queue] = []
47 55
             end

+ 1 - 1
spec/models/agents/trigger_agent_spec.rb

@@ -51,7 +51,7 @@ describe Agents::TriggerAgent do
51 51
 
52 52
       @checker.should_not be_working # no events have ever been received
53 53
       Agents::TriggerAgent.async_receive(@checker.id, [@event.id])
54
-      @checker.reload.should be_working # no events have ever been received
54
+      @checker.reload.should be_working # Events received
55 55
       three_days_from_now = 3.days.from_now
56 56
       stub(Time).now { three_days_from_now }
57 57
       @checker.reload.should_not be_working # too much time has passed

+ 31 - 1
spec/models/agents/twilio_agent_spec.rb

@@ -2,9 +2,16 @@ require 'spec_helper'
2 2
 
3 3
 describe Agents::TwilioAgent do
4 4
     before do
5
-        @checker = Agents::TwilioAgent.new(:name => "somename", :options => {:account_sid => "x",:auth_token => "x",:senderscell => "x", :receiverscell => "x", :expected_receive_period_in_days => "x"})
5
+        @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"})
6 6
         @checker.user = users(:bob)
7 7
         @checker.save!
8
+
9
+        @event = Event.new
10
+        @event.agent = agents(:bob_weather_agent)
11
+        @event.payload = {:message => "Gonna rain.."}
12
+        @event.save!
13
+
14
+        stub.any_instance_of(Agents::TwilioAgent).send_message {}
8 15
     end
9 16
 
10 17
     describe "#receive" do
@@ -23,4 +30,27 @@ describe Agents::TwilioAgent do
23 30
             @checker.reload.memory[:queue].should == ["Some payload", "More payload"]
24 31
         end
25 32
     end
33
+
34
+    describe "#working?" do
35
+        it "checks if events have been received within expected receive period" do
36
+            @checker.should_not be_working # No events received
37
+            Agents::TwilioAgent.async_receive @checker.id, [@event.id]
38
+            @checker.reload.should be_working # Just received events
39
+            two_days_from_now = 2.days.from_now
40
+            stub(Time).now { two_days_from_now }
41
+            @checker.reload.should_not be_working # More time have passed than expected receive period without sign of any new event
42
+        end
43
+    end
44
+
45
+    describe "#check" do
46
+        before do
47
+            Agents::TwilioAgent.async_receive @checker.id, [@event.id]
48
+        end
49
+        it "should send text message and Memory should be empty after that" do
50
+            @checker.reload.memory[:queue].should == [{:message => "Gonna rain.."}]
51
+            Agents::TwilioAgent.async_check(@checker.id)
52
+            @checker.reload.memory[:queue].should == []
53
+
54
+        end
55
+    end
26 56
 end