textmagic 0.2.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.
@@ -0,0 +1,176 @@
1
+ require 'test_helper'
2
+
3
+ class ResponseTest < Test::Unit::TestCase
4
+
5
+ context 'Account response' do
6
+
7
+ setup do
8
+ @balance = 0.1 * rand(1e4)
9
+ @response = { 'balance' => @balance.to_s }
10
+ @response.extend TextMagic::API::Response::Account
11
+ end
12
+
13
+ should 'allow access to balance' do
14
+ @response.balance.should be_close(@balance, 1e-10)
15
+ end
16
+ end
17
+
18
+ context 'Send response' do
19
+
20
+ setup do
21
+ @message_id = {
22
+ '141421' => '999314159265',
23
+ '173205' => '999271828182'
24
+ }
25
+ @text = random_string
26
+ @parts_count = rand(10)
27
+ @response = {
28
+ 'message_id' => @message_id,
29
+ 'sent_text' => @text,
30
+ 'parts_count' => @parts_count
31
+ }
32
+ @response.extend TextMagic::API::Response::Send
33
+ end
34
+
35
+ should 'allow access to inverted message_id hash' do
36
+ @response.message_id_hash.should == @message_id.invert
37
+ end
38
+
39
+ should 'allow access to message_ids array' do
40
+ @response.message_ids.should == ['141421', '173205']
41
+ end
42
+
43
+ should 'allow access to message_id for a given phone number' do
44
+ @response.message_id('999314159265').should == '141421'
45
+ @response.message_id('999271828182').should == '173205'
46
+ end
47
+
48
+ should 'allow access to sent_text' do
49
+ @response.sent_text.should == @text
50
+ end
51
+
52
+ should 'allow access to parts_count' do
53
+ @response.parts_count.should == @parts_count
54
+ end
55
+ end
56
+
57
+ context 'MessageStatus response' do
58
+
59
+ setup do
60
+ @text = random_string
61
+ @reply_number = random_phone
62
+ @created_time = (Time.now - 30).to_i
63
+ @completed_time = (Time.now - 20).to_i
64
+ @credits_cost = 0.01 * rand(300)
65
+ @response = {
66
+ '141421' => {
67
+ 'text' => @text,
68
+ 'status' => 'd',
69
+ 'created_time' => @created_time,
70
+ 'reply_number' => @reply_number,
71
+ 'completed_time' => @completed_time,
72
+ 'credits_cost' => @credits_cost
73
+ },
74
+ '173205' => {
75
+ 'text' => 'test',
76
+ 'status' => 'r',
77
+ 'created_time' => '1242979839',
78
+ 'reply_number' => '447624800500',
79
+ 'completed_time' => nil,
80
+ 'credits_cost' => 0.5
81
+ }
82
+ }
83
+ @response.extend TextMagic::API::Response::MessageStatus
84
+ end
85
+
86
+ should 'allow access to text for all statuses' do
87
+ @response['141421'].text.should == @text
88
+ @response['173205'].text.should == 'test'
89
+ end
90
+
91
+ should 'allow access to status for a given message_id' do
92
+ @response['141421'].status.should == 'd'
93
+ @response['173205'].status.should == 'r'
94
+ end
95
+
96
+ should 'allow access to reply_number for a given message_id' do
97
+ @response['141421'].reply_number.should == @reply_number
98
+ @response['173205'].reply_number.should == '447624800500'
99
+ end
100
+
101
+ should 'allow access to created_time for a given message_id' do
102
+ @response['141421'].created_time.should == Time.at(@created_time)
103
+ @response['173205'].created_time.should == Time.at(1242979839)
104
+ end
105
+
106
+ should 'allow access to completed_time for a given message_id' do
107
+ @response['141421'].completed_time.should == Time.at(@completed_time)
108
+ @response['173205'].completed_time.should == nil
109
+ end
110
+
111
+ should 'allow access to credits_cost for a given message_id' do
112
+ @response['141421'].credits_cost.should be_close(@credits_cost, 1e-10)
113
+ @response['173205'].credits_cost.should be_close(0.5, 1e-10)
114
+ end
115
+ end
116
+
117
+ context 'Receive response' do
118
+
119
+ setup do
120
+ @timestamp = (Time.now - 30).to_i
121
+ @message1 = {
122
+ 'timestamp' => @timestamp,
123
+ 'from' => '999314159265',
124
+ 'text' => 'Hi Fred',
125
+ 'message_id' => '141421'
126
+ }
127
+ @message2 = {
128
+ 'timestamp' => 1243244148,
129
+ 'from' => '999271828182',
130
+ 'text' => 'Hello buddy',
131
+ 'message_id' => '173205'
132
+ }
133
+ @messages = [@message1, @message2]
134
+ @unread = rand(1e4)
135
+ @response = { 'unread' => @unread, 'messages' => @messages }
136
+ @response.extend TextMagic::API::Response::Receive
137
+ end
138
+
139
+ should 'allow access to unread' do
140
+ @response.unread.should == @unread
141
+ end
142
+
143
+ should 'allow access to messages array' do
144
+ @response.messages.should == @messages
145
+ end
146
+
147
+ should 'allow access to message_id for all messages' do
148
+ @response.messages.first.message_id.should == '141421'
149
+ end
150
+
151
+ should 'allow access to timestamp for all messages' do
152
+ @response.messages.first.timestamp.should == Time.at(@timestamp)
153
+ end
154
+
155
+ should 'allow access to from for all messages' do
156
+ @response.messages.first.from.should == '999314159265'
157
+ end
158
+
159
+ should 'allow access to text for all messages' do
160
+ @response.messages.first.text.should == 'Hi Fred'
161
+ end
162
+ end
163
+
164
+ context 'DeleteReply response' do
165
+
166
+ setup do
167
+ @ids = ['141421', '1780826']
168
+ @response = { 'deleted' => @ids }
169
+ @response.extend TextMagic::API::Response::DeleteReply
170
+ end
171
+
172
+ should 'allow access to deleted' do
173
+ @response.deleted.should == @ids
174
+ end
175
+ end
176
+ end
@@ -0,0 +1,99 @@
1
+ require 'test_helper'
2
+ require 'json'
3
+
4
+ class ValidationTest < Test::Unit::TestCase
5
+
6
+ context 'validate_text_length method for non-unicode texts' do
7
+
8
+ should 'return true if parts limit is set to 1 and text length is less than or equal to 160' do
9
+ TextMagic::API.validate_text_length(random_string(160), false, 1).should == true
10
+ end
11
+
12
+ should 'return false if parts limit is set to 1 and text length is greater than 160' do
13
+ TextMagic::API.validate_text_length(random_string(161), false, 1).should == false
14
+ end
15
+
16
+ should 'return true if parts limit is set to 2 and text length is less than or equal to 306' do
17
+ TextMagic::API.validate_text_length(random_string(306), false, 2).should == true
18
+ end
19
+
20
+ should 'return false if parts limit is set to 2 and text length is greater than 306' do
21
+ TextMagic::API.validate_text_length(random_string(307), false, 2).should == false
22
+ end
23
+
24
+ should 'return true if parts limit is set to 3 or is not specified and text length is less than or equal to 459' do
25
+ TextMagic::API.validate_text_length(random_string(459), false).should == true
26
+ TextMagic::API.validate_text_length(random_string(459), false, 3).should == true
27
+ end
28
+
29
+ should 'return false if parts limit is set to 3 or is not specified and text length is greater than 459' do
30
+ TextMagic::API.validate_text_length(random_string(460), false).should == false
31
+ TextMagic::API.validate_text_length(random_string(460), false, 3).should == false
32
+ end
33
+ end
34
+
35
+ context 'validate_text_length method for unicode texts' do
36
+
37
+ should 'return true if parts limit is set to 1 and text length is less than or equal to 70' do
38
+ TextMagic::API.validate_text_length(random_string(70), true, 1).should == true
39
+ end
40
+
41
+ should 'return false if parts limit is set to 1 and text length is greater than 70' do
42
+ TextMagic::API.validate_text_length(random_string(71), true, 1).should == false
43
+ end
44
+
45
+ should 'return true if parts limit is set to 2 and text length is less than or equal to 134' do
46
+ TextMagic::API.validate_text_length(random_string(134), true, 2).should == true
47
+ end
48
+
49
+ should 'return false if parts limit is set to 2 and text length is greater than 134' do
50
+ TextMagic::API.validate_text_length(random_string(135), true, 2).should == false
51
+ end
52
+
53
+ should 'return true if parts limit is set to 3 or is not specified and text length is less than or equal to 201' do
54
+ TextMagic::API.validate_text_length(random_string(201), true).should == true
55
+ TextMagic::API.validate_text_length(random_string(201), true, 3).should == true
56
+ end
57
+
58
+ should 'return false if parts limit is set to 3 or is not specified and text length is greater than 201' do
59
+ TextMagic::API.validate_text_length(random_string(202), true).should == false
60
+ TextMagic::API.validate_text_length(random_string(202), true, 3).should == false
61
+ end
62
+ end
63
+
64
+ context 'validate_phones method' do
65
+
66
+ should 'return true if phone number consists of up to 15 digits' do
67
+ TextMagic::API.validate_phones(rand(10 ** 15).to_s).should == true
68
+ end
69
+
70
+ should 'return false if phone number is longer than 15 digits' do
71
+ TextMagic::API.validate_phones((10 ** 16 + rand(10 ** 15)).to_s).should == false
72
+ end
73
+
74
+ should 'return false if phone number contains non-digits' do
75
+ TextMagic::API.validate_phones(random_string).should == false
76
+ end
77
+
78
+ should 'return false if phone number is empty' do
79
+ TextMagic::API.validate_phones('').should == false
80
+ end
81
+
82
+ should 'return true if all phone numbers in a list are valid' do
83
+ phone1, phone2 = rand(10 ** 15).to_s, rand(10 ** 15).to_s
84
+ TextMagic::API.validate_phones(phone1, phone2).should == true
85
+ TextMagic::API.validate_phones([phone1, phone2]).should == true
86
+ end
87
+
88
+ should 'return false if phone numbers list is empty' do
89
+ TextMagic::API.validate_phones().should == false
90
+ end
91
+
92
+ should 'return false if format of any of phone numbers in a list is invalid' do
93
+ phone1 = rand(10 ** 15).to_s, rand(10 ** 15).to_s
94
+ phone2 = random_string
95
+ TextMagic::API.validate_phones(phone1, phone2).should == false
96
+ TextMagic::API.validate_phones([phone1, phone2]).should == false
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,64 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{textmagic}
5
+ s.version = "0.2.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Vladimir Bobes Tuzinsky"]
9
+ s.date = %q{2009-05-25}
10
+ s.email = %q{vladimir.tuzinsky@gmail.com}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE",
13
+ "README.rdoc"
14
+ ]
15
+ s.files = [
16
+ ".document",
17
+ ".gitignore",
18
+ "LICENSE",
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "VERSION.yml",
22
+ "lib/api.rb",
23
+ "lib/charset.rb",
24
+ "lib/error.rb",
25
+ "lib/executor.rb",
26
+ "lib/response.rb",
27
+ "lib/textmagic.rb",
28
+ "lib/validation.rb",
29
+ "test/test_api.rb",
30
+ "test/test_charset.rb",
31
+ "test/test_error.rb",
32
+ "test/test_executor.rb",
33
+ "test/test_helper.rb",
34
+ "test/test_response.rb",
35
+ "test/test_validation.rb",
36
+ "textmagic.gemspec"
37
+ ]
38
+ s.has_rdoc = true
39
+ s.homepage = %q{http://github.com/bobes/textmagic}
40
+ s.rdoc_options = ["--charset=UTF-8"]
41
+ s.require_paths = ["lib"]
42
+ s.rubyforge_project = %q{textmagic}
43
+ s.rubygems_version = %q{1.3.1}
44
+ s.summary = %q{Ruby interface to the TextMagic's SMS gateway}
45
+ s.test_files = [
46
+ "test/test_api.rb",
47
+ "test/test_charset.rb",
48
+ "test/test_error.rb",
49
+ "test/test_executor.rb",
50
+ "test/test_helper.rb",
51
+ "test/test_response.rb",
52
+ "test/test_validation.rb"
53
+ ]
54
+
55
+ if s.respond_to? :specification_version then
56
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
57
+ s.specification_version = 2
58
+
59
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
60
+ else
61
+ end
62
+ else
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: textmagic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Vladimir Bobes Tuzinsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-25 00:00:00 +02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: vladimir.tuzinsky@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION.yml
32
+ - lib/api.rb
33
+ - lib/charset.rb
34
+ - lib/error.rb
35
+ - lib/executor.rb
36
+ - lib/response.rb
37
+ - lib/textmagic.rb
38
+ - lib/validation.rb
39
+ - test/test_api.rb
40
+ - test/test_charset.rb
41
+ - test/test_error.rb
42
+ - test/test_executor.rb
43
+ - test/test_helper.rb
44
+ - test/test_response.rb
45
+ - test/test_validation.rb
46
+ - textmagic.gemspec
47
+ has_rdoc: true
48
+ homepage: http://github.com/bobes/textmagic
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project: textmagic
69
+ rubygems_version: 1.3.1
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: Ruby interface to the TextMagic's SMS gateway
73
+ test_files:
74
+ - test/test_api.rb
75
+ - test/test_charset.rb
76
+ - test/test_error.rb
77
+ - test/test_executor.rb
78
+ - test/test_helper.rb
79
+ - test/test_response.rb
80
+ - test/test_validation.rb