Sha256: 29707346ff1060f51dff7a73ebd03d61b847729ab516d47f02275a5983dd2fdb

Contents?: true

Size: 1.88 KB

Versions: 10

Compression:

Stored size: 1.88 KB

Contents

module Telesms
  # This class represents an outgoing message.
  class Outgoing
    extend Base

    # @return [String]
    # The FROM address.
    attr_accessor :from

    # @return [String]
    # The TO address (will be concatinated with a provider).
    attr_accessor :to

    # @return [String]
    # The provider for the number.
    attr_accessor :provider

    # @return [String]
    # The message body being sent.
    attr_accessor :message

    # This method creates a new outgoing message and sends it.
    #
    # @param [String] from
    #   The FROM address.
    #
    # @param [String] to
    #   The TO address.
    #
    # @param [String] provider
    #   The provider name.
    #
    # @param [String] message
    #   The message being sent.
    #
    # @return [Mail]
    def self.deliver(from, to, provider, message)
      self.new(from, to, provider, message).deliver
    end

    # This method creates a new outgoing message.
    #
    # @param [String] from
    #   The FROM address.
    #
    # @param [String] to
    #   The TO address.
    #
    # @param [String] provider
    #   The provider name.
    #
    # @param [String] message
    #   The message being sent.
    #
    # @return [Outgoing]
    def initialize(from, to, provider, message)
      @from     = from
      @to       = to
      @provider = provider
      @message  = message
    end

    # This method sends an email message disguised as an SMS message.
    #
    # @return [Mail]
    def deliver
      Mail.new(from: from, to: formatted_to, body: sanitized_message).deliver!
    end

    # This method formats the TO address to include the provider.
    #
    # @return [String]
    def formatted_to
      "#{to}@#{Base.gateways[@provider][:sms]}" rescue raise "Invalid provider"
    end

    # This method sanitizes the message body.
    #
    # @return [String]
    def sanitized_message
      message.to_s[0,140]
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
telesms-0.2.1 lib/telesms/outgoing.rb
telesms-0.2.0 lib/telesms/outgoing.rb
telesms-0.1.9 lib/telesms/outgoing.rb
telesms-0.1.8 lib/telesms/outgoing.rb
telesms-0.1.7 lib/telesms/outgoing.rb
telesms-0.1.6 lib/telesms/outgoing.rb
telesms-0.1.5 lib/telesms/outgoing.rb
telesms-0.1.4 lib/telesms/outgoing.rb
telesms-0.1.3 lib/telesms/outgoing.rb
telesms-0.1.2 lib/telesms/outgoing.rb