short_message 0.0.6 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 907b1e83f5941d4897cdf66427065f9698edc33c
4
- data.tar.gz: dd8e79d9686c98d3ad10378639441319bf50205c
3
+ metadata.gz: 8c279adb74034281791d363ed71482435e27abc7
4
+ data.tar.gz: 3fdecf7b086165361a255c65448bf0bee39d7814
5
5
  SHA512:
6
- metadata.gz: 5c46b64b56cbe6fd6e8cb901261ce9fd5e1fef11e4454d70dedab1482b26d941220022ec2987d0a38737b619d07772970845a850fad492d83fb05b25ba900377
7
- data.tar.gz: c469574b1421b4c60539b7f7fb09aa211cf715276fb59e4f9ec5d202e400688c3c4c1af065aebf67975e52614923e7c04a7d8b1401a688d9b6f22a58a6a83d05
6
+ metadata.gz: a28908e48e31fcc3c04681a60a9ef042a475964866f99317478de60bb73070fe5b4f93696dcc2e499b5cf57f05b1abf8074f977f6e82b240cf1b3066e3f38ae7
7
+ data.tar.gz: 5857e907d60d7d9a213c80ffa1088e8c08c59f49a5cd8e66e35f743506e70b501295e1752128029e240b135b707155fd21e0fea52bbe5302cf741dc4556dfbcb
@@ -1,16 +1,21 @@
1
1
  module ShortMessage
2
2
  class Mailer < ActionMailer::Base
3
+ default from: ShortMessage.config.default_mail_sender
4
+
3
5
  def recharge_notification amount
4
- mail to: ShortMessage.config.reload_notification_email, :from => ShortMessage.config.default_mail_sender, :subject => "SMS credit recharged", :body => "Your SMS Account has automatically been recharged with #{amount} sms."
6
+ mail to: ShortMessage.config.reload_notification_email, :subject => "SMS credit recharged", :body => "Your SMS Account has automatically been recharged with #{amount} sms."
5
7
  end
6
-
8
+
7
9
  def voucher_notification amount
8
- mail :to => ShortMessage.config.voucher_notification_email, :from => ShortMessage.config.default_mail_sender, :subject => "SMS credit recharged, please create a voucher", :body => "Please add a voucher:\r\nCustomer: #{ShortMessage.config.user_id}\r\nAmount: #{amount} sms\r\n\r\nThank you!"
10
+ mail :to => ShortMessage.config.voucher_notification_email, :subject => "SMS credit recharged, please create a voucher", :body => "Please add a voucher:\r\nCustomer: #{ShortMessage.config.user_id}\r\nAmount: #{amount} sms\r\n\r\nThank you!"
9
11
  end
10
-
12
+
11
13
  def recharge_failed_notification amount, message
12
- mail :to => ShortMessage.config.reload_notification_email, :from => ShortMessage.config.default_mail_sender, :subject => "SMS credit recharge failed", :body =>"SMS account could not be recharged with #{amount} sms. Error: #{body}"
14
+ mail :to => ShortMessage.config.reload_notification_email, :subject => "SMS credit recharge failed", :body =>"SMS account could not be recharged with #{amount} sms. Error: #{body}"
15
+ end
16
+
17
+ def error_notification message, response
18
+ mail :to => ShortMessage.config.reload_notification_email, :subject => "Error delivering SMS to #{message.recipient}", :body =>"SMS from #{message.sender} to #{message.recipient} could not be sent!\r\n\r\nResponse: #{response.to_yaml}"
13
19
  end
14
-
15
20
  end
16
21
  end
@@ -3,24 +3,28 @@ require "net/http"
3
3
  module ShortMessage
4
4
  class Message < ActiveRecord::Base
5
5
  attr_accessible :message_key, :recipient, :sender, :status_code, :text
6
-
6
+
7
7
  def status_text
8
8
  I18n.t("short_message.status.code_#{self.status_code}")
9
9
  end
10
-
10
+
11
11
  def deliver
12
12
  self.sender = ShortMessage.config.default_sms_sender if self.sender.blank?
13
-
13
+
14
14
  unless self.recipient.blank? and self.text.blank?
15
15
  http = Net::HTTP.new(ShortMessage.config.gateway_server, ShortMessage.config.gateway_port)
16
+ if ShortMessage.config.gateway_port == Net::HTTP.https_default_port()
17
+ http.use_ssl = true
18
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
19
+ end
16
20
  response, data = http.post(ShortMessage.config.send_file_path, build_deliver_params_string)
17
-
21
+
18
22
  if response.code == "200"
19
23
  # returns something like 0:200: OK (<mobilenumber>:<msgid>)
20
24
  result_set = response.body.gsub("(","").gsub(")","").split(":")
21
25
  self.status_code = result_set[0].to_i
22
26
  self.message_key = result_set[3] unless result_set[3].blank?
23
-
27
+
24
28
  if self.status_code == 3 and ShortMessage.config.default_reload_amount > 0
25
29
  if self.recharge
