lib/messenger/jabber.rb in messenger-0.2.0 vs lib/messenger/jabber.rb in messenger-0.3.0

- old
+ new

@@ -1,50 +1,46 @@ require 'xmpp4r-simple' -module Messenger +module Messenger::Jabber - class Jabber + def self.valid_url?(url) + !!matcher(url) + rescue NoMethodError + false + end - def self.valid_url?(url) - !!matcher(url) - rescue NoMethodError - false + # URL format: + # jabber://email@example.com/server_hostname + # + # The server's hostname is optional, but needed for Google Apps jabber accounts. + # + # Options: + # :jabber_id => The jabber id of the sender + # :jabber_password => The password of the sender + def self.deliver(url, body, options={}) + raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url) + recipient, host = url.sub("jabber://", "").split("/")[0,2] + jabber = ::Jabber::Simple.new(options[:jabber_id], options[:jabber_password], host) + jabber.deliver(recipient, body) + pending_messages_count = 1 + until pending_messages_count == 0 + pending_messages_count = jabber.send(:queue, :pending_messages).size + sleep 1 end + status = jabber.subscribed_to?(recipient) + Messenger::Result.new(status, status ? nil : "Not yet authorized") + end - # URL format: - # jabber://email@example.com/server_hostname - # - # The server's hostname is optional, but needed for Google Apps jabber accounts. - # - # Options: - # :jabber_id => The jabber id of the sender - # :jabber_password => The password of the sender - def self.send(url, body, options={}) - raise URLError, "The URL provided is invalid" unless valid_url?(url) - recipient, host = url.sub("jabber://", "").split("/")[0,2] - jabber = ::Jabber::Simple.new(options[:jabber_id], options[:jabber_password], host) - jabber.deliver(recipient, body) - pending_messages_count = 1 - until pending_messages_count == 0 - pending_messages_count = jabber.send(:queue, :pending_messages).size - sleep 1 - end - status = jabber.subscribed_to?(recipient) - Result.new(status, status ? nil : "Not yet authorized") - end + def self.obfuscate(url) + raise Messenger::URLError, "The URL provided is invalid" unless valid_url?(url) + url + end - def self.obfuscate(url) - raise URLError, "The URL provided is invalid" unless valid_url?(url) - url - end +private - private - - def self.matcher(url) - url.sub("jabber://", "").match("@") - end - + def self.matcher(url) + url.sub("jabber://", "").match("@") end end