Class: Mobitex::Message

Inherits:
Object
  • Object
show all
Includes:
Patterns
Defined in:
lib/mobitex/message.rb,
lib/mobitex/message/patterns.rb,
lib/mobitex/message/configuration.rb

Defined Under Namespace

Modules: Patterns Classes: Configuration

Constant Summary

Constants included from Patterns

Patterns::ALPHANUMERIC_FROM_REGEXP, Patterns::BULK_DELIMITER, Patterns::BULK_NUMBERS_LIMIT, Patterns::BULK_NUMBERS_REGEXP, Patterns::DOUBLE_CHARACTERS, Patterns::FROM_REGEXP, Patterns::MAX_LENGTH, Patterns::MESSAGE_ID_CHARACTERS, Patterns::MESSAGE_ID_REGEXP, Patterns::NON_WHITESPACE_REGEXP, Patterns::NUMBER_REGEXP, Patterns::NUMERIC_FROM_REGEXP, Patterns::SPECIAL_CHARACTER_REGEXP, Patterns::STANDARD_CHARACTERS, Patterns::TYPES, Patterns::WAP_PUSH_REGEXP

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Message

Public: Initialize a new Message.

args - Hash of attributes of the message. block - An optional block that can be used to build a new message using DSL-like syntax.

Examples

message = Mobitex::Message.new(:to => '48123456789', :body => 'Hello!')

message = Mobitex::Message.new do
  to   '48123456789'
  body 'Hello!'
end

message = Mobitex::Message.new do |m|
  m.to   = '48123456789'
  m.body = 'Hello!'
end


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mobitex/message.rb', line 53

def initialize(*args, &block)
  @delivery_handler = nil
  @delivery_method  = Mobitex.delivery_method.dup

  options = args.first.respond_to?(:each_pair) ? args.first : {}
  Configuration::VALID_OPTIONS.each do |key|
    send("#{key}=", options[key] || options[key.to_sym] || Configuration.instance.send("#{key}"))
  end

  if block_given?
    if block.arity == 1
      yield self
    else
      instance_eval &block
    end
  end

  self
end

Instance Attribute Details

#delivery_handlerObject

Returns the value of attribute delivery_handler.



9
10
11
# File 'lib/mobitex/message.rb', line 9

def delivery_handler
  @delivery_handler
end

Class Method Details

.configure(&block) ⇒ Object

Public: Set default values for Message attributes.

Examples

Mobitex::Message.configure do |config|
  config.body = 'My default body'
end

message = Mobitex::Message.new
message.body
# => 'My default body'


24
25
26
27
28
29
30
31
32
# File 'lib/mobitex/message.rb', line 24

def self.configure(&block)
  return unless block_given?

  if block.arity == 1
    yield Configuration.instance
  else
    Configuration.instance.instance_eval &block
  end
end

Instance Method Details

#body_valid?Boolean

Returns:

  • (Boolean)


127
128
129
130
# File 'lib/mobitex/message.rb', line 127

def body_valid?
  (!type_valid? && !(body !~ NON_WHITESPACE_REGEXP)) || # We can't determine validity of body if type is invalid
      (length > 0 && length <= MAX_LENGTH[type] && !(body !~ NON_WHITESPACE_REGEXP) && (type != 'wap_push' || !(body !~ WAP_PUSH_REGEXP)))
end

#deliverObject

Delivery #########################################################################################################



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/mobitex/message.rb', line 154

def deliver
  if delivery_handler
    delivery_handler.deliver_sms(self) { do_delivery }
  else
    do_delivery
  end

  inform_observers

  self
end

#deliver!Object



166
167
168
169
170
171
172
# File 'lib/mobitex/message.rb', line 166

def deliver!
  response = delivery_method.deliver!(self)

  inform_observers

  delivery_method.settings[:return_response] ? response : self
end

#delivery_method(method = nil, settings = {}) ⇒ Object



174
175
176
177
178
179
180
# File 'lib/mobitex/message.rb', line 174

def delivery_method(method = nil, settings = {})
  if method
    @delivery_method = Mobitex::Configuration.instance.lookup_delivery_method(method).new(settings)
  else
    @delivery_method
  end
end

#errorsObject

Validation #######################################################################################################



106
107
108
# File 'lib/mobitex/message.rb', line 106

def errors
  @errors ||= Set.new
end

#from_valid?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/mobitex/message.rb', line 132

def from_valid?
  !(from.to_s !~ FROM_REGEXP)
end

#invalid?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/mobitex/message.rb', line 110

def invalid?
  !valid?
end

#lengthObject



92
93
94
# File 'lib/mobitex/message.rb', line 92

def length
  body ? body.length + body.count(DOUBLE_CHARACTERS) : 0
end

#message_id_valid?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/mobitex/message.rb', line 136

def message_id_valid?
  !(message_id.to_s !~ MESSAGE_ID_REGEXP)
end

#sanitize!Object

Sanitization #####################################################################################################



142
143
144
145
146
147
148
149
150
# File 'lib/mobitex/message.rb', line 142

def sanitize!
  if from.is_a?(Numeric) || from =~ /^\d+$/
    self.from = from.to_s[0...16]
  else
    self.from = from.to_s.gsub(/[^a-zA-Z0-9]/, '')[0...11]
  end

  self.to = [*to].flatten.map(&:to_s).join(BULK_DELIMITER)
end

#to_valid?Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/mobitex/message.rb', line 123

def to_valid?
  !(to.to_s !~ BULK_NUMBERS_REGEXP)
end

#type(*value) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/mobitex/message.rb', line 96

def type(*value)
  if value.first
    self.type = value.first
  else
    @type ||= length > MAX_LENGTH['sms'] ? 'concat' : 'sms'
  end
end

#type_valid?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/mobitex/message.rb', line 119

def type_valid?
  TYPES.include?(type.to_s)
end

#valid?Boolean

Returns:

  • (Boolean)


114
115
116
117
# File 'lib/mobitex/message.rb', line 114

def valid?
  errors.clear
  validate!
end