Sha256: d94479a9d3c2ce78c450281241e76c0f6bfa78229d765a63306474ea90ca8541

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

require 'base64'

module Messenger

  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)
    if defined?(SystemTimer) && SystemTimer.respond_to?(:timeout_after)
      SystemTimer.timeout_after(options[:timeout] || 15) do
        service_handler.deliver(url, message, options)
      end
    else
      Timeout.timeout(options[:timeout] || 15) do
        service_handler.deliver(url, message, options)
      end
    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 /\Ahttp/ then      :http
    when /\Acampfire/ then  :campfire
    when /\Aslack/ then     :slack
    when /\Ajabber/ then    :jabber
    when /\Amailto|@+/ then :email
    end
  end

  def self.handler(url)
    case protocol(url)
    when :email then    Messenger::Email
    when :http then     Messenger::Web
    when :campfire then Messenger::Campfire
    when :jabber then   Messenger::Jabber
    when :slack then    Messenger::Slack
    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

1 entries across 1 versions & 1 rubygems

Version Path
messenger-0.7.1 lib/messenger/messenger.rb