class Twitter2Jabber::JabberClient

Constants

DEFAULT_FORMAT
DEFAULT_TEMPLATES
JABBER_NS
XHTML_NS

Attributes

config[R]
spec[R]

Public Class Methods

new(gw, config) click to toggle source
# File lib/twitter2jabber/jabber_client.rb, line 41
def initialize(gw, config)
  @gw, @config, @spec = gw, config, config[:username]

  @format = config.fetch(:format, DEFAULT_FORMAT).to_s

  if File.readable?(template = File.expand_path(File.join(
    config[:templates] || DEFAULT_TEMPLATES, "tweet.#{@format}")))

    @erb = ERB.new(File.read(template))
  else
    raise ArgumentError, "format not supported: #{@format}"
  end
end

Public Instance Methods

client() click to toggle source
# File lib/twitter2jabber/jabber_client.rb, line 57
def client
  @client ||= Jabber::Client.new(spec).tap { |client|
    client.connect
    client.auth(config[:password])
  }
end
connect() click to toggle source
# File lib/twitter2jabber/jabber_client.rb, line 64
def connect
  deliver(Jabber::Presence.new(nil, 'Available'), nil)
  log 'connected'
rescue Jabber::JabberError, Errno::ETIMEDOUT => err
  raise "Can't connect to Jabber with JID '#{spec}': #{err}"
end
deliver(msg, recipient = config[:recipient]) click to toggle source
# File lib/twitter2jabber/jabber_client.rb, line 85
def deliver(msg, recipient = config[:recipient])
  msg = format(msg) unless msg.is_a?(Jabber::XMPPStanza)
  msg.to = Jabber::JID.new(recipient).strip if recipient

  @gw.debug ? log("#{recipient}: #{msg}") : send_msg(msg)
rescue => err
  warn "#{err} (#{err.class})"
  retry if err.is_a?(Jabber::ServerDisconnected)
end
disconnect() click to toggle source
# File lib/twitter2jabber/jabber_client.rb, line 71
def disconnect
  close if @client
  log 'disconnected'
end
format(tweet) click to toggle source
# File lib/twitter2jabber/jabber_client.rb, line 76
def format(tweet)
  user = tweet.user
  text = @erb.result(binding)

  msg = Jabber::Message.new.set_type(:chat)
  msg.add_element(format_element(text))
  msg
end

Private Instance Methods

close() click to toggle source
# File lib/twitter2jabber/jabber_client.rb, line 126
def close
  begin
    @client.close
  rescue Errno::EPIPE, IOError
  end

  @client = nil
end
format_element(text) click to toggle source

cf. <devblog.famundo.com/articles/2006/10/18/ruby-and-xmpp-jabber-part-3-adding-html-to-the-messages>

# File lib/twitter2jabber/jabber_client.rb, line 98
def format_element(text)
  twitter = @gw.twitter

  text, body = twitter.process_message(text), REXML::Element.new('body')

  if @format == 'html'
    REXML::Text.new(twitter.process_html(text), false, body, true, nil, /.^/)

    html = REXML::Element.new('html').add_namespace(JABBER_NS)
    html.add(body.add_namespace(XHTML_NS))
    html
  else
    REXML::Text.new(twitter.process_text(text), true, body, true, nil, /.^/)
    body
  end
end
log(msg) click to toggle source
# File lib/twitter2jabber/jabber_client.rb, line 135
def log(msg)
  @gw.log("JABBER #{msg}")
end
send_msg(msg, attempts = 0) click to toggle source
# File lib/twitter2jabber/jabber_client.rb, line 115
def send_msg(msg, attempts = 0)
  attempts += 1
  client.send(msg)
rescue Errno::EPIPE, IOError
  raise if attempts > 3

  close
  sleep 1
  retry
end