Guest User

Untitled

a guest
Jul 3rd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. require 'timeout'
  2.  
  3. module Nagger::Plugin
  4. class Twitter
  5. def initialize(config)
  6. puts("Creating new insance of Twitter plugin")
  7.  
  8. @config = config
  9. @lastfailure = 0
  10. end
  11.  
  12. def self.register
  13. Nagger::Util.register_plugin("twitter", "Twitter")
  14. end
  15.  
  16. def send(recipient, subject, msg)
  17. puts("#{self.class} Sending message to '#{recipient}' with subject '#{subject}' and body '#{msg}'")
  18.  
  19. user = @config["username"]
  20. password = @config["password"]
  21. url = @config["url"]
  22.  
  23. # if we had a failed delivery in the last 10 minutes do not try to send a new message
  24. # this is just to prevent us spamming twitter and getting in their bad books
  25. if Time.now.to_i - @lastfailure.to_i > 600
  26. begin
  27. Timeout::timeout(10) do
  28. result = %x[curl -s -S -u #{user}:#{password} -d status="#{msg}" #{url}]
  29.  
  30. if result.include? "error"
  31. @lastfailure = Time.now
  32. raise(Nagger::PluginConnectionError, "Update Failure")
  33. else
  34. @lastfailure = 0
  35. end
  36. end
  37. rescue Timeout::Error => e
  38. @lastfailure = Time.now
  39. raise(Nagger::PluginConnectionError, "Failed to connect to twitter within 10 seconds")
  40. end
  41. else
  42. raise(Nagger::PluginConnectionError, "Not delivering message, we've had failures in the last 10 mins")
  43. end
  44. end
  45. end
  46. end
Add Comment
Please, Sign In to add comment