Commits

John Rizzo committed 13cf0a8

working copy but needs to be productionized

Comments (0)

Files changed (5)

 
 require 'twilio_text'
 
-c = new RunTest()
-c.run_tests
+account_sid = 'ACeab588bc6a0a1ecf0e8a93d8e2541e99'
+auth_token  = '661fa565e11b75c1c253cbdebb4b0ac2'
+from        = '+12012898785' # Your Twilio number
+key         = '+12012455669'
+ip_or_fqdn      = 'portal.caxalt.com'
+interval    = '1m'
+
+c = TwilioText::RunTest.new
+#c.run_single_ping_test ip_or_fqdn, from, key, account_sid, auth_token
+c.run_single_web_test 'https://portal.caxalt.com', from, key, account_sid, auth_token

lib/twilio_text.rb

 require 'twilio_text/version'
-require 'twilio-ruby'
-require 'whenever'
-require 'net/ping'
+require 'twilio_text/run_test'
 
 module TwilioText
 

lib/twilio_text/run_test.rb

 module TwilioText
+  require 'twilio-ruby'
+  require 'rufus-scheduler'
+  require 'net/ping'
+
   class RunTest
-    def run_test
-      account_sid = 'PNb7e63d0c794b36c42153b3c620ab00d2'
-      auth_token  = 'yyyyyyyyyyyyyyyyyyyyyyyyy'
-      client      = Twilio::REST::Client.new account_sid, auth_token
+    def run_scheduled_ping_test(ip_or_fqdn, from, key, account_sid, auth_token, interval)
+      scheduler = Rufus::Scheduler.new
+      scheduler.every interval do
+        ping_test ip_or_fqdn, from, key, account_sid, auth_token
+      end
+      scheduler.join
+    end
+
+    def run_single_ping_test(ip_or_fqdn, from, key, account_sid, auth_token)
+      ping_test ip_or_fqdn, from, key, account_sid, auth_token
+    end
 
-      from = '+12012898785' # Your Twilio number
+    def run_single_web_test(ip_or_fqdn, from, key, account_sid, auth_token)
+      web_test ip_or_fqdn, from, key, account_sid, auth_token
+    end
 
-      every 1.minute do
-        #runner 'MyModel.some_process'
-        #rake 'my:rake:task'
-        #command '/usr/bin/my_great_command'
+    private
+    def web_test(ip_or_fqdn, from, key, account_sid, auth_token)
+      np = Net::Ping::HTTP.new(ip_or_fqdn)
+      send_sms_if_ping(account_sid, auth_token, from, ip_or_fqdn, key, np)
+    end
 
-        portal = 'portal.caxalt.com'
-        p2     = Net::Ping::UDP.new(portal)
-        if p2.ping?
-          client.account.messages.create(
-            :from => from,
-            :to   => key,
-            :body => "#{portal} is online!!!"
-          )
-          puts 'Sent message'
-        else
-          puts portal + ' is not alive'
-        end
+    def ping_test(ip_or_fqdn, from, key, account_sid, auth_token)
+      np = Net::Ping::UDP.new(ip_or_fqdn)
+      send_sms_if_ping(account_sid, auth_token, from, ip_or_fqdn, key, np)
+    end
 
+    def send_sms_if_ping(account_sid, auth_token, from, ip_or_fqdn, key, np)
+      if np.ping?
+        puts 'Sent message'
+      else
+        send_sms from, key, account_sid, auth_token, "#{ip_or_fqdn} is online!!!"
+        puts ip_or_fqdn + ' is not alive'
       end
     end
+
+    def send_sms(from, key, account_sid, auth_token, msg)
+      client = Twilio::REST::Client.new account_sid, auth_token
+      client.account.messages.create(
+        :from => from,
+        :to   => key,
+        :body => msg
+      )
+    end
   end
 end

lib/twilio_text/version.rb

 module TwilioText
-  VERSION = "0.0.1"
+  VERSION = '0.0.1'
 end

twilio_text.gemspec

 
   spec.add_development_dependency 'bundler', '~> 1.6'
   spec.add_development_dependency 'rake', '~> 10.0'
-  spec.add_development_dependency 'twilio-ruby', '~> 3.11.5'
-  spec.add_development_dependency 'whenever', '~> 0.9.2'
-  spec.add_development_dependency 'net-ping', '~> 1.7.4'
+  spec.add_development_dependency 'twilio-ruby', '~> 3.11', '>= 3.11.5'
+  spec.add_development_dependency 'rufus-scheduler', '~> 3.0', '>= 3.0.8'
+  spec.add_development_dependency 'net-ping', '~> 1.7', '>= 1.7.4'
 end