Class: ActionSmser::Base

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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



56
57
58
59
60
61
62
# File 'lib/action_smser/base.rb', line 56

def initialize(method_name = 'no_name_given', *args)
  @delivery_options = ActionSmser.delivery_options.dup
  @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

#bodyObject

INSTANCE METHODS



43
44
45
# File 'lib/action_smser/base.rb', line 43

def body
  @body
end

#delivery_infoObject

Delivery methods can use this to save data for debugging, e.g. http responses etc



49
50
51
# File 'lib/action_smser/base.rb', line 49

def delivery_info
  @delivery_info
end

#delivery_optionsObject

Initialized to duplicate of ActionSmser.delivery_options



46
47
48
# File 'lib/action_smser/base.rb', line 46

def delivery_options
  @delivery_options
end

#delivery_reportsObject

Returns the value of attribute delivery_reports.



50
51
52
# File 'lib/action_smser/base.rb', line 50

def delivery_reports
  @delivery_reports
end

#fromObject

INSTANCE METHODS



43
44
45
# File 'lib/action_smser/base.rb', line 43

def from
  @from
end

#re_delivery_of_delivery_report_idObject

INSTANCE METHODS



43
44
45
# File 'lib/action_smser/base.rb', line 43

def re_delivery_of_delivery_report_id
  @re_delivery_of_delivery_report_id
end

#sms_typeObject

INSTANCE METHODS



43
44
45
# File 'lib/action_smser/base.rb', line 43

def sms_type
  @sms_type
end

#toObject

INSTANCE METHODS



43
44
45
# File 'lib/action_smser/base.rb', line 43

def to
  @to
end

#ttlObject

INSTANCE METHODS



43
44
45
# File 'lib/action_smser/base.rb', line 43

def ttl
  @ttl
end

Class Method Details

.message_real_cropped(message, max_length = 159) ⇒ Object

Make sure that double chars are taken account



27
28
29
30
31
32
33
34
35
# File 'lib/action_smser/base.rb', line 27

def message_real_cropped(message, max_length = 159)
  result = ""
  length = 0
  message.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



21
22
23
24
25
# File 'lib/action_smser/base.rb', line 21

def message_real_length(message)
  i = 0
  message.to_s.chars.each do |char| i += SMS_DOUBLE_CHARS.include?(char) ? 2 : 1 end
  i
end

.method_missing(method, *args) ⇒ Object

:nodoc:



9
10
11
12
# File 'lib/action_smser/base.rb', line 9

def method_missing(method, *args) #:nodoc:
  return super unless respond_to?(method)
  new(method, *args)
end

.respond_to?(method, include_private = false) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/action_smser/base.rb', line 14

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//TRANSLIT//IGNORE') ⇒ Object

Most of the gateways want escaped and ISO encoded messages Also make sure that its max 500 chars long



101
102
103
104
# File 'lib/action_smser/base.rb', line 101

def body_encoded_escaped(to = 'ISO-8859-15//TRANSLIT//IGNORE')
  msg = body.first(500)
  CGI.escape(Iconv.iconv(to, 'utf-8', msg).first.to_s)
end

#body_escapedObject



106
107
108
# File 'lib/action_smser/base.rb', line 106

def body_escaped
  CGI.escape(body.to_s.first(500))
end

#deliverObject



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/action_smser/base.rb', line 85

def deliver
  self.send(:before_delivery) if self.respond_to?(:before_delivery)

  return false unless valid?

  logger.info "Sending sms - Delivery_method: #{delivery_options[: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_methodObject



80
81
82
# File 'lib/action_smser/base.rb', line 80

def delivery_method
  ActionSmser::DeliveryMethods.const_get(delivery_options[:delivery_method].to_s.downcase.camelize)
end

#from_encodedObject



128
129
130
# File 'lib/action_smser/base.rb', line 128

def from_encoded
  from.to_s.gsub(/^(\+|0)/, "")
end

#loggerObject



136
137
138
# File 'lib/action_smser/base.rb', line 136

def logger
  ActionSmser::Logger
end

#sms(options) ⇒ Object

Main method for creating sms infos



65
66
67
68
69
70
# File 'lib/action_smser/base.rb', line 65

def sms(options)
  @body = options[:body]
  @to = options[:to]
  @from = options[:from]
  self
end

#to_as_arrayObject



120
121
122
# File 'lib/action_smser/base.rb', line 120

def to_as_array
  @to.is_a?(Array) ? @to : [@to]
end

#to_encodedObject



124
125
126
# File 'lib/action_smser/base.rb', line 124

def to_encoded
  to_numbers_array.join(",")
end

#to_numbers_arrayObject

make sure that to is an array and remove leading ‘+’ or ‘0’ chars



111
112
113
114
115
116
117
118
# File 'lib/action_smser/base.rb', line 111

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)/, "")}
end

#to_sObject



72
73
74
# File 'lib/action_smser/base.rb', line 72

def to_s
  "Sms #{sms_type} - From: #{from.inspect}, To: #{to.inspect}, Body: #{body.inspect}, Valid: #{@valid}"
end

#ttl_to_iObject



132
133
134
# File 'lib/action_smser/base.rb', line 132

def ttl_to_i
  ttl.blank? ? ActionSmser.delivery_options[:default_ttl] : ttl.to_i
end

#valid?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/action_smser/base.rb', line 76

def valid?
  !body.blank? && !from.blank? && !to_numbers_array.collect{|number| number.to_s.blank? ? nil : true}.compact.blank?
end