Class: Messed::Interface::Adapter::TwitterSender

Inherits:
Messed::Interface::Adapter show all
Defined in:
lib/messed/interface/adapter/twitter_sender.rb

Constant Summary

Constants inherited from Messed::Interface::Adapter

Registry

Instance Attribute Summary

Attributes inherited from Messed::Interface::Adapter

#interface

Instance Method Summary collapse

Methods inherited from Messed::Interface::Adapter

for_name, #init, #initialize, #load_state, register_for_name, #save_state, #send, #state_file, #type

Methods included from Logger::LoggingModule

included, #logger, #logger=

Constructor Details

This class inherits a constructor from Messed::Interface::Adapter

Instance Method Details

#message_classObject



8
9
10
# File 'lib/messed/interface/adapter/twitter_sender.rb', line 8

def message_class
  Messed::Message::Twitter
end

#process_outgoing(job, message) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/messed/interface/adapter/twitter_sender.rb', line 23

def process_outgoing(job, message)
  if message.private?
    req = EventMachine::HttpRequest.new("http://twitter.com/direct_messages/new.json")
    data = {:body => {:user => message.to_user_id, :text => message.body}, :timeout => 30, :head => {'authorization' => [interface.configuration.options[:username], interface.configuration.options[:password]]}}
    data[:body][:in_reply_to] = message.in_reply_to if message.in_reply_to
    http = req.post(data)
    http.callback {
      job.delete
    }
  else
    http = EventMachine::HttpRequest.new("http://twitter.com/statuses/update.json").
      post(:body => {:status => message.body}, :timeout => 30)
    http.callback {
      job.delete
    }
  end
end

#startObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/messed/interface/adapter/twitter_sender.rb', line 12

def start
  jack = EM::Beanstalk.new
  jack.watch(interface.application.outgoing.tube) do
    jack.use(interface.application.outgoing.tube) do
      jack.each_job do |job|
        process_outgoing(job, message_class.from_json(job.body))
      end
    end
  end
end