lleidasms 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
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,63 @@
1
+ = Lleidasms
2
+
3
+ This project rocks and uses MIT-LICENSE.
4
+
5
+ `Lleida.net` SMS gateway for Ruby
6
+
7
+ Features
8
+ --------
9
+
10
+ Enhancements
11
+ - client class
12
+ - gateway superclass for new client implementations
13
+
14
+ Examples
15
+ --------
16
+
17
+ sms = Lleidasms::Client.new
18
+ sms.connect(<YOUR USER>, <YOUR PASSWORD>)
19
+ puts sms.saldo
20
+ sms.close
21
+
22
+
23
+ Requirements
24
+ ------------
25
+
26
+ none
27
+
28
+ Install
29
+ -------
30
+
31
+ gem install lleidasms
32
+
33
+ Author
34
+ ------
35
+
36
+ Original author: Miguel Adolfo Barroso
37
+
38
+
39
+ License
40
+ -------
41
+
42
+ (The MIT License)
43
+
44
+ Copyright (c) 2012 Miguel Adolfo Barroso
45
+
46
+ Permission is hereby granted, free of charge, to any person obtaining
47
+ a copy of this software and associated documentation files (the
48
+ 'Software'), to deal in the Software without restriction, including
49
+ without limitation the rights to use, copy, modify, merge, publish,
50
+ distribute, sublicense, and/or sell copies of the Software, and to
51
+ permit persons to whom the Software is furnished to do so, subject to
52
+ the following conditions:
53
+
54
+ The above copyright notice and this permission notice shall be
55
+ included in all copies or substantial portions of the Software.
56
+
57
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
58
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
59
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
60
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
61
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
62
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
63
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ task :default => :test
5
+
6
+ desc "Run all tests"
7
+ task :test do
8
+ exec "rspec --color"
9
+ end
@@ -0,0 +1,59 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+ require "gateway"
3
+
4
+ module Lleidasms
5
+ class Client < Lleidasms::Gateway
6
+ event :ALL, :new_event
7
+
8
+ attr_accessor :timeout
9
+
10
+ def connect(user, password, timeout = 2.4)
11
+ super()
12
+ listener
13
+ cmd_login(user, password)
14
+ self.timeout= timeout
15
+ end
16
+
17
+ def saldo()
18
+ cmd_saldo
19
+ return false unless wait_for(last_label)
20
+ return @response_args[0]
21
+ end
22
+
23
+ def tarifa(numero)
24
+ cmd_tarifa numero
25
+ return false unless wait_for(last_label)
26
+ return @response_args
27
+ end
28
+
29
+ def send_sms(wait = true)
30
+ saldo
31
+ wait_for(last_label) if wait
32
+ end
33
+
34
+ private
35
+ def new_event(label, cmd, args)
36
+ @event_label = label
37
+ @event_cmd = cmd
38
+ @event_args = args
39
+ if @wait_for_label.eql? @event_label
40
+ @wait = false
41
+ @response_label = label
42
+ @response_cmd = cmd
43
+ @response_args = args
44
+ end
45
+ end
46
+
47
+ def wait_for(label)
48
+ @wait_for_label = label.to_s
49
+ @wait = true
50
+ t = Time.new
51
+ while @wait do
52
+ sleep 0.2
53
+ return false if @timeout < (Time.new - t)
54
+ end
55
+ return true
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,182 @@
1
+ require "thread"
2
+ require "socket"
3
+ require "io/wait"
4
+
5
+ module Lleidasms
6
+ class Gateway
7
+ # Client
8
+ def initialize(host = 'sms.lleida.net', port = 2048)
9
+ @host = host
10
+ @port = port
11
+ @my_label = 0
12
+
13
+ # prepare global (scriptable) data
14
+ $conected = false
15
+ $input_buffer = Queue.new
16
+ $output_buffer = String.new
17
+
18
+ $reader = lambda do |line|
19
+ $input_buffer << line.strip
20
+ parser
21
+ end
22
+ $writer = lambda do |buffer|
23
+ $server.puts "#{buffer}\r\n"
24
+ puts "<#{buffer}\r\n"
25
+ buffer.replace("")
26
+ end
27
+ end
28
+
29
+ def connect
30
+ begin
31
+ $server = TCPSocket.new(@host, @port)
32
+ $conected = true
33
+ rescue
34
+ $conected = false
35
+ puts "Unable to open a connection."
36
+ exit
37
+ end
38
+ end
39
+
40
+ def listener
41
+ Thread.new($server) do |socket|
42
+ while line = socket.gets
43
+ $reader[line]
44
+ end
45
+ close!
46
+ end
47
+ end
48
+
49
+ def conected?
50
+ $conected
51
+ end
52
+
53
+ def close
54
+ cmd_quit
55
+ end
56
+
57
+ def close!
58
+ $server.close
59
+ $conected = false
60
+ end
61
+ # Client end
62
+
63
+ # Callbacks
64
+ $callbacks = {}
65
+
66
+ def self.event(name, method_name)
67
+ $callbacks[name] = [] unless $callbacks[name]
68
+ $callbacks[name] << method_name
69
+ end
70
+
71
+ def do_event(name)
72
+ run_event name.to_sym, @label, @cmd, @args
73
+ end
74
+
75
+ def run_event(name, *args)
76
+ run_event_for(name.to_sym, self, *args)
77
+ end
78
+
79
+ def run_event_for(name, scope, *args)
80
+ return unless $callbacks[name.to_sym]
81
+ $callbacks[name.to_sym].each do |callback|
82
+ if callback.kind_of? Symbol
83
+ scope.send(callback, *args)
84
+ else
85
+ scope.instance_exec(*args, &callback)
86
+ end
87
+ end
88
+ end
89
+ # Callbacks end
90
+
91
+ def new_label
92
+ @my_label += 1
93
+ @my_label.to_s
94
+ end
95
+
96
+ def last_label
97
+ @my_label
98
+ end
99
+
100
+ # CMD Generales
101
+ def cmd_login(user, password, label_response = new_label)
102
+ $writer[label_response + " LOGIN #{user} #{password}"]
103
+ end
104
+
105
+ def cmd_ping(time = Time.now.to_i.to_s, label_response = new_label)
106
+ $writer[label_response + " PING "+ time]
107
+ end
108
+
109
+ def cmd_pong(time = Time.now.to_i.to_s, label_response = new_label)
110
+ $writer[label_response + " PONG "+ time]
111
+ end
112
+
113
+ def cmd_saldo(label_response = new_label)
114
+ $writer[label_response + " SALDO"]
115
+ end
116
+
117
+ def cmd_infonum(numero, label_response = new_label)
118
+ $writer[label_response + " INFONUM #{numero}"]
119
+ end
120
+
121
+ def cmd_tarifa(numero, label_response = new_label)
122
+ $writer[label_response + " TARIFA #{numero}"]
123
+ end
124
+
125
+ def cmd_quit(label_response = new_label)
126
+ $writer[label_response + " QUIT"]
127
+ end
128
+ # CMD Generales end
129
+
130
+ # CMD Envios MT
131
+ # CMD Envios MT end
132
+
133
+ # CMD Recepcion SMS (no premium)
134
+ def cmd_allowanswer(allow = true, label_response = new_label)
135
+ $writer[label_response + " ALLOWANSWER " + (allow ? 'ON' : 'OFF')]
136
+ end
137
+
138
+ def cmd_incomingmoack(m_id, label_response = new_label)
139
+ $writer[label_response + " INCOMINGMOACK #{m_id}"]
140
+ end
141
+ # CMD Recepcion SMS (no premium) end
142
+
143
+ # CMD Recepcion SMS (premium)
144
+ # CMD Recepcion SMS (premium) end
145
+
146
+ def parser
147
+ until $input_buffer.empty?
148
+ line = $input_buffer.shift
149
+ puts ">#{line}\r\n"
150
+ @args = line.split(' ')
151
+ @label = @args.shift
152
+ @cmd = @args.shift
153
+
154
+ case @cmd
155
+ # CMD Generales
156
+ when 'OK'
157
+ when 'NOOK'
158
+ when 'RSALDO'
159
+ when 'PING'
160
+ cmd_pong @args[0], @label
161
+ when 'PONG'
162
+ cmd_ping @args[0], @label
163
+ when 'RINFONUM'
164
+ when 'RTARIFA'
165
+ when 'BYE'
166
+ close!
167
+ # CMD Envios MT
168
+
169
+ # CMD Recepcion SMS (no premium)
170
+ when 'INCOMINGMO'
171
+
172
+ # CMD Recepcion SMS (premium)
173
+
174
+ else
175
+ # unknow
176
+ end
177
+ do_event(@cmd)
178
+ do_event(:ALL)
179
+ end
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,3 @@
1
+ module Lleidasms
2
+ VERSION = "1.0.0"
3
+ end
data/lib/lleidasms.rb ADDED
@@ -0,0 +1,2 @@
1
+ require File.expand_path('../lleidasms/client', __FILE__)
2
+ require File.expand_path('../lleidasms/gateway', __FILE__)
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class LleidasmsTest < ActiveSupport::TestCase
4
+ test "truth" do
5
+ assert_kind_of Module, Lleidasms
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
+ require "rails/test_help"
6
+
7
+ Rails.backtrace_cleaner.remove_silencers!
8
+
9
+ # Load support files
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lleidasms
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Miguel Adolfo Barroso
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-05 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &19028940 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.7.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *19028940
25
+ description: Receive and send standar and premium SMS/MMS using Lleida.net services.
26
+ email:
27
+ - mabarroso@mabarroso.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - lib/lleidasms.rb
33
+ - lib/lleidasms/version.rb
34
+ - lib/lleidasms/client.rb
35
+ - lib/lleidasms/gateway.rb
36
+ - MIT-LICENSE
37
+ - Rakefile
38
+ - README.rdoc
39
+ - test/lleidasms_test.rb
40
+ - test/test_helper.rb
41
+ homepage: https://github.com/mabarroso/lleidasms
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.10
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Lleida.net SMS gateway for Ruby.
65
+ test_files:
66
+ - test/lleidasms_test.rb
67
+ - test/test_helper.rb