@@ -116,6 +116,8 @@ GEM |
||
116 | 116 |
json (1.7.7) |
117 | 117 |
jsonpath (0.5.1) |
118 | 118 |
multi_json |
119 |
+ jwt (0.1.8) |
|
120 |
+ multi_json (>= 1.5) |
|
119 | 121 |
kaminari (0.14.1) |
120 | 122 |
actionpack (>= 3.0.0) |
121 | 123 |
activesupport (>= 3.0.0) |
@@ -228,6 +230,10 @@ GEM |
||
228 | 230 |
treetop (1.4.12) |
229 | 231 |
polyglot |
230 | 232 |
polyglot (>= 0.3.1) |
233 |
+ twilio-ruby (3.9.0) |
|
234 |
+ builder (>= 2.1.2) |
|
235 |
+ jwt (>= 0.1.2) |
|
236 |
+ multi_json (>= 1.3.0) |
|
231 | 237 |
twitter (4.4.0) |
232 | 238 |
faraday (~> 0.8) |
233 | 239 |
multi_json (~> 1.3) |
@@ -286,6 +292,7 @@ DEPENDENCIES |
||
286 | 292 |
select2-rails |
287 | 293 |
shoulda-matchers |
288 | 294 |
system_timer |
295 |
+ twilio-ruby |
|
289 | 296 |
|
290 | 297 |
twitter-stream (>= 0.1.16) |
291 | 298 |
typhoeus |
@@ -1,58 +1,62 @@ |
||
1 |
-require 'rubygems' |
|
2 |
-require 'twilio-ruby' |
|
1 |
+require 'twilio-ruby' |
|
3 | 2 |
|
4 | 3 |
module Agents |
5 |
- class TwilioAgent < Agent |
|
6 |
- |
|
7 |
- default_schedule "every_10m" |
|
8 |
- |
|
9 |
- description <<-MD |
|
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 |
- MD |
|
14 |
- |
|
15 |
- def default_options |
|
16 |
- { |
|
17 |
- :account_sid => "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
|
18 |
- :auth_token => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", |
|
19 |
- :sender_cell => "xxxxxxxxxx", |
|
20 |
- :receiver_cell => "xxxxxxxxxx", |
|
21 |
- :expected_receive_period_in_days => "x" |
|
22 |
- } |
|
23 |
- end |
|
4 |
+ class TwilioAgent < Agent |
|
5 |
+ default_schedule "every_10m" |
|
24 | 6 |
|
25 |
- def validate_options |
|
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 |
- end |
|
7 |
+ description <<-MD |
|
8 |
+ The TwilioAgent receives and collects events and sends them via text message when scheduled. |
|
28 | 9 |
|
29 |
- def receive(incoming_events) |
|
30 |
- incoming_events.each do |event| |
|
31 |
- self.memory[:queue] ||= [] # If memory[:queue] is not true, assign [] to it, a || a = b |
|
32 |
- self.memory[:queue] << event.payload # Append |
|
33 |
- end |
|
34 |
- end |
|
10 |
+ It is assumed that events have a `:message`, `:text`, or `:sms` key, the value of which is sent as the content of the text message. |
|
35 | 11 |
|
36 |
- def working? |
|
37 |
- last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago |
|
38 |
- end |
|
12 |
+ Set `receiver_cell` to the number to receive text messages and `sender_cell` to the number sending them. |
|
39 | 13 |
|
40 |
- def send_message(client,message) |
|
41 |
- client.account.sms.messages.create(:from=>options[:sender_cell],:to=>options[:receiver_cell],:body=>message) |
|
42 |
- end |
|
14 |
+ `expected_receive_period_in_days` is maximum number of days that you would expect to pass between events being received by this agent. |
|
15 |
+ MD |
|
16 |
+ |
|
17 |
+ def default_options |
|
18 |
+ { |
|
19 |
+ :account_sid => 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
|
20 |
+ :auth_token => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
|
21 |
+ :sender_cell => 'xxxxxxxxxx', |
|
22 |
+ :receiver_cell => 'xxxxxxxxxx', |
|
23 |
+ :expected_receive_period_in_days => 'x' |
|
24 |
+ } |
|
25 |
+ end |
|
26 |
+ |
|
27 |
+ def validate_options |
|
28 |
+ unless options[:account_sid].present? && options[:auth_token].present? && options[:sender_cell].present? && options[:receiver_cell].present? && options[:expected_receive_period_in_days].present? |
|
29 |
+ errors.add(:base, 'account_sid, auth_token, sender_cell, receiver_cell, and expected_receive_period_in_days are all required') |
|
30 |
+ end |
|
31 |
+ end |
|
32 |
+ |
|
33 |
+ def receive(incoming_events) |
|
34 |
+ memory[:queue] ||= [] |
|
35 |
+ incoming_events.each do |event| |
|
36 |
+ memory[:queue] << event.payload |
|
37 |
+ end |
|
38 |
+ end |
|
39 |
+ |
|
40 |
+ def working? |
|
41 |
+ last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago |
|
42 |
+ end |
|
43 |
+ |
|
44 |
+ def send_message(client, message) |
|
45 |
+ client.account.sms.messages.create(:from => options[:sender_cell], :to => options[:receiver_cell], :body => message) |
|
46 |
+ end |
|
43 | 47 |
|
44 |
- def check |
|
45 |
- if self.memory[:queue] && self.memory[:queue].length > 0 |
|
46 |
- @client = Twilio::REST::Client.new options[:account_sid],options[:auth_token] |
|
47 |
- self.memory[:queue].each do |text| |
|
48 |
- message = text[:message] || text[:text] || text[:sms] |
|
49 |
- if message |
|
50 |
- message.slice! 160, message.length |
|
51 |
- send_message @client, message |
|
52 |
- end |
|
53 |
- end |
|
54 |
- self.memory[:queue] = [] |
|
55 |
- end |
|
48 |
+ def check |
|
49 |
+ if memory[:queue] && memory[:queue].length > 0 |
|
50 |
+ @client = Twilio::REST::Client.new(options[:account_sid], options[:auth_token]) |
|
51 |
+ memory[:queue].each do |text| |
|
52 |
+ message = text[:message] || text[:text] || text[:sms] |
|
53 |
+ if message |
|
54 |
+ message.slice! 160, message.length |
|
55 |
+ send_message @client, message |
|
56 |
+ end |
|
56 | 57 |
end |
58 |
+ memory[:queue] = [] |
|
59 |
+ end |
|
57 | 60 |
end |
61 |
+ end |
|
58 | 62 |
end |
@@ -1,56 +1,64 @@ |
||
1 | 1 |
require 'spec_helper' |
2 | 2 |
|
3 | 3 |
describe Agents::TwilioAgent do |
4 |
- before do |
|
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 |
- @checker.user = users(:bob) |
|
7 |
- @checker.save! |
|
4 |
+ before do |
|
5 |
+ @checker = Agents::TwilioAgent.new(:name => 'somename', |
|
6 |
+ :options => { :account_sid => 'x', |
|
7 |
+ :auth_token => 'x', |
|
8 |
+ :sender_cell => 'x', |
|
9 |
+ :receiver_cell => 'x', |
|
10 |
+ :expected_receive_period_in_days => '1' }) |
|
11 |
+ @checker.user = users(:bob) |
|
12 |
+ @checker.save! |
|
8 | 13 |
|
9 |
- @event = Event.new |
|
10 |
- @event.agent = agents(:bob_weather_agent) |
|
11 |
- @event.payload = {:message => "Gonna rain.."} |
|
12 |
- @event.save! |
|
14 |
+ @event = Event.new |
|
15 |
+ @event.agent = agents(:bob_weather_agent) |
|
16 |
+ @event.payload = { :message => 'Gonna rain..' } |
|
17 |
+ @event.save! |
|
13 | 18 |
|
14 |
- stub.any_instance_of(Agents::TwilioAgent).send_message {} |
|
15 |
- end |
|
19 |
+ @sent_messages = [] |
|
20 |
+ stub.any_instance_of(Agents::TwilioAgent).send_message { |client, message| @sent_messages << message} |
|
21 |
+ end |
|
22 |
+ |
|
23 |
+ describe '#receive' do |
|
24 |
+ it 'should queue any payload it receives' do |
|
25 |
+ event1 = Event.new |
|
26 |
+ event1.agent = agents(:bob_rain_notifier_agent) |
|
27 |
+ event1.payload = 'Some payload' |
|
28 |
+ event1.save! |
|
16 | 29 |
|
17 |
- describe "#receive" do |
|
18 |
- it "should queue any payload it receives" do |
|
19 |
- event1 = Event.new |
|
20 |
- event1.agent = agents(:bob_rain_notifier_agent) |
|
21 |
- event1.payload = "Some payload" |
|
22 |
- event1.save! |
|
23 |
- |
|
24 |
- event2 = Event.new |
|
25 |
- event2.agent = agents(:bob_weather_agent) |
|
26 |
- event2.payload = "More payload" |
|
27 |
- event2.save! |
|
28 |
- |
|
29 |
- Agents::TwilioAgent.async_receive(@checker.id, [event1.id,event2.id]) |
|
30 |
- @checker.reload.memory[:queue].should == ["Some payload", "More payload"] |
|
31 |
- end |
|
30 |
+ event2 = Event.new |
|
31 |
+ event2.agent = agents(:bob_weather_agent) |
|
32 |
+ event2.payload = 'More payload' |
|
33 |
+ event2.save! |
|
34 |
+ |
|
35 |
+ Agents::TwilioAgent.async_receive(@checker.id, [event1.id, event2.id]) |
|
36 |
+ @checker.reload.memory[:queue].should == ['Some payload', 'More payload'] |
|
37 |
+ @sent_messages.should be_empty |
|
32 | 38 |
end |
39 |
+ end |
|
33 | 40 |
|
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 |
|
41 |
+ describe '#working?' do |
|
42 |
+ it 'checks if events have been received within the expected receive period' do |
|
43 |
+ @checker.should_not be_working # No events received |
|
44 |
+ Agents::TwilioAgent.async_receive @checker.id, [@event.id] |
|
45 |
+ @checker.reload.should be_working # Just received events |
|
46 |
+ two_days_from_now = 2.days.from_now |
|
47 |
+ stub(Time).now { two_days_from_now } |
|
48 |
+ @checker.reload.should_not be_working # More time has passed than the expected receive period without any new events |
|
43 | 49 |
end |
50 |
+ end |
|
44 | 51 |
|
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 == [] |
|
52 |
+ describe '#check' do |
|
53 |
+ before do |
|
54 |
+ Agents::TwilioAgent.async_receive @checker.id, [@event.id] |
|
55 |
+ end |
|
53 | 56 |
|
54 |
- end |
|
57 |
+ it 'should send text message and Memory should be empty after that' do |
|
58 |
+ @checker.reload.memory[:queue].should == [ { :message => 'Gonna rain..' } ] |
|
59 |
+ Agents::TwilioAgent.async_check(@checker.id) |
|
60 |
+ @checker.reload.memory[:queue].should == [] |
|
61 |
+ @sent_messages.should == ['Gonna rain..'] |
|
55 | 62 |
end |
63 |
+ end |
|
56 | 64 |
end |