Sha256: acf10930d1760a10134bc82e28f734438a11b1775eed9dcf927369ae8757045d

Contents?: true

Size: 1.61 KB

Versions: 2

Compression:

Stored size: 1.61 KB

Contents

require 'system_timer'
require 'base64'

module Messenger

  MESSAGER_VERSION = [0,1] unless defined?(MESSAGER_VERSION)
  APP_ROOT = File.expand_path(File.dirname(__FILE__) + '/..') unless defined?(APP_ROOT)

  def self.version
    MESSAGER_VERSION.join(".")
  end

  def self.root
    APP_ROOT
  end


  def self.valid_url?(url)
    service_handler = handler(url)
    service_handler.valid_url?(url)
  rescue ProtocolError
    false
  end

  def self.deliver(url, message, options={})
    service_handler = handler(url)
    SystemTimer.timeout_after(options[:timeout] || 15) do
      service_handler.deliver(url, message, options)
    end
  end

  def self.obfuscate(url)
    service_handler = handler(url)
    service_handler.obfuscate(url)
  end


  def self.protocol(url)
    # TODO: More services
    #   sms://1231231234
    #   twitter://username
    #   aim://username
    case url
    when /^http/:      :http
    when /^campfire/:  :campfire
    when /^jabber/:    :jabber
    when /^notifo/:    :notifo
    when /^mailto|@+/: :email
    end
  end

  def self.handler(url)
    case protocol(url)
    when :email:    Messenger::Email
    when :http:     Messenger::Web
    when :campfire: Messenger::Campfire
    when :jabber:   Messenger::Jabber
    when :notifo:   Messenger::Notifo
    else
      raise Messenger::ProtocolError, "Malformed service URL: #{url}. Either this syntax is wrong or this service type is not yet implemented."
    end
  end

  def self.basic_auth(user, password)
    encoded_credentials = ["#{user}:#{password}"].pack("m*").gsub(/\n/, '')
    {"HTTP_AUTHORIZATION" => "Basic #{encoded_credentials}"}
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
messenger-0.3.1 lib/messenger/messenger.rb
messenger-0.3.0 lib/messenger/messenger.rb