smess 1.0.3 → 1.0.4

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: 2c74dd29d5a45e1ac70089ea67c1f29148706bb4
4
- data.tar.gz: fd3a185d53f641db6226a9a4a80dd2de5e8f57c3
3
+ metadata.gz: 7bd97cb82c4e104f4878fd93026390b7f64e557d
4
+ data.tar.gz: fb1c0c5c63933f965109182d2f6525359939b7e8
5
5
  SHA512:
6
- metadata.gz: 818eee9d6e7328c6fee1bf647bbf30342dddf9d83dbdd97a6292e0cc70dabad8bef04b94105bd3042aaf0fe0f1076709db516ab2bfb0af8644ca71fe7386c59a
7
- data.tar.gz: 5bd2f48c54d878ccdb589be344d756ea49024d296eca34bd3da11d90e22e46648b3470ebf41b2845615295257a2ebf161e208dd978d4cde8407d4892e8a9f843
6
+ metadata.gz: a02d6af367b2b63838c692cb1e4bf9fc5d012d286de836e07d8c42b8f7d926f0aa8878f74204baaeef48a5149f5b5b586d289e01acfc269a57b69d548fc2f917
7
+ data.tar.gz: 2a6bd99ef07c08ece8c04beea6ea65e0a820d422b6377e76f948ce22c76f9232fb34ef133eceead3ee62c11b5ac1286e0108fe75af9e8eb7fdd322830c5884b9
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- smess (1.0.2)
4
+ smess (1.0.3)
5
5
  activesupport (>= 3.0)
6
6
  clickatell
7
7
  httpi
@@ -41,7 +41,7 @@ GEM
41
41
  mime-types (1.25)
42
42
  mini_portile (0.5.1)
43
43
  minitest (4.7.5)
44
- multi_json (1.7.9)
44
+ multi_json (1.8.0)
45
45
  nokogiri (1.6.0)
46
46
  mini_portile (~> 0.5.0)
47
47
  nori (1.1.5)
@@ -4,70 +4,88 @@ module Smess
4
4
  class Twilio
5
5
  include Smess::Logging
6
6
 
7
- attr_reader :sms
8
-
9
7
  def initialize(sms)
10
8
  @sms = sms
9
+ @results = []
11
10
  end
12
11
 
13
12
  def deliver
14
- parts = Smess.separate_sms sms.message.strip_nongsm_chars
15
- return false if parts[0].empty?
13
+ parts.each do |part|
14
+ results << send_one_sms(part)
15
+ end
16
16
 
17
- @client = ::Twilio::REST::Client.new(ENV["SMESS_TWILIO_SID"], ENV["SMESS_TWILIO_AUTH_TOKEN"])
17
+ # we don't actually return the status for any of additional messages which is cheating
18
+ results.first
19
+ end
18
20
 
21
+ private
19
22
 
20
- results = []
21
- while parts.length > 0
22
- results << send_one_sms(sms, parts.shift)
23
- end
24
- results[0][:data][:text] = sms.message.strip_nongsm_chars
25
- results[0]
23
+ attr_reader :sms
24
+ attr_accessor :results
25
+
26
+ def from
27
+ ENV["SMESS_TWILIO_FROM"]
28
+ end
29
+
30
+ def parts
31
+ @parts ||= split_parts
26
32
  end
27
33
 
28
- def send_one_sms(sms, message)
34
+ def split_parts
35
+ Smess.split_sms(sms.message.strip_nongsm_chars).reject {|s| s.empty? }
36
+ end
37
+
38
+ def client
39
+ @client ||= ::Twilio::REST::Client.new(ENV["SMESS_TWILIO_SID"], ENV["SMESS_TWILIO_AUTH_TOKEN"])
40
+ end
41
+
42
+ def send_one_sms(message)
29
43
  begin