26
30
  self.deliver
@@ -29,24 +33,25 @@ module ShortMessage
29
33
  return false
30
34
  end
31
35
  end
32
-
36
+
33
37
  self.save
34
38
  true
35
39
  else
40
+ Mailer.error_notification(self, response).deliver unless ShortMessage.config.admin_notification_email.blank?
36
41
  false
37
42
  end
38
43
  end
39
44
  end
40
-
45
+
41
46
  def recharge amount = ShortMessage.config.default_reload_amount
42
47
  unless ShortMessage.config.account_functions_path.blank?
43
48
  http = Net::HTTP.new(ShortMessage.config.gateway_server)
44
49
  response, body = http.post(ShortMessage.config.account_functions_path, build_recharge_params_string(amount))
45
-
50
+
46
51
  if response.code == "200"
47
52
  # returns something like 0:Successful
48
53
  result_set = response.body.split(":")
49
-
54
+
50
55
  if result_set[0].to_i == 0
51
56
  logger.info "SMS account successfully charged with #{amount} sms."
52
57
  Mailer.recharge_notification(amount).deliver unless ShortMessage.config.reload_notification_email.blank?
@@ -62,7 +67,7 @@ module ShortMessage
62
67
  end
63
68
  end
64
69
  end
65
-
70
+
66
71
  private
67
72
  def build_deliver_params_string
68
73
  params = []
@@ -75,7 +80,7 @@ module ShortMessage
75
80
  params << "utf-8=1"
76
81
  params.join("&")
77
82
  end
78
-
83
+
79
84
  def build_recharge_params_string amount
80
85
  params = []
81
86
  params << "UserIDFS=#{ShortMessage.config.user_id}"
@@ -1,21 +1,22 @@
1
1
  ShortMessage.configure do |config|
2
2
  config.gateway_server = "www1.q-x.ch"
3
- config.gateway_port = 80
4
-
3
+ config.gateway_port = 443
4
+
5
5
  config.send_file_path = "/app/sms/gw1/cust/send.php"
6
- config.account_functions_path = "/app/sms/gw1/cust/accfunc.php"
7
-
6
+ config.account_functions_path = "/app/sms/gw1/cust/accfunc.php"
7
+
8
8
  # enter your details received from provider here
9
9
  config.user_id = ""
10
10
  config.ccu_id = ""
11
11
  config.id_string = ""
12
-
12
+
13
13
  config.default_reload_amount = 1000
14
-
14
+
15
15
  config.default_mail_sender = "webmaster@your-domain.com"
16
16
  config.reload_notification_email = "webmaster@your-domain.com"
17
+ config.admin_notification_email = "webmaster@your-domain.com" # set to blank to deactivate error mailing
17
18
  config.voucher_notification_email = "give-me-money@your-domain.com" # set nil to disable voucher mailing
18
-
19
+
19
20
  # set a default sms sender (used if no sender is present)
20
21
  # config.default_sms_sender = ""
21
22
  end
@@ -4,35 +4,36 @@ module ShortMessage
4
4
  def self.configure(&block)
5
5
  yield @config ||= ShortMessage::Configuration.new
6
6
  end
7
-
7
+
8
8
  def self.config
9
9
  @config
10
10
  end
11
-
11
+
12
12
  class Configuration #:nodoc:
13
13
  include ActiveSupport::Configurable
14
14
  config_accessor :gateway_server
15
15
  config_accessor :gateway_port
16
16
  config_accessor :send_file_path
17
17
  config_accessor :account_functions_path
18
-
18
+
19
19
  config_accessor :user_id
20
20
  config_accessor :ccu_id
21
21
  config_accessor :id_string
22
-
22
+
23
23
  config_accessor :default_sms_sender
24
24
  config_accessor :default_reload_amount
25
25
  config_accessor :reload_notification_email
26
+ config_accessor :admin_notification_email
26
27
  config_accessor :voucher_notification_email
27
28
  config_accessor :default_mail_sender
28
-
29
+
29
30
  def param_name
30
31
  config.param_name.respond_to?(:call) ? config.param_name.call : config.param_name
31
32
  end
32
-
33
+
33
34
  # define param_name writer (copied from AS::Configurable)
34
35
  writer, line = 'def param_name=(value); config.param_name = value; end', __LINE__
35
36
  singleton_class.class_eval writer, __FILE__, line
36
37
  class_eval writer, __FILE__, line
37
38
  end
38
- end
39
+ end
@@ -1,3 +1,3 @@
1
1
  module ShortMessage
2
- VERSION = "0.0.6"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: short_message
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andi Saurer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-26 00:00:00.000000000 Z
11
+ date: 2014-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -202,7 +202,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
202
202
  version: '0'
203
203
  requirements: []
204
204
  rubyforge_project:
205
- rubygems_version: 2.2.2
205
+ rubygems_version: 2.4.2
206
206
  signing_key:
207
207
  specification_version: 4
208
208
  summary: Send short messages to a specific SMS Gateway