Skip to content

Commit

Permalink
Convert Twitter contact to new style
Browse files Browse the repository at this point in the history
  • Loading branch information
mojombo committed Jun 30, 2010
1 parent 852b5b1 commit 8ec3ff0
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions lib/god/contacts/twitter.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
# For Twitter updates you need the 'twitter' gem
# (gem install twitter)
# Send a notice to a Twitter account (http://twitter.com/).
#
# Configure your watches like this:
#
# God::Contacts::Twitter.settings = { :username => 'sender@example.com',
# :password => 'secret' }
# God.contact(:twitter) do |c|
# c.name = 'Tester'
# c.group = 'developers'
# end
# consumer_token - The String OAuth consumer token (defaults to God's
# existing consumer token).
# consumer_secret - The String OAuth consumer secret (defaults to God's
# existing consumer secret).
# access_token - The String OAuth access token.
# access_secret - The String OAuth access secret.

require 'twitter'

module God
module Contacts
class Twitter < Contact
class << self
attr_accessor :settings
attr_accessor :consumer_token, :consumer_secret,
:access_token, :access_secret
end

self.consumer_token = 'gOhjax6s0L3mLeaTtBWPw'
self.consumer_secret = 'yz4gpAVXJHKxvsGK85tEyzQJ7o2FEy27H1KEWL75jfA'

def valid?
valid = true
valid &= complain("Attribute 'consumer_token' must be specified", self) unless arg(:consumer_token)
valid &= complain("Attribute 'consumer_secret' must be specified", self) unless arg(:consumer_secret)
valid &= complain("Attribute 'access_token' must be specified", self) unless arg(:access_token)
valid &= complain("Attribute 'access_secret' must be specified", self) unless arg(:access_secret)
valid
end

attr_accessor :consumer_token, :consumer_secret,
:access_token, :access_secret

def notify(message, time, priority, category, host)
begin
::Twitter::Base.new(Twitter.settings[:username],
Twitter.settings[:password]).update(message)

self.info = "sent twitter update as #{Twitter.settings[:username]}"
rescue => e
self.info = "failed to send twitter update from #{self.twitter_id}: #{e.message}"
end
oauth = ::Twitter::OAuth.new(arg(:consumer_token), arg(:consumer_secret))
oauth.authorize_from_access(arg(:access_token), arg(:access_secret))

::Twitter::Base.new(oauth).update(message)

self.info = "sent twitter update"
rescue => e
applog(nil, :info, "failed to send twitter update: #{e.message}")
applog(nil, :debug, e.backtrace.join("\n"))
end
end
end
Expand Down

0 comments on commit 8ec3ff0

Please sign in to comment.