Class: ActionSmser::Base
- Inherits:
-
Object
- Object
- ActionSmser::Base
- Defined in:
- lib/action_smser/base.rb
Constant Summary collapse
- SMS_DOUBLE_CHARS =
en.wikipedia.org/wiki/GSM_03.38 , some chars takes 2 spaces
'€[\]^{|}~'
Instance Attribute Summary collapse
-
#body ⇒ Object
INSTANCE METHODS.
-
#delivery_info ⇒ Object
Delivery methods can use this to save data for debugging, e.g.
-
#delivery_options ⇒ Object
Initialized to duplicate of ActionSmser.delivery_options.
-
#from ⇒ Object
INSTANCE METHODS.
-
#re_delivery_of_delivery_report_id ⇒ Object
INSTANCE METHODS.
-
#sms_type ⇒ Object
INSTANCE METHODS.
-
#to ⇒ Object
INSTANCE METHODS.
-
#ttl ⇒ Object
INSTANCE METHODS.
Class Method Summary collapse
-
.message_real_cropped(message, max_length = 159) ⇒ Object
Make sure that double chars are taken account.
- .message_real_length(message) ⇒ Object
-
.method_missing(method, *args) ⇒ Object
:nodoc:.
-
.respond_to?(method, include_private = false) ⇒ Boolean
:nodoc:.
Instance Method Summary collapse
-
#body_encoded_escaped(to = 'ISO-8859-15') ⇒ Object
Most of the gateways want escaped and ISO encoded messages Also make sure that its max 500 chars long.
- #body_escaped ⇒ Object
- #deliver ⇒ Object
- #delivery_method ⇒ Object
-
#delivery_reports ⇒ Object
attr_accessor :delivery_reports.
- #from_encoded ⇒ Object
- #from_escaped ⇒ Object
-
#initialize(method_name = 'no_name_given', *args) ⇒ Base
constructor
Called from class.method_missing with own_sms_message when you call OwnMailer.own_sms_message.
-
#sms(options) ⇒ Object
Main method for creating sms infos.
- #to_as_array ⇒ Object
- #to_encoded ⇒ Object
-
#to_numbers_array ⇒ Object
make sure that to is an array and remove leading ‘+’ or ‘0’ chars.
- #to_s ⇒ Object
- #ttl_to_i ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(method_name = 'no_name_given', *args) ⇒ Base
Called from class.method_missing with own_sms_message when you call OwnMailer.own_sms_message
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/action_smser/base.rb', line 55 def initialize(method_name = 'no_name_given', *args) @delivery_options = {} ActionSmser..each do |key,value| value = value.dup if value.is_a?(Hash) || value.is_a?(Array) @delivery_options[key] = value end @valid = true @sms_action = method_name @sms_type = "#{self.class}.#{@sms_action}" send method_name, *args if respond_to?(method_name) end |
Instance Attribute Details
#body ⇒ Object
INSTANCE METHODS
42 43 44 |
# File 'lib/action_smser/base.rb', line 42 def body @body end |
#delivery_info ⇒ Object
Delivery methods can use this to save data for debugging, e.g. http responses etc
48 49 50 |
# File 'lib/action_smser/base.rb', line 48 def delivery_info @delivery_info end |
#delivery_options ⇒ Object
Initialized to duplicate of ActionSmser.delivery_options
45 46 47 |
# File 'lib/action_smser/base.rb', line 45 def @delivery_options end |
#from ⇒ Object
INSTANCE METHODS
42 43 44 |
# File 'lib/action_smser/base.rb', line 42 def from @from end |
#re_delivery_of_delivery_report_id ⇒ Object
INSTANCE METHODS
42 43 44 |
# File 'lib/action_smser/base.rb', line 42 def re_delivery_of_delivery_report_id @re_delivery_of_delivery_report_id end |
#sms_type ⇒ Object
INSTANCE METHODS
42 43 44 |
# File 'lib/action_smser/base.rb', line 42 def sms_type @sms_type end |
#to ⇒ Object
INSTANCE METHODS
42 43 44 |
# File 'lib/action_smser/base.rb', line 42 def to @to end |
#ttl ⇒ Object
INSTANCE METHODS
42 43 44 |
# File 'lib/action_smser/base.rb', line 42 def ttl @ttl end |
Class Method Details
.message_real_cropped(message, max_length = 159) ⇒ Object
Make sure that double chars are taken account
26 27 28 29 30 31 32 33 34 |
# File 'lib/action_smser/base.rb', line 26 def (, max_length = 159) result = "" length = 0 .to_s.chars.each do |char| length += SMS_DOUBLE_CHARS.include?(char) ? 2 : 1 result << char if length <= max_length end result end |
.message_real_length(message) ⇒ Object
20 21 22 23 24 |
# File 'lib/action_smser/base.rb', line 20 def () i = 0 .to_s.chars.each do |char| i += SMS_DOUBLE_CHARS.include?(char) ? 2 : 1 end i end |
.method_missing(method, *args) ⇒ Object
:nodoc:
8 9 10 11 |
# File 'lib/action_smser/base.rb', line 8 def method_missing(method, *args) #:nodoc: return super unless respond_to?(method) new(method, *args) end |
.respond_to?(method, include_private = false) ⇒ Boolean
:nodoc:
13 14 15 16 |
# File 'lib/action_smser/base.rb', line 13 def respond_to?(method, include_private = false) #:nodoc: #super || public_instance_methods(true).include?(method.to_s) super || method_defined?(method.to_sym) end |
Instance Method Details
#body_encoded_escaped(to = 'ISO-8859-15') ⇒ Object
Most of the gateways want escaped and ISO encoded messages Also make sure that its max 500 chars long
104 105 106 107 108 109 110 |
# File 'lib/action_smser/base.rb', line 104 def body_encoded_escaped(to = 'ISO-8859-15') msg = body.first(500) # This could use better translittering with fallback param, see http://blog.segment7.net/2010/12/17/from-iconv-iconv-to-string-encode msg_encoded = msg.encode(to, :invalid => :replace, :undef => :replace, :replace => '_') CGI.escape(msg_encoded) end |
#body_escaped ⇒ Object
112 113 114 |
# File 'lib/action_smser/base.rb', line 112 def body_escaped CGI.escape(body.to_s.first(500)) end |
#deliver ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/action_smser/base.rb', line 88 def deliver self.send(:before_delivery) if self.respond_to?(:before_delivery) return false unless valid? ActionSmser::Logger.info "Sending sms - Delivery_method: #{[:delivery_method]} - Sms: (#{self.to_s})" response = delivery_method.deliver(self) self.send(:after_delivery, response) if self.respond_to?(:after_delivery) response end |
#delivery_method ⇒ Object
83 84 85 |
# File 'lib/action_smser/base.rb', line 83 def delivery_method ActionSmser::DeliveryMethods.const_get([:delivery_method].to_s.downcase.camelize) end |
#delivery_reports ⇒ Object
attr_accessor :delivery_reports
50 51 52 |
# File 'lib/action_smser/base.rb', line 50 def delivery_reports @delivery_reports ||= [] end |
#from_encoded ⇒ Object
134 135 136 |
# File 'lib/action_smser/base.rb', line 134 def from_encoded from.to_s.gsub(/^(\+|0)/, "") end |
#from_escaped ⇒ Object
138 139 140 |
# File 'lib/action_smser/base.rb', line 138 def from_escaped CGI.escape from_encoded end |
#sms(options) ⇒ Object
Main method for creating sms infos
68 69 70 71 72 73 |
# File 'lib/action_smser/base.rb', line 68 def sms() @body = [:body] @to = [:to] @from = [:from] self end |
#to_as_array ⇒ Object
126 127 128 |
# File 'lib/action_smser/base.rb', line 126 def to_as_array @to.is_a?(Array) ? @to : [@to] end |
#to_encoded ⇒ Object
130 131 132 |
# File 'lib/action_smser/base.rb', line 130 def to_encoded to_numbers_array.join(",") end |
#to_numbers_array ⇒ Object
make sure that to is an array and remove leading ‘+’ or ‘0’ chars
117 118 119 120 121 122 123 124 |
# File 'lib/action_smser/base.rb', line 117 def to_numbers_array array = if @to.is_a?(Array) @to.collect{|number| number.to_s} else [@to.to_s] end array.collect{|number| number.gsub(/^(\+|0{1,2})/, "")} end |
#to_s ⇒ Object
75 76 77 |
# File 'lib/action_smser/base.rb', line 75 def to_s "Sms #{sms_type} - From: #{from.inspect}, To: #{to.inspect}, Body: #{body.inspect}, Valid: #{@valid}" end |
#ttl_to_i ⇒ Object
142 143 144 |
# File 'lib/action_smser/base.rb', line 142 def ttl_to_i ttl.blank? ? ActionSmser.[:default_ttl] : ttl.to_i end |
#valid? ⇒ Boolean
79 80 81 |
# File 'lib/action_smser/base.rb', line 79 def valid? !body.blank? && !from.blank? && !to_numbers_array.collect{|number| number.to_s.blank? ? nil : true}.compact.blank? end |