Sha256: 57cff82329d7461aecfa73644eecc883ae0ad9f7872934cd425b0b18b3318ff8

Contents?: true

Size: 1.15 KB

Versions: 9

Compression:

Stored size: 1.15 KB

Contents

module Smsapi
  class SMS
    include Smsapi::Defaults

    attr_accessor :to, :message, :id, :points, :status, :error_code, :date
    def initialize(to, message, server, options = {})
      @options = default_options.merge options
      @to = to
      @message = message
      @server = server
    end

    def deliver
      read_response @server.sms(server_params).first
      self
    end

    def deliver_at(date)
      @date = date
      params = server_params.merge(date: date.to_time.to_i)

      read_response @server.sms(params).first
      self
    end

    def sent?
      not self.status.nil?
    end

    def error?
      self.status == 'ERROR'
    end

    def success?
      self.status == 'SUCCESS'
    end

    def error_message
      Smsapi::ERROR_MESSAGES[error_code]
    end

    def read_response(response)
      response = response.split(':')

      self.status = response[0]
      if status == 'ERROR'
        self.error_code = response[1]
      else
        self.id = response[1]
        self.points = response[2]
      end
    end

    private

    def server_params
      @options.merge(
        to: to,
        message: message
      )
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
smsapipl-client-1.0.4 lib/smsapi/sms.rb
smsapipl-client-1.0.3 lib/smsapi/sms.rb
smsapipl-client-1.0.2 lib/smsapi/sms.rb
smsapi-client-1.0.2 lib/smsapi/sms.rb
smsapi-client-1.0.1 lib/smsapi/sms.rb
smsapi-client-1.0.0 lib/smsapi/sms.rb
smsapi-client-0.3.1 lib/smsapi/sms.rb
smsapi-client-0.3.0 lib/smsapi/sms.rb
smsapi-client-0.2.0 lib/smsapi/sms.rb