Class | OpenWFE::Extras::TwitterParticipant |
In: |
lib/openwfe/extras/participants/twitterparticipants.rb
|
Parent: | Object |
Sometimes email is a bit too heavy for notification, this participant emit messages via a twitter account.
By default, the message emitted is the value of the field "twitter_message", but this behaviour can be changed by overriding the extract_message() method.
If the extract_message doesn‘t find a message, the message will be the result of the default_message method call, of course this method is overridable as well.
client | [RW] | The actual twitter4r client instance. |
params | [RW] | Keeping the initialization params at hand (if any) |
This participant expects a login (twitter user name) and a password.
The only optional param for now is :no_ssl, which you can set to true if you want the connection to twitter to not use SSL. (seems like the Twitter SSL service is available less often than the plain http one).
# File lib/openwfe/extras/participants/twitterparticipants.rb, line 92 92: def initialize (login, password, params={}) 93: 94: super() 95: 96: Twitter::Client.configure do |conf| 97: conf.protocol = :http 98: conf.port = 80 99: end if params[:no_ssl] == true 100: 101: @client = Twitter::Client.new( 102: :login => login, 103: :password => password) 104: 105: @params = params 106: end
The method called by the engine when a workitem for this participant is available.
# File lib/openwfe/extras/participants/twitterparticipants.rb, line 112 112: def consume (workitem) 113: 114: user, tmessage = extract_message workitem 115: 116: tmessage = default_message(workitem) unless tmessage 117: 118: begin 119: 120: if user 121: # 122: # direct message 123: # 124: tuser = @client.user user.to_s 125: @client.message :post, tmessage, tuser 126: else 127: # 128: # just the classical status 129: # 130: @client.status :post, tmessage 131: end 132: 133: rescue Exception => e 134: 135: linfo do 136: "consume() not emitted twitter, because of " + 137: OpenWFE::exception_to_s(e) 138: end 139: end 140: 141: reply_to_engine(workitem) if @application_context 142: end
Returns the default message (called when the extract_message returned nil as the second element of its pair).
This default implementation simply returns the workitem FlowExpressionId instance in its to_s() representation.
# File lib/openwfe/extras/participants/twitterparticipants.rb, line 168 168: def default_message (workitem) 169: 170: workitem.fei.to_s 171: end
Returns a pair : the target user (twitter login name) and the message (or status message if there is no target user) to send to Twitter.
This default implementation returns a pair composed with the values of the field ‘twitter_target’ and of the field ‘twitter_message’.
# File lib/openwfe/extras/participants/twitterparticipants.rb, line 155 155: def extract_message (workitem) 156: 157: [ workitem.attributes['twitter_target'], 158: workitem.attributes['twitter_message'] ] 159: end