Class: Mblox::Sms
- Inherits:
-
Object
- Object
- Mblox::Sms
- Defined in:
- lib/mblox/sms.rb
Defined Under Namespace
Classes: BatchIdOutOfRangeError, InvalidMessageError, InvalidPhoneNumberError, InvalidSenderIdError, MessageTooLongError
Constant Summary collapse
- MAX_LENGTH =
160
- MAX_SECTION_LENGTH =
MAX_LENGTH - "(MSG XXX/XXX): ".size
- LEGAL_CHARACTERS =
"~\`!\"#\$\%&'\(\)*+,-.\/:;<=>?@_£¤¥§¿i¡ÄÅÆÇÉÑÖØÜßâáäåæçèéìíñòöøóùüú\n\r\t "
- ILLEGAL_CHARACTERS =
/([^a-zA-Z0-9#{LEGAL_CHARACTERS}\\])/
- ON_MESSAGE_TOO_LONG_HANDLER =
{ :raise_error => Proc.new { raise MessageTooLongError, "Message cannot be longer than #{MAX_LENGTH} characters" }, :truncate => Proc.new { || Mblox.log "Truncating message due to length. Message was: \"#{}\" but will now be \"#{ = [0,MAX_LENGTH]}\""; [] }, :split => Proc.new { || () } }
Instance Attribute Summary collapse
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#phone ⇒ Object
readonly
Returns the value of attribute phone.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(phone, message, batch_id = nil) ⇒ Sms
constructor
A new instance of Sms.
- #send ⇒ Object
- #send_from(sender_id, service_id = nil) ⇒ Object
Constructor Details
#initialize(phone, message, batch_id = nil) ⇒ Sms
Returns a new instance of Sms.
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mblox/sms.rb', line 28 def initialize(phone, , batch_id=nil) phone = phone.to_s raise InvalidPhoneNumberError, "Phone number must be ten digits" unless /\A[0-9]{10}\z/.match(phone) raise InvalidPhoneNumberError, "Phone number cannot begin with 0 or 1" if ['0','1'].include?(phone[0].to_s) raise InvalidMessageError, "Message cannot be blank" if .empty? illegal_characters = ILLEGAL_CHARACTERS.match().to_a raise InvalidMessageError, "Message cannot contain the following special characters: #{illegal_characters.uniq.join(', ')}" unless illegal_characters.size.zero? Mblox.log "WARNING: Some characters may be lost because the message must be broken into at least 1000 sections" if .size > (999 * MAX_SECTION_LENGTH) @message = (.size > MAX_LENGTH) ? ON_MESSAGE_TOO_LONG_HANDLER[Mblox.config.].call() : [.dup] @phone = "1#{phone}" raise BatchIdOutOfRangeError, "batch_id must be in the range 1 to #{MAX_BATCH_ID}. The batch_id specified (#{batch_id}) is out of range." if !batch_id.blank? && (MAX_BATCH_ID < batch_id.to_i) @batch_id = batch_id.to_i unless batch_id.blank? end |
Instance Attribute Details
#message ⇒ Object (readonly)
Returns the value of attribute message.
20 21 22 |
# File 'lib/mblox/sms.rb', line 20 def @message end |
#phone ⇒ Object (readonly)
Returns the value of attribute phone.
20 21 22 |
# File 'lib/mblox/sms.rb', line 20 def phone @phone end |
Class Method Details
.http ⇒ Object
111 112 113 |
# File 'lib/mblox/sms.rb', line 111 def http @http ||= Net::HTTP.new(url.host, url.port) end |
.request ⇒ Object
114 115 116 117 118 119 |
# File 'lib/mblox/sms.rb', line 114 def request return @request if @request @request = Net::HTTP::Post.new(url.request_uri) @request.content_type = 'text/xml' @request end |
Instance Method Details
#send ⇒ Object
51 52 53 |
# File 'lib/mblox/sms.rb', line 51 def send @message.collect { || commit build() } end |
#send_from(sender_id, service_id = nil) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/mblox/sms.rb', line 42 def send_from(sender_id, service_id=nil) raise InvalidSenderIdError, "You can only send from a 5-digit shortcode" unless Mblox.is_a_five_digit_number?(sender_id) @sender_id = sender_id.to_i.to_s unless service_id.nil? raise InvalidSenderIdError, "You can only send using a 5-digit service ID. Leave out the 2nd argument of send_from to use the globally configured '#{Mblox.config.service_id}'" unless Mblox.is_a_five_digit_number?(service_id) @service_id = service_id.to_i.to_s end end |