30
- response = @client.account.sms.messages.create({
31
- from: ENV["SMESS_TWILIO_FROM"],
44
+ response = client.account.sms.messages.create({
45
+ from: from,
32
46
  to: "+#{sms.to}",
33
47
  body: message,
34
48
  status_callback: ENV["SMESS_TWILIO_CALLBACK_URL"]
35
49
  })
50
+ result = normal_result(response)
36
51
  rescue Exception => e
37
- logger.warn response
38
- # connection problem or some error
39
- result = {
40
- response_code: '-1',
41
- response: {
42
- temporaryError: 'true',
43
- responseCode: e.code,
44
- responseText: e.message
45
- },
46
- data: {
47
- to: sms.to,
48
- text: message,
49
- from: ENV["SMESS_TWILIO_FROM"]
50
- }
51
- }
52
- else
53
- response_code = response.status
54
- response_code = "0" unless response.status == "failed"
55
- # Successful response
56
- result = {
57
- message_id: response.sid,
58
- response_code: response_code.to_s,
59
- response: MultiJson.load(@client.last_response.body),
60
- destination_address: sms.to,
61
- data: {
62
- to: sms.to,
63
- text: message,
64
- from: ENV["SMESS_TWILIO_FROM"]
65
- }
66
- }
52
+ result = result_for_error(e)
67
53
  end
68
54
  result
69
55
  end
70
56
 
57
+ def normal_result(response)
58
+ response_code = response.status
59
+ response_code = "0" unless response.status == "failed"
60
+ # Successful response
61
+ {
62
+ message_id: response.sid,
63
+ response_code: response_code.to_s,
64
+ response: MultiJson.load(client.last_response.body),
65
+ destination_address: sms.to,
66
+ data: result_data
67
+ }
68
+ end
69
+
70
+ def result_for_error(e)
71
+ {
72
+ response_code: '-1',
73
+ response: {
74
+ temporaryError: 'true',
75
+ responseCode: '-1',
76
+ responseText: e.message
77
+ },
78
+ data: result_data
79
+ }
80
+ end
81
+
82
+ def result_data
83
+ {
84
+ to: sms.to,
85
+ text: sms.message.strip_nongsm_chars,
86
+ from: from
87
+ }
88
+ end
71
89
 
72
90
  end
73
91
  end
data/lib/smess/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Smess
2
- VERSION = '1.0.3'
2
+ VERSION = '1.0.4'
3
3
  end
data/lib/string.rb CHANGED
@@ -88,36 +88,46 @@ class String
88
88
  def msisdn(*args)
89
89
  ret = self.dup
90
90
  ret.msisdn!(*args)
91
- ret.empty? ? nil : ret
92
91
  end
93
92
  def msisdn!(country_code = 1, force_country_code = false)
94
- self.replace("") and return self if /\{[a-z]+:.+\}/.match(self) # custom crap
95
-
96
- # make num all digits
97
93
  self.gsub!(/\D/,"")
98
94
 
99
- if length > 0 && self[0..1] == "00"
95
+ if begins_with_msisdn_international_prefix
100
96
  self.replace(self[2..-1])
101
- elsif length > 0 && self[0] == "0"
102
- # cut away leading zeros
97
+ elsif begins_with_msisdn_national_prefix
103
98
  self.replace(self[1..-1])
104
- # Add country code unless the start of the number is the correct country code
105
- unless self[0..country_code.to_s.length-1] == country_code.to_s
106
- self.prepend(country_code.to_s)
107
- end
99
+ ensure_msisdn_countrycode_prefix(country_code)
108
100
  elsif force_country_code
109
- # Add country code unless the start of the number is the correct country code
110
- unless self[0..country_code.to_s.length-1] == country_code.to_s
111
- self.prepend(country_code.to_s)
112
- end
101
+ ensure_msisdn_countrycode_prefix(country_code)
113
102
  end
114
103
 
115
- # number must be in a valid range
116
- unless (10..15) === self.length
117
- self.replace("") and return self
104
+ validate_msisdn_length_range
105
+ self
106
+ end
107
+
108
+ private
109
+
110
+ def begins_with_msisdn_international_prefix
111
+ length > 0 && self[0..1] == "00"
112
+ end
113
+
114
+ # correct most places in the world but notably not in north america (1... countries)
115
+ def begins_with_msisdn_national_prefix
116
+ length > 0 && self[0] == "0"
117
+ end
118
+
119
+ # Add country code unless the start of the number is the correct country code
120
+ def ensure_msisdn_countrycode_prefix(country_code)
121
+ unless self[0..country_code.to_s.length-1] == country_code.to_s
122
+ self.prepend(country_code.to_s)
118
123
  end
124
+ end
119
125
 
120
- self
126
+ def validate_msisdn_length_range
127
+ unless (10..15) === self.length
128
+ self.replace("")
129
+ end
121
130
  end
122
131
 
132
+
123
133
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smess
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Westin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-10 00:00:00.000000000 Z
11
+ date: 2013-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec