sgsms 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,44 @@
1
+ = Sgsms
2
+
3
+ == Description
4
+
5
+ sgsms - A tool to send sms via SMSGlobal's HTTP API.
6
+
7
+ == Installation
8
+
9
+ gem install sgsms
10
+
11
+ == Usage
12
+
13
+ require 'sgsms'
14
+ texter = Sgsms::Texter.new
15
+ texter.send_sms("Hello", 1234567, {:user => 'smsglobalusername', :password=>'smsglobalpassword'})
16
+
17
+ Alternatively you can supply the options hash to the constructor of the texter object:
18
+ texter = Sgsms::Texter.new({:user => 'smsglobalusername', :password=>'smsglobalpassword'})
19
+ texter.send_sms("Hello", 1234567)
20
+
21
+ Finally, you can change who it appears to be from by supplying a :from in the options hash.
22
+ Sgsms::Texter.new({:user => 'smsglobalusername', :password=>'smsglobalpassword', :from => 'youknowwho'})
23
+
24
+ == License
25
+
26
+ Copyright (c) <2011> <Leonard Garvey>
27
+
28
+ Permission is hereby granted, free of charge, to any person
29
+ obtaining a copy of this software and associated documentation
30
+ files (the "Software"), to deal in the Software without
31
+ restriction, including without limitation the rights to use,
32
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
33
+ copies of the Software, and to permit persons to whom the
34
+ Software is furnished to do so, subject to the following
35
+ conditions:
36
+
37
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
38
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
39
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
40
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
41
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
42
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
43
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
44
+ OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,65 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/testtask'
4
+
5
+ require File.join(File.dirname(__FILE__), %w(lib sgsms version))
6
+
7
+ spec = Gem::Specification.new do |s|
8
+ s.name = 'sgsms'
9
+ s.version = Sgsms::Version.to_s
10
+ s.extra_rdoc_files = %w(README.rdoc)
11
+ s.rdoc_options = %w(--main README.rdoc)
12
+ s.summary = "This gem does ... "
13
+ s.author = 'First Last'
14
+ s.email = 'user@example.com'
15
+ s.homepage = 'http://my-site.net'
16
+ s.files = %w(README.rdoc Rakefile) + Dir.glob("{lib,test}/**/*")
17
+ # s.executables = ['sgsms']
18
+
19
+ # s.add_dependency('gem_name', '~> 0.0.1')
20
+
21
+ s.add_development_dependency('rake', '~> 0.8.0')
22
+ s.add_development_dependency('bundler', '~> 1.0.0')
23
+ s.add_development_dependency('jnunemaker-matchy', '~> 0.4.0')
24
+ s.add_development_dependency('shoulda', '~> 2.11.0')
25
+ s.add_development_dependency('mocha', '~> 0.9.0')
26
+ end
27
+
28
+ Rake::GemPackageTask.new(spec) do |pkg|
29
+ pkg.gem_spec = spec
30
+ end
31
+
32
+ Rake::TestTask.new do |t|
33
+ t.libs << 'test'
34
+ t.test_files = FileList["test/**/*_test.rb"]
35
+ t.verbose = true
36
+ end
37
+
38
+ begin
39
+ require 'rcov/rcovtask'
40
+
41
+ exclude_paths = %w(/Library/Ruby /usr/lib/ruby)
42
+ exclude_paths += Array(Gem.path)
43
+
44
+ options = ['--text-report'] + exclude_paths.map {|p| "-x '#{p}'" }
45
+
46
+ Rcov::RcovTask.new(:coverage) do |t|
47
+ t.libs = ['test']
48
+ t.test_files = FileList["test/**/*_test.rb"]
49
+ t.verbose = true
50
+ t.rcov_opts = options
51
+ end
52
+
53
+ task :default => :coverage
54
+
55
+ rescue LoadError
56
+ warn "\n**** Install rcov (gem install rcov) to get coverage stats ****\n"
57
+ task :default => :test
58
+ end
59
+
60
+ desc 'Generate the gemspec for this Gem'
61
+ task :gemspec do
62
+ file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
63
+ File.open(file, 'w') {|f| f << spec.to_ruby }
64
+ puts "Created gemspec: #{file}"
65
+ end
@@ -0,0 +1,13 @@
1
+ module Sgsms
2
+ module Version
3
+
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ TINY = 0
7
+
8
+ def self.to_s # :nodoc:
9
+ [MAJOR, MINOR, TINY].join('.')
10
+ end
11
+
12
+ end
13
+ end
data/lib/sgsms.rb ADDED
@@ -0,0 +1,53 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ # require 'sgsms/...'
4
+ require 'net/http'
5
+
6
+ module Sgsms
7
+ class Texter
8
+
9
+
10
+ def initialize(opts={})
11
+ @options = opts
12
+ @options[:from] = "sgsms" unless @options[:from]
13
+ end
14
+
15
+ attr_accessor :options
16
+
17
+ def send_sms(message, to, opts={})
18
+ sms_params = {
19
+ :user => opts[:user] || @options[:user],
20
+ :password => opts[:password] || @options[:password],
21
+ :from => opts[:from] || @options[:from],
22
+ :action => 'sendsms',
23
+ :text => message,
24
+ :to => to
25
+ }
26
+
27
+ raise :user_missing unless sms_params[:user]
28
+ raise :password_missing unless sms_params[:password]
29
+ raise :from_missing unless sms_params[:from]
30
+
31
+ url = URI.join("http://smsglobal.com.au", 'http-api.php')
32
+ url.query = sms_params.map {|k,v| "#{k.to_s}=#{v.to_s}"}.join('&')
33
+ response = Net::HTTP.get_response(url)
34
+
35
+ #puts response
36
+ #return {:status => :ok, :id => 1 }
37
+ case response
38
+ when Net::HTTPSuccess
39
+ if response.body.match(/^OK: 0; .+ ID: ([\dabcdef]+).*^SMSGlobalMsgID:(\d+)/m)
40
+ return {:status => :ok, :id => $1.to_s, :msgid => $2.to_i}
41
+ elsif response.body.match(/^ERROR: (.*): (.*)$/)
42
+ return {:status => :fail, :error => $1, :detail => $2}
43
+ else
44
+ raise "Cannot parse body: #{response.body}"
45
+ end
46
+ else
47
+ raise "Net error: #{response.inspect}"
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,18 @@
1
+ require 'test/unit'
2
+ require 'shoulda'
3
+ require 'fakeweb'
4
+ require File.dirname(__FILE__) + '/../lib/sgsms'
5
+
6
+ FakeWeb.allow_net_connect = false
7
+
8
+ def stub_send(response)
9
+ FakeWeb.register_uri(:any, /.*smsglobal.*/, :body => response)
10
+ end
11
+
12
+ def stub_send_ok
13
+ stub_send("OK: 0; Sent queued message ID: 34a637908349a796\nSMSGlobalMsgID:6191845908398754")
14
+ end
15
+
16
+ def stub_send_failure(error="Missing parameter",param="from")
17
+ stub_send("ERROR: #{error}: #{param}")
18
+ end
@@ -0,0 +1,52 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), %w(.. test_helper)))
2
+
3
+ class SgsmsTest < Test::Unit::TestCase
4
+ include Sgsms
5
+ context "An instance of the Sgsms class" do
6
+
7
+ should "should load" do
8
+ smser = Texter.new
9
+ assert_equal smser.class, Texter
10
+ end
11
+
12
+ should "initialise parameters" do
13
+ opts = {:user => 'test',
14
+ :password => 'password',
15
+ :from => 'sgsms-test'}
16
+ smser = Texter.new(opts)
17
+ assert smser.respond_to?(:options)
18
+ assert_equal smser.options, opts
19
+ end
20
+ should "default to values" do
21
+ opts = {:user => 'test',
22
+ :password => 'password'}
23
+ smser = Texter.new(opts)
24
+ assert smser.respond_to?(:options)
25
+ assert_equal smser.options[:from], "sgsms"
26
+ end
27
+ end
28
+
29
+ context "Sending an sms" do
30
+ setup do
31
+ @opts = {:user => 'test',
32
+ :password => 'password'}
33
+ @smser = Texter.new(@opts)
34
+ end
35
+ should "send an sms using instance" do
36
+ stub_send_ok
37
+ response = @smser.send_sms("Hello", 1234)
38
+ assert_equal response, {:status=>:ok,
39
+ :id=>"34a637908349a796",
40
+ :msgid=>6191845908398754}
41
+ end
42
+
43
+ should "not send an sms" do
44
+ stub_send_failure
45
+ response = @smser.send_sms("Hello", 1234)
46
+ assert_equal response, {:status => :fail, :error => "Missing parameter", :detail => "from"}
47
+ stub_send_failure "Missing parameter", "to"
48
+ response = @smser.send_sms("Hello", 1234)
49
+ assert_equal response, {:status => :fail, :error => "Missing parameter", :detail => "to"}
50
+ end
51
+ end
52
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sgsms
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Leonard Garvey
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-04-14 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &2156805280 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *2156805280
25
+ - !ruby/object:Gem::Dependency
26
+ name: shoulda
27
+ requirement: &2156804780 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.11.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2156804780
36
+ - !ruby/object:Gem::Dependency
37
+ name: fakeweb
38
+ requirement: &2156804300 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 1.3.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2156804300
47
+ description:
48
+ email: lengarvey@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files:
52
+ - README.rdoc
53
+ files:
54
+ - README.rdoc
55
+ - Rakefile
56
+ - lib/sgsms/version.rb
57
+ - lib/sgsms.rb
58
+ - test/test_helper.rb
59
+ - test/unit/sgsms_test.rb
60
+ homepage: https://github.com/lengarvey/sgsms
61
+ licenses: []
62
+ post_install_message:
63
+ rdoc_options:
64
+ - --main
65
+ - README.rdoc
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.7.2
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: This gem sends sms via SMSGlobal's http api
86
+ test_files: []