mblox 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -0
- data/README.md +6 -2
- data/lib/mblox/configuration.rb +1 -1
- data/lib/mblox/sms.rb +33 -8
- data/lib/mblox/version.rb +1 -1
- data/spec/configuration_spec.rb +5 -1
- data/spec/sms_spec.rb +30 -7
- metadata +24 -14
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/README.md
CHANGED
@@ -33,17 +33,21 @@ Configuration
|
|
33
33
|
# You can also configure some logging options
|
34
34
|
# In a Rails environment, config.logger will default to Rails.logger and config.log_at will default to :debug
|
35
35
|
# config.log_at means the level at which Mblox will log.
|
36
|
-
#
|
36
|
+
# For instance, if config.log_at == :debug, Mblox will log only if the logger's log level is :debug
|
37
|
+
# Note that if config.log_at == :debug and your logger's log level is :info,
|
38
|
+
# logging will be suppressed because it is below the log level of the logger.
|
37
39
|
config.logger = Logger.new(STDOUT)
|
38
40
|
config.log_at :info
|
39
41
|
|
40
42
|
# What to do if messages are longer than 160 characters. Default is :raise_error
|
43
|
+
# Other options are :truncate and :split
|
41
44
|
config.on_message_too_long = :truncate
|
42
45
|
end
|
43
46
|
|
44
47
|
Once your application is configured, send messages:
|
45
48
|
|
46
|
-
|
49
|
+
# The number to sending to must be a 10-digit number, including the area code. Can be a String or Fixnum.
|
50
|
+
phone_number = 2225555555 # or: phone_number = "2225555555"
|
47
51
|
Mblox::Sms.new(phone_number, "your message").send
|
48
52
|
|
49
53
|
## Testing
|
data/lib/mblox/configuration.rb
CHANGED
@@ -19,7 +19,7 @@ module Mblox
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def on_message_too_long= action
|
22
|
-
raise ArgumentError, "Mblox.config.on_message_too_long must be either :truncate or :raise_error" unless [:truncate, :raise_error].include?(action)
|
22
|
+
raise ArgumentError, "Mblox.config.on_message_too_long must be either :truncate, :split or :raise_error" unless [:truncate, :raise_error, :split].include?(action)
|
23
23
|
@on_message_too_long = action
|
24
24
|
end
|
25
25
|
|
data/lib/mblox/sms.rb
CHANGED
@@ -5,22 +5,29 @@ require "net/https"
|
|
5
5
|
|
6
6
|
module Mblox
|
7
7
|
class Sms
|
8
|
+
MAX_LENGTH = 160
|
9
|
+
MAX_SECTION_LENGTH = MAX_LENGTH - "(MSG XXX/XXX): ".size
|
10
|
+
|
8
11
|
attr_reader :phone, :message
|
12
|
+
|
13
|
+
ON_MESSAGE_TOO_LONG_HANDLER = {
|
14
|
+
:raise_error => Proc.new { raise SmsError, "Message cannot be longer than #{MAX_LENGTH} characters" },
|
15
|
+
:truncate => Proc.new { |message| Mblox.log "Truncating message due to length. Message was: \"#{message}\" but will now be \"#{message = message[0,MAX_LENGTH]}\""; [message] },
|
16
|
+
:split => Proc.new { |message| split_message(message) }
|
17
|
+
}
|
18
|
+
|
9
19
|
def initialize(phone,message)
|
10
20
|
phone = phone.to_s
|
11
21
|
raise SmsError, "Phone number must be ten digits" unless /\A[0-9]{10}\z/.match(phone)
|
12
22
|
raise SmsError, "Phone number cannot begin with 0 or 1" if ['0','1'].include?(phone[0].to_s)
|
13
23
|
raise SmsError, "Message cannot be blank" if message.empty?
|
14
|
-
if message.size >
|
15
|
-
|
16
|
-
message = message[0,160] if :truncate == Mblox.config.on_message_too_long
|
17
|
-
end
|
24
|
+
Mblox.log "WARNING: Some characters may be lost because the message must be broken into at least 1000 sections" if message.size > (999 * MAX_SECTION_LENGTH)
|
25
|
+
@message = (message.size > MAX_LENGTH) ? ON_MESSAGE_TOO_LONG_HANDLER[Mblox.config.on_message_too_long].call(message) : [message.dup]
|
18
26
|
@phone = "1#{phone}"
|
19
|
-
@message = message.dup
|
20
27
|
end
|
21
28
|
|
22
29
|
def send
|
23
|
-
commit build
|
30
|
+
@message.collect { |message| commit build(message) }
|
24
31
|
end
|
25
32
|
private
|
26
33
|
def commit(request_body)
|
@@ -43,7 +50,7 @@ module Mblox
|
|
43
50
|
"RequestResult: \"#{result_header['RequestResultCode']}:#{result_header['RequestResultText']}\" / SubscriberResult: \"#{subscriber_result['SubscriberResultCode']}:#{subscriber_result['SubscriberResultText']}\""
|
44
51
|
end
|
45
52
|
|
46
|
-
def build
|
53
|
+
def build(message)
|
47
54
|
builder = Builder::XmlMarkup.new
|
48
55
|
builder.instruct!(:xml, :encoding => "ISO-8859-1")
|
49
56
|
builder.NotificationRequest(:Version => "3.5") do |nr|
|
@@ -53,7 +60,7 @@ module Mblox
|
|
53
60
|
end
|
54
61
|
nr.NotificationList(:BatchID => "1") do |nl|
|
55
62
|
nl.Notification(:SequenceNumber => "1", :MessageType => "SMS") do |n|
|
56
|
-
n.Message(
|
63
|
+
n.Message(message)
|
57
64
|
n.Profile(Mblox.config.profile_id)
|
58
65
|
n.SenderID(Mblox.config.sender_id, :Type => 'Shortcode')
|
59
66
|
n.Tariff(Mblox.config.tariff)
|
@@ -65,5 +72,23 @@ module Mblox
|
|
65
72
|
end
|
66
73
|
end
|
67
74
|
end
|
75
|
+
|
76
|
+
def self.section_counter(size)
|
77
|
+
size / MAX_SECTION_LENGTH + ((size % MAX_SECTION_LENGTH).zero? ? 0 : 1)
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.split_message(message)
|
81
|
+
sections = section_counter(message.size)
|
82
|
+
Mblox.log "Splitting message into #{sections} messages due to length."
|
83
|
+
split_message = []
|
84
|
+
(sections - 1).times do |i|
|
85
|
+
first_char = i * MAX_SECTION_LENGTH
|
86
|
+
Mblox.log "Section ##{i + 1} of ##{sections} contains characters #{first_char + 1} thru #{first_char + MAX_SECTION_LENGTH} of #{message.size}"
|
87
|
+
split_message << "(MSG #{i+1}/#{sections}): #{message[first_char, MAX_SECTION_LENGTH]}"
|
88
|
+
end
|
89
|
+
first_char = (sections-1)*MAX_SECTION_LENGTH
|
90
|
+
Mblox.log "Section ##{sections} of ##{sections} contains characters #{first_char + 1} thru #{message.size} of #{message.size}"
|
91
|
+
split_message << "(MSG #{sections}/#{sections}): #{message[first_char..-1]}"
|
92
|
+
end
|
68
93
|
end
|
69
94
|
end
|
data/lib/mblox/version.rb
CHANGED
data/spec/configuration_spec.rb
CHANGED
@@ -11,12 +11,16 @@ describe "configuration" do
|
|
11
11
|
expect { Mblox.config.on_message_too_long = :truncate }.to_not raise_error
|
12
12
|
end
|
13
13
|
|
14
|
+
it "should allow the value :split" do
|
15
|
+
expect { Mblox.config.on_message_too_long = :split }.to_not raise_error
|
16
|
+
end
|
17
|
+
|
14
18
|
it "should allow the value :raise_error" do
|
15
19
|
expect { Mblox.config.on_message_too_long = :raise_error }.to_not raise_error
|
16
20
|
end
|
17
21
|
|
18
22
|
it "should not allow other values and should remain in a valid state" do
|
19
|
-
expect { Mblox.config.on_message_too_long = :do_nothing }.to raise_error(ArgumentError, "Mblox.config.on_message_too_long must be either :truncate or :raise_error")
|
23
|
+
expect { Mblox.config.on_message_too_long = :do_nothing }.to raise_error(ArgumentError, "Mblox.config.on_message_too_long must be either :truncate, :split or :raise_error")
|
20
24
|
expect(Mblox.config.on_message_too_long).to eq(:raise_error)
|
21
25
|
end
|
22
26
|
end
|
data/spec/sms_spec.rb
CHANGED
@@ -33,7 +33,7 @@ describe "message" do
|
|
33
33
|
message = "A"+"ABCDEFGHIJ"*16
|
34
34
|
Mblox.config.on_message_too_long = :truncate
|
35
35
|
expect { @mblox = Mblox::Sms.new("2"*10, message) }.to_not raise_error
|
36
|
-
expect(@mblox.message).to eq(message[0,160])
|
36
|
+
expect(@mblox.message).to eq([message[0,160]])
|
37
37
|
end
|
38
38
|
|
39
39
|
it "cannot be longer than 160 characters if configured to raise error" do
|
@@ -41,28 +41,51 @@ describe "message" do
|
|
41
41
|
expect { Mblox::Sms.new("2"*10, "A"*161) }.to raise_error(Mblox::SmsError, "Message cannot be longer than 160 characters")
|
42
42
|
end
|
43
43
|
|
44
|
-
it "should be
|
44
|
+
it "should be split into multiple messages when longer than 160 characters if configured to split and even split" do
|
45
|
+
message = "ABCDEFGHIJ"*58
|
46
|
+
Mblox.config.on_message_too_long = :split
|
47
|
+
expect { @mblox = Mblox::Sms.new("2"*10, message) }.to_not raise_error
|
48
|
+
expect(@mblox.message).to eq(["(MSG 1/4): #{message[0,145]}", "(MSG 2/4): #{message[145,145]}", "(MSG 3/4): #{message[290,145]}", "(MSG 4/4): #{message[435,145]}"])
|
49
|
+
expect(@mblox.send).to eq(Array.new(4, result_unroutable))
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should be split into multiple messages when longer than 160 characters if configured to split and not even split" do
|
53
|
+
message = "ABCDEFGHIJ"*32
|
54
|
+
Mblox.config.on_message_too_long = :split
|
55
|
+
expect { @mblox = Mblox::Sms.new("2"*10, message) }.to_not raise_error
|
56
|
+
expect(@mblox.message).to eq(["(MSG 1/3): #{message[0,145]}", "(MSG 2/3): #{message[145,145]}", "(MSG 3/3): #{message[290..-1]}"])
|
57
|
+
expect(@mblox.send).to eq(Array.new(3, result_unroutable))
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should be safe from changing when short" do
|
45
61
|
msg = the_message
|
46
62
|
mblox = Mblox::Sms.new(TEST_NUMBER,msg)
|
47
63
|
msg[1..3] = ''
|
48
|
-
expect(mblox.message).to eq(the_message)
|
64
|
+
expect(mblox.message).to eq([the_message])
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should be safe from changing when long" do
|
68
|
+
msg = the_message * 10
|
69
|
+
mblox = Mblox::Sms.new(TEST_NUMBER,msg)
|
70
|
+
msg[1..3] = ''
|
71
|
+
expect(mblox.message[0][11, 20]).to eq(the_message[0,20])
|
49
72
|
end
|
50
73
|
end
|
51
74
|
|
52
75
|
describe "SMS messages" do
|
53
76
|
it "should be sent when the phone number is a Fixnum" do
|
54
|
-
expect(Mblox::Sms.new(TEST_NUMBER.to_i,the_message).send).to eq(result_ok)
|
77
|
+
expect(Mblox::Sms.new(TEST_NUMBER.to_i,the_message).send).to eq([result_ok])
|
55
78
|
end
|
56
79
|
|
57
80
|
it "should be sent when the phone number is a String" do
|
58
|
-
expect(Mblox::Sms.new(TEST_NUMBER.to_s,the_message).send).to eq(result_ok)
|
81
|
+
expect(Mblox::Sms.new(TEST_NUMBER.to_s,the_message).send).to eq([result_ok])
|
59
82
|
end
|
60
83
|
|
61
84
|
it "should allow 160-character messages" do
|
62
|
-
expect(Mblox::Sms.new(TEST_NUMBER,"A"*160).send).to eq(result_ok)
|
85
|
+
expect(Mblox::Sms.new(TEST_NUMBER,"A"*160).send).to eq([result_ok])
|
63
86
|
end
|
64
87
|
|
65
88
|
it "should fail when sent to a landline" do
|
66
|
-
expect(Mblox::Sms.new("6176354500",the_message).send).to eq(result_unroutable)
|
89
|
+
expect(Mblox::Sms.new("6176354500",the_message).send).to eq([result_unroutable])
|
67
90
|
end
|
68
91
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mblox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-08-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -115,6 +115,7 @@ extensions: []
|
|
115
115
|
extra_rdoc_files: []
|
116
116
|
files:
|
117
117
|
- .gitignore
|
118
|
+
- .rspec
|
118
119
|
- Gemfile
|
119
120
|
- LICENSE.txt
|
120
121
|
- README.md
|
@@ -143,12 +144,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
143
144
|
- - ! '>='
|
144
145
|
- !ruby/object:Gem::Version
|
145
146
|
version: '0'
|
147
|
+
segments:
|
148
|
+
- 0
|
149
|
+
hash: 965711471
|
146
150
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
147
151
|
none: false
|
148
152
|
requirements:
|
149
153
|
- - ! '>='
|
150
154
|
- !ruby/object:Gem::Version
|
151
155
|
version: '0'
|
156
|
+
segments:
|
157
|
+
- 0
|
158
|
+
hash: 965711471
|
152
159
|
requirements: []
|
153
160
|
rubyforge_project:
|
154
161
|
rubygems_version: 1.8.24
|
@@ -162,18 +169,21 @@ summary: ! '# Mblox This gem is for subscribers to Mblox to send SMS messages.
|
|
162
169
|
= ... config.partner_name = ... config.tariff = ... config.service_id = ... # You
|
163
170
|
can also configure some logging options # In a Rails environment, config.logger
|
164
171
|
will default to Rails.logger and config.log_at will default to :debug # config.log_at
|
165
|
-
means the level at which Mblox will log. #
|
166
|
-
log
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
172
|
+
means the level at which Mblox will log. # For instance, if config.log_at == :debug,
|
173
|
+
Mblox will log only if the logger''s log level is :debug # Note that if config.log_at
|
174
|
+
== :debug and your logger''s log level is :info, # logging will be suppressed
|
175
|
+
because it is below the log level of the logger. config.logger = Logger.new(STDOUT)
|
176
|
+
config.log_at :info # What to do if messages are longer than 160 characters. Default
|
177
|
+
is :raise_error # Other options are :truncate and :split config.on_message_too_long
|
178
|
+
= :truncate end Once your application is configured, send messages: # The number
|
179
|
+
to sending to must be a 10-digit number, including the area code. Can be a String
|
180
|
+
or Fixnum. phone_number = 2225555555 # or: phone_number = "2225555555" Mblox::Sms.new(phone_number,
|
181
|
+
"your message").send ## Testing Copy config.yml.example to config.yml and set
|
182
|
+
all the values in that file. Run: rspec You should recieve 3 SMS messages to
|
183
|
+
your phone within several seconds. ## Contributing 1. Fork it 2. Create your feature
|
184
|
+
branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am
|
185
|
+
''Add some feature''`) 4. Push to the branch (`git push origin my-new-feature`)
|
186
|
+
5. Create new Pull Request'
|
177
187
|
test_files:
|
178
188
|
- spec/configuration_spec.rb
|
179
189
|
- spec/log_spec.rb
|