Class: Cellcom::Sms

Inherits:
OpenStruct
  • Object
show all
Extended by:
Forwardable
Includes:
Attestor::Validations
Defined in:
lib/cellcom/sms.rb

Constant Summary collapse

MessagePolicy =
Attestor::Policy.new(:attributes) do
  REQUIRED_ATTRIBUTES = [:to, :m]

  def validate!
    validate_require_attributes
    validate_ttl if attributes[:ttl]
    validate_mid if attributes[:mid]
    validate_msg_hexa_encoded if attributes[:cod] == 2
  end

  private

  def validate_ttl
    unless 60 <= attributes[:ttl] && attributes[:ttl] <= 10080
      invalid "invalid ttl: Must be: 60 <= ttl <= 10080(default)"
    end
  end

  def validate_mid
    unless 1 < attributes[:mid] && attributes[:mid] < 4294967295
      invalid "invalid mid: Must be: 1 < mid < 4294967295"
    end
  end

  def validate_require_attributes
    undefined_keys = REQUIRED_ATTRIBUTES - attributes.keys
    unless undefined_keys.empty?
      invalid "invalid message: #{undefined_keys.join(', ')} needs to be defined"
    end
  end

  def validate_msg_hexa_encoded
    unless attributes[:m][/\H/]
      invalid "message needs to be hexadecimal when cod == 2"
    end
  end
end

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Sms

Returns a new instance of Sms.



47
48
49
# File 'lib/cellcom/sms.rb', line 47

def initialize(params={})
  super(default_values.merge(params))
end

Instance Method Details

#deliver(client = Cellcom.client) ⇒ Object



53
54
55
# File 'lib/cellcom/sms.rb', line 53

def deliver(client=Cellcom.client)
  client.deliver(self.to_h)
end

#to_hexObject



57
58
59
# File 'lib/cellcom/sms.rb', line 57

def to_hex
  message.unpack('H*')[0]
end