mblox 0.2.2 → 0.2.5

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OWEwYTcwMzU3ODdiNGNhOWQ3MmUxODcwMDMxMjU0ZjNlM2FlNTA1MA==
4
+ NjNlOGM0NDk1MjU5MzEzZWVkMTAwYmM2ZjhiZDlmOTk4Y2I3ZGQyZA==
5
5
  data.tar.gz: !binary |-
6
- YzhjMDQ3MWI2Zjk0YTJjMjg2MGQ2ZWUyMGU4MDlkZTY2ODg4YzI1Yg==
6
+ ZjA3YzY2MGM5ZjhiNGY1NjNmNDVmODliNDIwNmZhNzI3ZjRhYTE0Zg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MDU5YTVhMmNkOGY4ZWFjMTZjMTQxNTdjNzNjNDgxNWFkNTM2ZGMyZjI1ODNl
10
- ZWMwMzJmZmNiNzY2NmFmZTIyZmQxNzMxMDY1Mzg2NDFlODBmYTNmYjg5YzRk
11
- NDNlNmU4ZjEzZjcwNWU1MDdiZGIxZmM1YmRmZDNiYWQ2OTFkNGI=
9
+ OGNiMDI2ZTExZWM0MzFiN2VlMzQ1YWQ1MzQwZDlmZWIxMmNmNDg4YWIxNGJm
10
+ MWNlZGNiMDc0OTJjYjZjMDQyNjU2M2NhYzQ4NjA4YWI0MDVmNGE0MzRjZjRl
11
+ ODAxOTVhMTE0ODQzZmQ5YmY2NGJjNTFmYTcwNzBjOTc4YjYxOWE=
12
12
  data.tar.gz: !binary |-
13
- OGEwZDk5NDQ0ODk2MjI5M2UxZDA0MGJjZjg2YzE1MDE1MDAzMTU5NmQ0ODU0
14
- MTJmMmQ1NmY2NGIwMzA2NDAyOWVlMzVjNWVlNzczNTkxZjVmYmQwOWVkOWEz
15
- NjBlMjQwNWM1MGRhZjI1M2ZiNmExZWYzOGIzNWRjNmViOTVkNzc=
13
+ NDA1ZTAwNGE1NDNjNmNjZmExYmI0NWVkOWY2NGE2MThlMjM3NjU0MjJhNGMz
14
+ YjkyZmI1ZjBiZGRiMzhhYmVlYmJmMjMzYTg3ODQ1YmJlOTkxYWM4YjU5YzYy
15
+ N2IzYTlmNjJiYTFlNmFkN2E1ODk3YjFmMmRmYmE4MTJhMDE2Yjk=
data/lib/mblox.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "mblox/configuration"
2
2
  require "mblox/sms"
3
+ require "mblox/sms_receipt"
3
4
  require "mblox/version"
4
5
 
5
6
  module Mblox
@@ -9,4 +10,5 @@ module Mblox
9
10
  self.config.logger.__send__(self.config.log_level, *args) if self.config.logger
10
11
  end
11
12
  end
13
+ class MissingExpectedXmlContentError < StandardError; end
12
14
  end
data/lib/mblox/sms.rb CHANGED
@@ -12,7 +12,7 @@ module Mblox
12
12
  class BatchIdOutOfRangeError < ::ArgumentError; end
13
13
  MAX_LENGTH = 160
14
14
  MAX_SECTION_LENGTH = MAX_LENGTH - "(MSG XXX/XXX): ".size
