Class: Mblox::Sms
- Inherits:
-
Object
- Object
- Mblox::Sms
- Defined in:
- lib/mblox/sms.rb
Defined Under Namespace
Classes: BatchIdOutOfRangeError, InvalidMessageError, InvalidPhoneNumberError
Constant Summary collapse
- MAX_LENGTH =
160- MAX_SECTION_LENGTH =
MAX_LENGTH - "(MSG XXX/XXX): ".size
- ILLEGAL_CHARACTERS =
/([^a-zA-Z0-9!"#$\%&'\(\)*+,-.\/:;<=>?@_£¤¥§¿i¡ÄÅÆÇÉÑÖØÜßáäåæèéìñòöøùü\n\r\tí ])/- ON_MESSAGE_TOO_LONG_HANDLER =
{ :raise_error => Proc.new { raise InvalidMessageError, "Message cannot be longer than #{MAX_LENGTH} characters" }, :truncate => Proc.new { || Mblox.log "Truncating message due to length. Message was: \"#{message}\" but will now be \"#{message = message[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
Constructor Details
#initialize(phone, message, batch_id = nil) ⇒ Sms
Returns a new instance of Sms.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mblox/sms.rb', line 25 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) = (.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 && (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.
17 18 19 |
# File 'lib/mblox/sms.rb', line 17 def end |
#phone ⇒ Object (readonly)
Returns the value of attribute phone.
17 18 19 |
# File 'lib/mblox/sms.rb', line 17 def phone @phone end |
Class Method Details
.http ⇒ Object
99 100 101 |
# File 'lib/mblox/sms.rb', line 99 def http @http ||= Net::HTTP.new(url.host, url.port) end |
.request ⇒ Object
102 103 104 105 106 107 |
# File 'lib/mblox/sms.rb', line 102 def request return @request if @request @request = Net::HTTP::Post.new(url.request_uri) @request.content_type = 'text/xml' @request end |
.url ⇒ Object
96 97 98 |
# File 'lib/mblox/sms.rb', line 96 def url @url ||= URI.parse(URI.escape(Mblox.config.outbound_url)) end |
Instance Method Details
#send ⇒ Object
39 40 41 |
# File 'lib/mblox/sms.rb', line 39 def send .collect { || commit build() } end |