bobes-textmagic 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Vladimir Bobes Tuzinsky
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = textmagic
2
+
3
+ Documentation will come soon.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 Vladimir Bobes Tuzinsky. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "textmagic"
8
+ gem.summary = %Q{Ruby interface to the TextMagic's SMS gateway}
9
+ gem.email = "vladimir.tuzinsky@gmail.com"
10
+ gem.homepage = "http://github.com/bobes/textmagic"
11
+ gem.authors = ["Vladimir Bobes Tuzinsky"]
12
+ gem.rubyforge_project = "textmagic"
13
+ end
14
+
15
+ Jeweler::RubyforgeTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+
41
+ task :default => :test
42
+
43
+ require 'rake/rdoctask'
44
+ Rake::RDocTask.new do |rdoc|
45
+ if File.exist?('VERSION.yml')
46
+ config = YAML.load(File.read('VERSION.yml'))
47
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "textmagic #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
57
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 0
data/lib/api.rb ADDED
@@ -0,0 +1,34 @@
1
+ module TextMagic
2
+
3
+ class API
4
+
5
+ def initialize(username, password)
6
+ @username = username
7
+ @password = password
8
+ end
9
+
10
+ def account
11
+ response = Executor.execute('account', @username, @password)
12
+ response['balance'] = response['balance'].to_f
13
+ response
14
+ end
15
+
16
+ def send(text, *args)
17
+ options = args.last.is_a?(Hash) ? args.pop : {}
18
+ phones = args.flatten.join(',')
19
+ Executor.execute('send', @username, @password, options.merge(:text => text, :phone => phones))
20
+ end
21
+
22
+ def message_status(*ids)
23
+ Executor.execute('message_status', @username, @password, :ids => ids.flatten.join(','))
24
+ end
25
+
26
+ def receive
27
+ Executor.execute('receive', @username, @password)
28
+ end
29
+
30
+ def delete_reply(*ids)
31
+ Executor.execute('delete_reply', @username, @password, :ids => ids.flatten.join(','))
32
+ end
33
+ end
34
+ end
data/lib/error.rb ADDED
@@ -0,0 +1,18 @@
1
+ module TextMagic
2
+
3
+ class API
4
+
5
+ class Error < StandardError
6
+
7
+ attr_reader :code, :message
8
+
9
+ def initialize(response)
10
+ @code, @message = response[:error_code], response[:error_message]
11
+ end
12
+
13
+ def to_s
14
+ "#{@message} (#{@code})"
15
+ end
16
+ end
17
+ end
18
+ end
data/lib/executor.rb ADDED
@@ -0,0 +1,18 @@
1
+ module TextMagic
2
+
3
+ class API
4
+
5
+ class Executor
6
+
7
+ include HTTParty
8
+ base_uri "https://www.textmagic.com/app"
9
+
10
+ def self.execute(command, username, password, options = {})
11
+ options.merge!(:username => username, :password => password, :cmd => command)
12
+ response = self.get('/api', :query => options, :format => :json)
13
+ raise Error.new(response) if response['error_code']
14
+ response
15
+ end
16
+ end
17
+ end
18
+ end
data/lib/textmagic.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ gem 'httparty'
3
+ require 'httparty'
4
+ require 'api'
5
+ require 'executor'
6
+ require 'error'
7
+
8
+ module TextMagic
9
+ end
data/test/test_api.rb ADDED
@@ -0,0 +1,192 @@
1
+ require 'test_helper'
2
+
3
+ class APITest < Test::Unit::TestCase
4
+
5
+ def prepare_api(command, raw_response, options = {})
6
+ FakeWeb.allow_net_connect = false
7
+
8
+ @username, @password = random_string, random_string
9
+ uri = build_uri(command, @username, @password, options)
10
+ FakeWeb.register_uri(:get, uri, :string => raw_response)
11
+ TextMagic::API.new(@username, @password)
12
+ end
13
+
14
+ context 'Initialization' do
15
+
16
+ should 'require username and password' do
17
+ lambda { TextMagic::API.new }.should raise_error
18
+ TextMagic::API.new(random_string, random_string)
19
+ end
20
+ end
21
+
22
+ context 'Account command' do
23
+
24
+ setup do
25
+ @username, @password = random_string, random_string
26
+ @api = TextMagic::API.new(@username, @password)
27
+ end
28
+
29
+ should 'call Executor execute with correct arguments' do
30
+ TextMagic::API::Executor.expects(:execute).with('account', @username, @password).returns({})
31
+ @api.account
32
+ end
33
+
34
+ should 'return a hash with numeric balance value' do
35
+ TextMagic::API::Executor.expects(:execute).returns({ 'balance' => '3.14' })
36
+ response = @api.account
37
+ response.class.should == Hash
38
+ response['balance'].should == 3.14
39
+ end
40
+ end
41
+
42
+ context 'Send command' do
43
+
44
+ setup do
45
+ @username, @password = random_string, random_string
46
+ @text, @phone = random_string, random_phone
47
+ @api = TextMagic::API.new(@username, @password)
48
+ end
49
+
50
+ should 'call Executor execute with correct arguments' do
51
+ TextMagic::API::Executor.expects(:execute).with('send', @username, @password, :text => @text, :phone => @phone)
52
+ @api.send(@text, @phone)
53
+ end
54
+
55
+ should 'join multiple phone numbers supplied as an array' do
56
+ phones = Array.new(3) { random_phone }
57
+ TextMagic::API::Executor.expects(:execute).with('send', @username, @password, :text => @text, :phone => phones.join(','))
58
+ @api.send(@text, phones)
59
+ end
60
+
61
+ should 'join multiple phone numbers supplied as arguments' do
62
+ phones = Array.new(3) { random_phone }
63
+ TextMagic::API::Executor.expects(:execute).with('send', @username, @password, :text => @text, :phone => phones.join(','))
64
+ @api.send(@text, *phones)
65
+ end
66
+
67
+ should 'return a hash with message_id, sent_text and parts_count' do
68
+ message_id = random_string
69
+ TextMagic::API::Executor.expects(:execute).returns({ 'message_id' => { message_id => @phone }, 'sent_text' => @text, 'parts_count' => 1 })
70
+ response = @api.send(@text, @phone)
71
+ response['message_id'].should == { message_id => @phone }
72
+ response['sent_text'].should == @text
73
+ response['parts_count'].should == 1
74
+ end
75
+ end
76
+
77
+ context 'Message status command' do
78
+
79
+ setup do
80
+ @username, @password = random_string, random_string
81
+ @api = TextMagic::API.new(@username, @password)
82
+ end
83
+
84
+ should 'call Executor execute with correct arguments' do
85
+ id = random_string
86
+ TextMagic::API::Executor.expects(:execute).with('message_status', @username, @password, :ids => id)
87
+ @api.message_status(id)
88
+ end
89
+
90
+ should 'join ids supplied as array' do
91
+ ids = Array.new(3) { random_string }
92
+ TextMagic::API::Executor.expects(:execute).with('message_status', @username, @password, :ids => ids.join(','))
93
+ @api.message_status(ids)
94
+ end
95
+
96
+ should 'join ids supplied as arguments' do
97
+ ids = Array.new(3) { random_string }
98
+ TextMagic::API::Executor.expects(:execute).with('message_status', @username, @password, :ids => ids.join(','))
99
+ @api.message_status(*ids)
100
+ end
101
+
102
+ should 'return a hash with message ids as keys' do
103
+ TextMagic::API::Executor.expects(:execute).returns({ '8659912' => {} })
104
+ response = @api.message_status
105
+ response['8659912'].should == {}
106
+ end
107
+ end
108
+
109
+ context 'Receive command' do
110
+
111
+ setup do
112
+ @username, @password = random_string, random_string
113
+ @api = TextMagic::API.new(@username, @password)
114
+ end
115
+
116
+ should 'call Executor execute with correct arguments' do
117
+ TextMagic::API::Executor.expects(:execute).with('receive', @username, @password)
118
+ @api.receive
119
+ end
120
+
121
+ should 'return a hash with messages and number of unread messages' do
122
+ TextMagic::API::Executor.expects(:execute).returns({ 'messages' => [], 'unread' => 0 })
123
+ response = @api.receive
124
+ response['unread'].should == 0
125
+ response['messages'].should == []
126
+ end
127
+ end
128
+
129
+ context 'Delete reply command' do
130
+
131
+ setup do
132
+ @username, @password = random_string, random_string
133
+ @api = TextMagic::API.new(@username, @password)
134
+ end
135
+
136
+ should 'call Executor execute with correct arguments' do
137
+ id = random_string
138
+ TextMagic::API::Executor.expects(:execute).with('delete_reply', @username, @password, :ids => id)
139
+ @api.delete_reply(id)
140
+ end
141
+
142
+ should 'join ids supplied as array' do
143
+ ids = Array.new(3) { random_string }
144
+ TextMagic::API::Executor.expects(:execute).with('delete_reply', @username, @password, :ids => ids.join(','))
145
+ @api.delete_reply(ids)
146
+ end
147
+
148
+ should 'join ids supplied as arguments' do
149
+ ids = Array.new(3) { random_string }
150
+ TextMagic::API::Executor.expects(:execute).with('delete_reply', @username, @password, :ids => ids.join(','))
151
+ @api.delete_reply(*ids)
152
+ end
153
+
154
+ should 'return a hash with deleted ids' do
155
+ ids = Array.new(3) { random_string }
156
+ TextMagic::API::Executor.expects(:execute).returns({ 'deleted' => ids })
157
+ response = @api.receive
158
+ response['deleted'].should == ids
159
+ end
160
+ end
161
+ end
162
+
163
+ __END__
164
+
165
+ Example responses
166
+
167
+ account:
168
+ {"balance":"100"}
169
+
170
+ send:
171
+ {"message_id":{"1234567":"444444123456"},"sent_text":"test","parts_count":1}
172
+
173
+ message_status:
174
+ {"8659912":{"text":"test","status":"d","created_time":"1242979818","reply_number":"447624800500","completed_time":null,"credits_cost":"0.5"},"8659914":{"text":"test","status":"d","created_time":"1242979839","reply_number":"447624800500","completed_time":null,"credits_cost":"0.5"}}
175
+
176
+ receive (empty):
177
+ {"messages":[],"unread":0}
178
+
179
+ empty message:
180
+ {"error_code":1,"error_message":"Messages text is empty"}
181
+
182
+ insufficient parameters:
183
+ {"error_code":4,"error_message":"Insufficient parameters"}
184
+
185
+ invalid credentials:
186
+ {"error_code":5,"error_message":"Invalid username & password combination"}
187
+
188
+ invalid phone number format:
189
+ {"error_code":9,"error_message":"Wrong phone number format"}
190
+
191
+ invalid message_id:
192
+ {"error_code":14,"error_message":"Message with id 8659913 does not exist"}
@@ -0,0 +1,37 @@
1
+ require 'test_helper'
2
+ require 'json'
3
+
4
+ class ExecutorTest < Test::Unit::TestCase
5
+
6
+ context "execute method" do
7
+
8
+ setup do
9
+ FakeWeb.allow_net_connect = false
10
+
11
+ @username, @password = random_string, random_string
12
+ @command, @options = random_string, random_hash
13
+ @uri = build_uri(@command, @username, @password, @options)
14
+ end
15
+
16
+ should 'send a GET request to proper uri' do
17
+ response = random_string
18
+ FakeWeb.register_uri(:get, @uri, :string => response)
19
+ TextMagic::API::Executor.execute(@command, @username, @password, @options)
20
+ end
21
+
22
+ should 'raise an error if the response contains error_code' do
23
+ response = "{error_code:#{1 + rand(10)}}"
24
+ FakeWeb.register_uri(:get, @uri, :string => response)
25
+ lambda {
26
+ TextMagic::API::Executor.execute(@command, @username, @password, @options)
27
+ }.should raise_error(TextMagic::API::Error)
28
+ end
29
+
30
+ should 'return a hash with values in the response' do
31
+ hash = random_hash
32
+ FakeWeb.register_uri(:get, @uri, :string => hash.to_json)
33
+ response = TextMagic::API::Executor.execute(@command, @username, @password, @options)
34
+ response.should == hash
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+ require 'matchy'
6
+ require 'fakeweb'
7
+
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
10
+
11
+ require 'textmagic'
12
+
13
+ class Test::Unit::TestCase
14
+ end
15
+
16
+ def random_string(legth = 5)
17
+ rand(36 ** legth).to_s(36)
18
+ end
19
+
20
+ def random_phone
21
+ rand(10 ** 12).to_s
22
+ end
23
+
24
+ def random_hash
25
+ hash = {}
26
+ 3.times { hash[random_string] = random_string }
27
+ hash
28
+ end
29
+
30
+ def build_uri(command, username, password, options = {})
31
+ options.merge!(:cmd => command, :username => username, :password => password)
32
+ uri = "https://www.textmagic.com:443/app/api?"
33
+ uri << options.collect { |key, value| "#{key}=#{value}"}.join('&')
34
+ end
35
+
36
+ def load_response(filename)
37
+ File.read(File.join(File.dirname(__FILE__), 'fixtures', filename) + '.json')
38
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ # class TextmagicTest < Test::Unit::TestCase
4
+ # end
data/textmagic.gemspec ADDED
@@ -0,0 +1,55 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{textmagic}
5
+ s.version = "0.1.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-22}
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/error.rb",
24
+ "lib/executor.rb",
25
+ "lib/textmagic.rb",
26
+ "test/test_api.rb",
27
+ "test/test_executor.rb",
28
+ "test/test_helper.rb",
29
+ "test/test_textmagic.rb",
30
+ "textmagic.gemspec"
31
+ ]
32
+ s.has_rdoc = true
33
+ s.homepage = %q{http://github.com/bobes/textmagic}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubyforge_project = %q{textmagic}
37
+ s.rubygems_version = %q{1.3.1}
38
+ s.summary = %q{Ruby interface to the TextMagic's SMS gateway}
39
+ s.test_files = [
40
+ "test/test_api.rb",
41
+ "test/test_executor.rb",
42
+ "test/test_helper.rb",
43
+ "test/test_textmagic.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 2
49
+
50
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
51
+ else
52
+ end
53
+ else
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bobes-textmagic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vladimir Bobes Tuzinsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-22 00:00:00 -07: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/error.rb
34
+ - lib/executor.rb
35
+ - lib/textmagic.rb
36
+ - test/test_api.rb
37
+ - test/test_executor.rb
38
+ - test/test_helper.rb
39
+ - test/test_textmagic.rb
40
+ - textmagic.gemspec
41
+ has_rdoc: true
42
+ homepage: http://github.com/bobes/textmagic
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --charset=UTF-8
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project: textmagic
63
+ rubygems_version: 1.2.0
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: Ruby interface to the TextMagic's SMS gateway
67
+ test_files:
68
+ - test/test_api.rb
69
+ - test/test_executor.rb
70
+ - test/test_helper.rb
71
+ - test/test_textmagic.rb