15
- ILLEGAL_CHARACTERS = /([^a-zA-Z0-9!"#$\%&'\(\)*+,-.\/:;<=>?@_£¤¥§¿i¡ÄÅÆÇÉÑÖØÜßáäåæèéìñòöøùü\n\r\tí ])/
15
+ ILLEGAL_CHARACTERS = /([^a-zA-Z0-9!"#\$\%&'\(\)*+,-.\/:;<=>?@_£¤¥§¿i¡ÄÅÆÇÉÑÖØÜßáäåæèéìñòöøùü\n\r\tí ])/
16
16
 
17
17
  attr_reader :phone, :message
18
18
 
@@ -32,7 +32,7 @@ module Mblox
32
32
  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)
33
33
  @message = (message.size > MAX_LENGTH) ? ON_MESSAGE_TOO_LONG_HANDLER[Mblox.config.on_message_too_long].call(message) : [message.dup]
34
34
  @phone = "1#{phone}"
35
- raise BatchIdOutOfRangeError, "batch_id must be in the range 1 to #{MAX_BATCH_ID}. The batch_id specified (#{batch_id}) is out of range." if batch_id && (MAX_BATCH_ID < batch_id.to_i)
35
+ raise BatchIdOutOfRangeError, "batch_id must be in the range 1 to #{MAX_BATCH_ID}. The batch_id specified (#{batch_id}) is out of range." if !batch_id.blank? && (MAX_BATCH_ID < batch_id.to_i)
36
36
  @batch_id = batch_id.to_i unless batch_id.blank?
37
37
  end
38
38
 
@@ -0,0 +1,39 @@
1
+ require 'active_model/serialization'
2
+ require 'active_model/serializers/xml.rb'
3
+ module Mblox
4
+ class SmsReceipt
5
+ attr_reader :batch_id, :subscriber_number, :timestamp, :msg_reference, :status, :reason
6
+ def initialize(xml)
7
+ data = Hash.from_xml(xml)['NotificationService']
8
+ raise MissingExpectedXmlContentError, "Xml should have contained a 'NotificationService' node, but was #{xml}" if data.blank?
9
+
10
+ data = data['NotificationList']
11
+ raise MissingExpectedXmlContentError, "Xml should have contained a 'NotificationService' -> 'NotificationList' node, but was #{xml}" if data.blank?
12
+
13
+ data = data['Notification']
14
+ raise MissingExpectedXmlContentError, "Xml should have contained a 'NotificationService' -> 'NotificationList' -> 'Notification' node, but was #{xml}" if data.blank?
15
+
16
+ @batch_id = data['BatchID'].to_i
17
+
18
+ data = data['Subscriber']
19
+ raise MissingExpectedXmlContentError, "Xml should have contained a 'NotificationService' -> 'NotificationList' -> 'Notification' -> 'Subscriber' node, but was #{xml}" if data.blank?
20
+
21
+ @subscriber_number = data['SubscriberNumber']
22
+ @subscriber_number = @subscriber_number[1..-1] if '1' == @subscriber_number[0]
23
+
24
+ unless data['TimeStamp'].blank?
25
+ @timestamp = begin
26
+ Time.strptime("#{data['TimeStamp']}+0000", '%Y%m%d%H%M%z')
27
+ rescue ArgumentError
28
+ nil
29
+ end
30
+ end
31
+
32
+ @timestamp = @timestamp.to_datetime if @timestamp
33
+
34
+ @msg_reference = data['MsgReference']
35
+ @status = data['Status']
36
+ @reason = data['Reason'].blank? ? nil : data['Reason'].to_i
37
+ end
38
+ end
39
+ end
@@ -10,7 +10,6 @@ require 'active_model/errors'
10
10
 
11
11
  module Mblox
12
12
  class SmsResponse
13
- class MissingExpectedXmlContentError < StandardError; end
14
13
  class Result
15
14
  include ActiveModel::Validations
16
15
  validates_presence_of :text, :code, :message => "%{attribute} cannot be blank"
@@ -46,13 +45,13 @@ module Mblox
46
45
  result_list = data['NotificationResultList']
47
46
  raise MissingExpectedXmlContentError, "Xml should have contained a 'NotificationRequestResult' -> 'NotificationResultList' node, but was #{xml}" if result_list.blank?
48
47
  result_list = result_list['NotificationResult']
49
- raise MissingExpectedXmlContentError, "Xml should have contained a 'NotificationRequestResult' -> 'NotificationResultList' => 'NotificationResult' node, but was #{xml}" if result_list.blank?
48
+ raise MissingExpectedXmlContentError, "Xml should have contained a 'NotificationRequestResult' -> 'NotificationResultList' -> 'NotificationResult' node, but was #{xml}" if result_list.blank?
50
49
  @result = Result.new(result_list['NotificationResultCode'], result_list['NotificationResultText'])
51
50
  @result = nil unless @result.valid?
52
51
 
53
52
  if @result.ok?
54
53
  result_list = result_list['SubscriberResult']
55
- raise MissingExpectedXmlContentError, "Xml should have contained a 'NotificationRequestResult' -> 'NotificationResultList' => 'NotificationResult' -> 'SubscriberResult' node, but was #{xml}" if result_list.blank?
54
+ raise MissingExpectedXmlContentError, "Xml should have contained a 'NotificationRequestResult' -> 'NotificationResultList' -> 'NotificationResult' -> 'SubscriberResult' node, but was #{xml}" if result_list.blank?
56
55
  @subscriber_result = Result.new(result_list['SubscriberResultCode'], result_list['SubscriberResultText'])
57
56
  @subscriber_result = nil unless @subscriber_result.valid?
58
57
  end
data/lib/mblox/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Mblox
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.5"
3
3
  end
@@ -0,0 +1,146 @@
1
+ require "spec_helper"
2
+
3
+ describe Mblox::SmsReceipt do
4
+ def msg_reference
5
+ 'VIShbJoUqEcRLNDWosxwYOLP'
6
+ end
7
+
8
+ def batch_id
9
+ 1234
10
+ end
11
+
12
+ def subscriber_number
13
+ '8885554444'
14
+ end
15
+
16
+ def status
17
+ 'acked'
18
+ end
19
+
20
+ def reason
21
+ 3
22
+ end
23
+
24
+ def valid
25
+ xml = Builder::XmlMarkup.new
26
+ xml.instruct!(:xml, :version => 1.0, :encoding => "ISO-8859-1", :standalone => :yes)
27
+ xml.NotificationService(:Version => "2.3") do |ns|
28
+ ns.NotificationList do |nl|
29
+ nl.Notification(:BatchID => batch_id) do |n|
30
+ n.Subscriber do |s|
31
+ s.SubscriberNumber("1#{subscriber_number}")
32
+ s.TimeStamp(201310071736)
33
+ s.MsgReference(msg_reference)
34
+ s.Status(status)
35
+ s.Reason(reason)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ xml.target!
41
+ end
42
+
43
+ def missing_notification_service
44
+ xml = Builder::XmlMarkup.new
45
+ xml.instruct!(:xml, :version => 1.0, :encoding => "ISO-8859-1", :standalone => :yes)
46
+ xml.WrongNode(:Version => "2.3") do |ns|
47
+ end
48
+ xml.target!
49
+ end
50
+
51
+ def missing_notification_list
52
+ xml = Builder::XmlMarkup.new
53
+ xml.instruct!(:xml, :version => 1.0, :encoding => "ISO-8859-1", :standalone => :yes)
54
+ xml.NotificationService(:Version => "2.3") do |ns|
55
+ end
56
+ xml.target!
57
+ end
58
+
59
+ def missing_notification
60
+ xml = Builder::XmlMarkup.new
61
+ xml.instruct!(:xml, :version => 1.0, :encoding => "ISO-8859-1", :standalone => :yes)
62
+ xml.NotificationService(:Version => "2.3") do |ns|
63
+ ns.NotificationList do |nl|
64
+ nl.WrongNode do |n|
65
+ end
66
+ end
67
+ end
68
+ xml.target!
69
+ end
70
+
71
+ def missing_subscriber
72
+ xml = Builder::XmlMarkup.new
73
+ xml.instruct!(:xml, :version => 1.0, :encoding => "ISO-8859-1", :standalone => :yes)
74
+ xml.NotificationService(:Version => "2.3") do |ns|
75
+ ns.NotificationList do |nl|
76
+ nl.Notification(:BatchID => batch_id) do |n|
77
+ end
78
+ end
79
+ end
80
+ xml.target!
81
+ end
82
+
83
+ def unexpected_nested_values
84
+ xml = Builder::XmlMarkup.new
85
+ xml.instruct!(:xml, :version => 1.0, :encoding => "ISO-8859-1", :standalone => :yes)
86
+ xml.NotificationService(:Version => "2.3") do |ns|
87
+ ns.NotificationList do |nl|
88
+ nl.Notification(:BatchID => batch_id) do |n|
89
+ n.Subscriber do |s|
90
+ s.SubscriberNumber("2#{subscriber_number}")
91
+ s.TimeStamp('Abcdefg')
92
+ s.Reason
93
+ end
94
+ end
95
+ end
96
+ end
97
+ xml.target!
98
+ end
99
+
100
+ it "should access attributes for valid data" do
101
+ target = described_class.new(valid)
102
+ target.batch_id.should == batch_id
103
+ target.subscriber_number.should == subscriber_number
104
+ target.timestamp.should == DateTime.new(2013,10,7,17,36)
105
+ target.msg_reference.should == msg_reference
106
+ target.status.should == status
107
+ target.reason.should == reason
108
+ end
109
+
110
+ it "should raise error when missing notification service node" do
111
+ expect{described_class.new(missing_notification_service)}.to raise_error(Mblox::MissingExpectedXmlContentError, "Xml should have contained a 'NotificationService' node, but was #{missing_notification_service}")
112
+ end
113
+
114
+ it "should raise error when missing notification list node" do
115
+ expect{described_class.new(missing_notification_list)}.to raise_error(Mblox::MissingExpectedXmlContentError, "Xml should have contained a 'NotificationService' -> 'NotificationList' node, but was #{missing_notification_list}")
116
+ end
117
+
118
+ it "should raise error when missing notification node" do
119
+ expect{described_class.new(missing_notification)}.to raise_error(Mblox::MissingExpectedXmlContentError, "Xml should have contained a 'NotificationService' -> 'NotificationList' -> 'Notification' node, but was #{missing_notification}")
120
+ end
121
+
122
+ it "should raise error when missing subscriber node" do
123
+ expect{described_class.new(missing_subscriber)}.to raise_error(Mblox::MissingExpectedXmlContentError, "Xml should have contained a 'NotificationService' -> 'NotificationList' -> 'Notification' -> 'Subscriber' node, but was #{missing_subscriber}")
124
+ end
125
+
126
+ describe "subscriber_number" do
127
+ it "should not drop leading character unless it is '1'" do
128
+ target = described_class.new(unexpected_nested_values)
129
+ target.subscriber_number.should == "2#{subscriber_number}"
130
+ end
131
+ end
132
+
133
+ describe "reason" do
134
+ it "should leave reason blank if it is nil" do
135
+ target = described_class.new(unexpected_nested_values)
136
+ target.reason.should be_nil
137
+ end
138
+ end
139
+
140
+ describe "timestamp" do
141
+ it "should fail gracefully if it can't be converted into a DateTime" do
142
+ target = described_class.new(unexpected_nested_values)
143
+ target.timestamp.should be_nil
144
+ end
145
+ end
146
+ end
data/spec/sms_spec.rb CHANGED
@@ -133,7 +133,7 @@ describe Mblox::Sms do
133
133
  response.first.ok?.should be_false
134
134
  end
135
135
 
136
- "\r\n!\"#$\%&'\(\)*+,-.\/:;<=>?@_£¤¥§¿iÄÅÆÇÉÑÖØÜßáäåæèéìñòöøùü\tí¡ ".each_char do |i|
136
+ "\r\n!\"#\$\%&'\(\)*+,-.\/:;<=>?@_£¤¥§¿iÄÅÆÇÉÑÖØÜßáäåæèéìñòöøùü\tí¡ ".each_char do |i|
137
137
  it "allows the special char #{i}, correctly escaping illegal XML characters where necessary" do
138
138
  response = Mblox::Sms.new(LANDLINE,"#{the_message}#{i}#{the_message}").send
139
139
  response.size.should eq(1)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mblox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Isaac Betesh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-10 00:00:00.000000000 Z
11
+ date: 2013-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -125,10 +125,12 @@ files:
125
125
  - lib/mblox.rb
126
126
  - lib/mblox/configuration.rb
127
127
  - lib/mblox/sms.rb
128
+ - lib/mblox/sms_receipt.rb
128
129
  - lib/mblox/sms_response.rb
129
130
  - lib/mblox/version.rb
130
131
  - mblox.gemspec
131
132
  - spec/configuration_spec.rb
133
+ - spec/sms_receipt_spec.rb
132
134
  - spec/sms_response_result_spec.rb
133
135
  - spec/sms_spec.rb
134
136
  - spec/spec_helper.rb
@@ -180,6 +182,7 @@ summary: ! '# Mblox This gem is for subscribers to Mblox to send SMS messages.
180
182
  5. Create new Pull Request'
181
183
  test_files:
182
184
  - spec/configuration_spec.rb
185
+ - spec/sms_receipt_spec.rb
183
186
  - spec/sms_response_result_spec.rb
184
187
  - spec/sms_spec.rb
185
188
  - spec/spec_helper.rb