smsglobal 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Alex Cooper
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.
@@ -0,0 +1,26 @@
1
+ = smsglobal
2
+
3
+ Ruby gem to send and receive SMS messages via SMSGlobal.
4
+
5
+ To use:
6
+
7
+ require 'smsglobal'
8
+ sender = SmsGlobal::Sender.new :user => 'CHANGEME', :password => 'CHANGEME'
9
+
10
+ sender.send_text 'Test text message', '040xxxxxxx', '040yyyyyyy'
11
+
12
+ Enjoy!
13
+
14
+ == Note on Patches/Pull Requests
15
+
16
+ * Fork the project.
17
+ * Make your feature addition or bug fix.
18
+ * Add tests for it. This is important so I don't break it in a
19
+ future version unintentionally.
20
+ * Commit, do not mess with rakefile, version, or history.
21
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
22
+ * Send me a pull request. Bonus points for topic branches.
23
+
24
+ == Copyright
25
+
26
+ Copyright (c) 2010 Alex Cooper. See LICENSE for details.
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "smsglobal"
8
+ gem.summary = %Q{Ruby interface for SMSGlobal}
9
+ gem.description = %Q{Ruby gem to send and receive SMS messages via SMSGlobal.}
10
+ gem.email = "alex@kuperov.com"
11
+ gem.homepage = "http://github.com/kuperov/smsglobal"
12
+ gem.authors = ["Alex Cooper"]
13
+ gem.add_development_dependency "rspec", ">= 2.0.0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ task :default => :spec
22
+
23
+ require 'rake/rdoctask'
24
+ Rake::RDocTask.new do |rdoc|
25
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
26
+
27
+ rdoc.rdoc_dir = 'rdoc'
28
+ rdoc.title = "smsglobal #{version}"
29
+ rdoc.rdoc_files.include('README*')
30
+ rdoc.rdoc_files.include('lib/**/*.rb')
31
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,72 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ module SmsGlobal
5
+ class Sender
6
+ include Net
7
+
8
+ def initialize(options = {})
9
+ @options = options
10
+ raise 'missing :user' unless @options[:user]
11
+ raise 'missing :password' unless @options[:password]
12
+ @base = @options[:base] || 'http://www.smsglobal.com/'
13
+ end
14
+
15
+ def send_text(text, to, sender = nil, send_at = nil)
16
+ from = sender || @options[:from] || raise_error('sender is required')
17
+ params = {
18
+ :action => 'sendsms',
19
+ :user => @options[:user],
20
+ :password => @options[:password],
21
+ :from => from,
22
+ :to => to,
23
+ :text => text
24
+ }
25
+ if send_at
26
+ params[:scheduledatetime] = send_at.strftime('%Y-%m-%d %h:%M:%S')
27
+ end
28
+ params[:maxsplit] = @options[:maxsplit] if @options[:maxsplit]
29
+
30
+ resp = get(params)
31
+
32
+ case resp
33
+ when Net::HTTPSuccess
34
+ if resp.body =~ /^OK: (\d+); ([^\n]+)\s+SMSGlobalMsgID:\s*(\d+)\s*$/
35
+ return {
36
+ :status => :ok,
37
+ :code => $1.to_i,
38
+ :message => $2,
39
+ :id => $3.to_i
40
+ }
41
+ elsif resp.body =~ /^ERROR: (.+)$/
42
+ return {
43
+ :status => :error,
44
+ :message => $1
45
+ }
46
+ else
47
+ raise "Unable to parse response: '#{resp.body}'"
48
+ end
49
+ else
50
+ return {
51
+ :status => :failed,
52
+ :code => resp.code,
53
+ :message => 'Unable to reach SMSGlobal'
54
+ }
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def get(params = nil)
61
+ url = URI.join(@base, 'http-api.php')
62
+ if params
63
+ url.query = params.map { |k,v| "%s=%s" % [URI.encode(k.to_s), URI.encode(v)] }.join("&")
64
+ end
65
+ res = HTTP.start(url.host, url.port) do |http|
66
+ http.get(url.request_uri)
67
+ end
68
+ end
69
+ end
70
+
71
+ end
72
+
@@ -0,0 +1,42 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe 'SmsGlobal' do
4
+ include SmsGlobal
5
+
6
+ describe 'Sender' do
7
+ before do
8
+ @sender = Sender.new :user => 'DUMMY', :password => 'DUMMY'
9
+ end
10
+
11
+ it "requires :user and :password" do
12
+ lambda { Sender.new }.should raise_error
13
+ end
14
+
15
+ it "sends SMS correctly" do
16
+ stub_sms_ok
17
+ resp = @sender.send_text('Lorem Ipsum', '12341324', '1234')
18
+ resp[:status].should == :ok
19
+ resp[:code].should == 0
20
+ resp[:message].should == 'Sent queued message ID: 941596d028699601'
21
+ end
22
+
23
+ it "gracefully fails" do
24
+ stub_sms_failed
25
+ resp = @sender.send_text('Lorem Ipsum', '12341324', '1234')
26
+ resp[:status].should == :error
27
+ resp[:message].should == 'Missing parameter: from'
28
+ end
29
+
30
+ it "hits the right URL" do
31
+ stub_request(:get, 'http://www.smsglobal.com/http-api.php?action=sendsms&from=5678&password=DUMMY&text=xyz&to=1234&user=DUMMY').to_return(:body => 'ERROR: Missing parameter: from')
32
+ @sender.send_text('xyz', '1234', '5678')
33
+ end
34
+
35
+ it "gracefully fails on connection error" do
36
+ stub_request(:get, /www.smsglobal.com.*/).to_return(:status => [500, "Internal Server Error"])
37
+ resp = @sender.send_text('xyz', '1234', '5678')
38
+ resp[:status].should == :failed
39
+ resp[:message].should == "Unable to reach SMSGlobal"
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,23 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'smsglobal'
4
+ require 'rspec'
5
+ require 'webmock/rspec'
6
+
7
+ include SmsGlobal
8
+
9
+ RSpec.configure do |config|
10
+ config.include WebMock
11
+ end
12
+
13
+ def stub_sms(response_body)
14
+ stub_request(:get, /http:\/\/www.smsglobal.com\/http-api.php.*/).to_return(:body => response_body)
15
+ end
16
+
17
+ def stub_sms_ok
18
+ stub_sms("OK: 0; Sent queued message ID: 941596d028699601\nSMSGlobalMsgID:6764842339385521")
19
+ end
20
+
21
+ def stub_sms_failed
22
+ stub_sms("ERROR: Missing parameter: from")
23
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smsglobal
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Alex Cooper
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-19 00:00:00 +10:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 2
30
+ - 0
31
+ - 0
32
+ version: 2.0.0
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Ruby gem to send and receive SMS messages via SMSGlobal.
36
+ email: alex@kuperov.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - .gitignore
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/smsglobal.rb
52
+ - spec/smsglobal_spec.rb
53
+ - spec/spec_helper.rb
54
+ has_rdoc: true
55
+ homepage: http://github.com/kuperov/smsglobal
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --charset=UTF-8
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
70
+ version: "0"
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.3.7
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Ruby interface for SMSGlobal
86
+ test_files:
87
+ - spec/smsglobal_spec.rb
88
+ - spec/spec_helper.rb