Sha256: f831b26bbfc2ab5d870f5231b1992b402f89f5c35b02c7227370cf1f9d7f7b5d

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

require 'xmpp4r-simple'
require 'htmlentities/string'

module Whistle
  class Switchbox
    attr_reader :user
    
    def initialize(user,password,subscribers)
      @user, @password, @subscribers = user, password, subscribers
      @jabber = Jabber::Simple.new(@user, @password)
    end
    
    def deliver(messages)
      @subscribers.each do |to| 
        Array(messages).each do |body| 
          format_and_send :to => to, :body => body  
        end
      end
    end
    
    # Sends message to primary (first) subscriber only
    def inform(message)
      if (primary = @subscribers.first)
        format_and_send :to => @subscribers.first, :body => message
      end
    end
private    
    # Processes html in the messages and sends it to a recipient
    def format_and_send(options)
      m = Jabber::Message::new(options[:to],options[:body]).set_type(:normal)
      r = REXML::Document.new(options[:body]) rescue nil
      if r && r.root && r.root.name == 'html'
        # We have a html message here, prepare for sending via jabber
        m.body = strip_tags(options[:body]) # Make a plaintext version
        r.root.add_namespace('http://jabber.org/protocol/xhtml-im')
        r.root.elements['body'].add_namespace('http://www.w3.org/1999/xhtml')
        m.add_element(r)
      end
      # Use deliver from xmpp4r-simple so it will try adding contact first and queue the delivery
      @jabber.deliver(options[:to],m) 
    end
    
    def strip_tags(html)
      html.gsub(/<.+?>/,'').
      gsub(/&amp;/,'&').
      gsub(/&quot;/,'"').
      gsub(/&lt;/,'<').
      gsub(/&gt;/,'>').
      gsub(/&ellip;/,'...').
      gsub(/&apos;/, "'").
      gsub("\n",'')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
whistle-0.1 lib/switchbox.rb
whistle-0.1.1 lib/switchbox.rb