mblox 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ config.yml
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mblox.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Isaac Betesh
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # Mblox
2
+
3
+ This gem is for subscribers to Mblox to send SMS messages.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mblox'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mblox
18
+
19
+ ## Usage
20
+
21
+ Configuration
22
+
23
+ Mblox.configure do |config|
24
+ # Set all of these values, provided to you in your Mblox subscription
25
+ config.outbound_url = ...
26
+ config.profile_id = ...
27
+ config.sender_id = ...
28
+ config.password = ...
29
+ config.partner_name = ...
30
+ config.tariff = ...
31
+ config.service_id = ...
32
+
33
+ # You can also configure some logging options
34
+ # In a Rails environment, config.logger will default to Rails.logger and config.log_at will default to :debug
35
+ # config.log_at means the level at which Mblox will log.
36
+ # If config.log_at == :debug and your logger's log level is :info, logging will be suppressed because it is below the log level of the logger.
37
+ config.logger = Logger.new(STDOUT)
38
+ config.log_at :info
39
+ end
40
+
41
+ Once your application is configured, send messages:
42
+
43
+ phone_number = 2225555555 # The number you're sending to. Must be a 10-digit number, including the area code. Can be a String or Fixnum.
44
+ Mblox::Sms.new(phone_number, "your message").send
45
+
46
+ ## Testing
47
+
48
+ Copy config.yml.example to config.yml and set all the values in that file. Run:
49
+
50
+ rspec
51
+
52
+ You should recieve 2 SMS messages to your phone within several seconds.
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/STDOUT ADDED
@@ -0,0 +1 @@
1
+ # Logfile created on 2013-05-01 15:05:03 -0400 by logger.rb/31641
@@ -0,0 +1,8 @@
1
+ outbound_url: http://xml2.us.mblox.com:8180/send
2
+ profile_id: "A five digit number"
3
+ sender_id: "A five digit number that SMS messages are sent from"
4
+ password: "Your password"
5
+ partner_name: "Abbreviation of your company name, assigned by Mblox"
6
+ tariff: 0
7
+ service_id: "A five digit number"
8
+ test_number: "A phone number that you want to send SMS messages to when testing"
data/lib/mblox.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "mblox/configuration"
2
+ require "mblox/sms"
3
+ require "mblox/sms_error"
4
+ require "mblox/version"
5
+
6
+ module Mblox
7
+ class << self
8
+ def log *args
9
+ self.config.logger.__send__(self.config.log_level, *args) if self.config.logger
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,33 @@
1
+ module Mblox
2
+ class << self
3
+ def config
4
+ @config ||= Configuration.new
5
+ end
6
+ end
7
+
8
+ def self.configure
9
+ yield self.config
10
+ end
11
+
12
+ class Configuration
13
+ attr_accessor :outbound_url, :profile_id, :sender_id, :password, :partner_name, :tariff, :service_id
14
+ attr_reader :logger, :log_level
15
+ def initialize
16
+ @logger = Rails.logger if defined?(::Rails)
17
+ @log_level = :debug
18
+ end
19
+
20
+ def log_at level
21
+ validate @logger, level
22
+ @log_level = level
23
+ end
24
+ def logger= logger
25
+ validate logger, @log_level
26
+ @logger = logger
27
+ end
28
+ private
29
+ def validate logger, level
30
+ raise ArgumentError, "Mblox log level must be set to :fatal, :error, :warn, :info or :debug" if (logger && !logger.respond_to?(level))
31
+ end
32
+ end
33
+ end
data/lib/mblox/sms.rb ADDED
@@ -0,0 +1,65 @@
1
+ require 'active_support/core_ext/hash'
2
+ require 'addressable/uri'
3
+ require 'builder'
4
+ require "net/https"
5
+
6
+ module Mblox
7
+ class Sms
8
+ attr_reader :phone, :message
9
+ def initialize(phone,message)
10
+ phone = phone.to_s
11
+ raise SmsError, "Phone number must be ten digits" unless /\A[0-9]{10}\z/.match(phone)
12
+ raise SmsError, "Phone number cannot begin with 0 or 1" if ['0','1'].include?(phone[0].to_s)
13
+ raise SmsError, "Message cannot be blank" if message.empty?
14
+ @phone = "1#{phone}"
15
+ @message = message.dup
16
+ end
17
+
18
+ def send
19
+ commit build
20
+ end
21
+ private
22
+ def commit(request_body)
23
+ Mblox.log "Sending SMS to Mblox:\n#{request_body}"
24
+ uri = URI.parse(Mblox.config.outbound_url)
25
+ http = Net::HTTP.new(uri.host, uri.port)
26
+ request = Net::HTTP::Post.new(uri.request_uri)
27
+ request.body = request_body
28
+ request.content_type = 'text/xml'
29
+ response = http.start {|http| http.request(request) }
30
+ response = response.body
31
+ Mblox.log "Mblox responds with:\n#{response}"
32
+ build_response(Hash.from_xml(response))
33
+ end
34
+
35
+ def build_response(result)
36
+ result = result['NotificationRequestResult']
37
+ result_header = result['NotificationResultHeader']
38
+ subscriber_result = result['NotificationResultList']['NotificationResult']['SubscriberResult']
39
+ "RequestResult: \"#{result_header['RequestResultCode']}:#{result_header['RequestResultText']}\" / SubscriberResult: \"#{subscriber_result['SubscriberResultCode']}:#{subscriber_result['SubscriberResultText']}\""
40
+ end
41
+
42
+ def build
43
+ builder = Builder::XmlMarkup.new
44
+ builder.instruct!(:xml, :encoding => "ISO-8859-1")
45
+ builder.NotificationRequest(:Version => "3.5") do |nr|
46
+ nr.NotificationHeader do |nh|
47
+ nh.PartnerName(Mblox.config.partner_name)
48
+ nh.PartnerPassword(Mblox.config.password)
49
+ end
50
+ nr.NotificationList(:BatchID => "1") do |nl|
51
+ nl.Notification(:SequenceNumber => "1", :MessageType => "SMS") do |n|
52
+ n.Message(@message)
53
+ n.Profile(Mblox.config.profile_id)
54
+ n.SenderID(Mblox.config.sender_id, :Type => 'Shortcode')
55
+ n.Tariff(Mblox.config.tariff)
56
+ n.Subscriber do |s|
57
+ s.SubscriberNumber(@phone)
58
+ end
59
+ n.ServiceId(Mblox.config.service_id)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,4 @@
1
+ module Mblox
2
+ class SmsError < ArgumentError
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Mblox
2
+ VERSION = "0.0.1"
3
+ end
data/mblox.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mblox/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mblox"
8
+ spec.version = Mblox::VERSION
9
+ spec.authors = ["Isaac Betesh"]
10
+ spec.email = ["iybetesh@gmail.com"]
11
+ spec.description = "Send SMS messages"
12
+ spec.summary = `cat README.md`
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+
25
+ spec.add_runtime_dependency 'activesupport'
26
+ spec.add_runtime_dependency 'addressable'
27
+ spec.add_runtime_dependency 'builder'
28
+ end
data/spec/log_spec.rb ADDED
@@ -0,0 +1,43 @@
1
+ require "spec_helper"
2
+ require "logger"
3
+
4
+ module Mblox
5
+ class << self
6
+ def reset_configuration
7
+ @config = Configuration.new
8
+ end
9
+ end
10
+ end
11
+
12
+ describe "logger" do
13
+ before(:each) do
14
+ Mblox.reset_configuration
15
+ end
16
+
17
+ after(:all) do
18
+ set_configuration
19
+ end
20
+
21
+ it "should allow log level info" do
22
+ Mblox.config.log_at :info
23
+ Mblox.config.logger = ::Logger.new('/dev/null')
24
+ expect { Mblox.log "Some info" }.to_not raise_error
25
+ end
26
+
27
+ it "should default to log level debug" do
28
+ expect(Mblox.config.log_level).to eq(:debug)
29
+ expect { Mblox.log "Some debug info" }.to_not raise_error
30
+ end
31
+
32
+ it "should not allow log level news when the logger is created after log level is set" do
33
+ Mblox.config.log_at :news
34
+ expect { Mblox.config.logger = ::Logger.new("STDOUT")}.to raise_error(ArgumentError, "Mblox log level must be set to :fatal, :error, :warn, :info or :debug")
35
+ expect { Mblox.log "Some news" }.to_not raise_error
36
+ end
37
+
38
+ it "should not allow log level news when the logger is created before log level is set and should remain in a valid state" do
39
+ Mblox.config.logger = ::Logger.new("/dev/null")
40
+ expect { Mblox.config.log_at :news }.to raise_error(ArgumentError, "Mblox log level must be set to :fatal, :error, :warn, :info or :debug")
41
+ expect { Mblox.log "Some news" }.to_not raise_error
42
+ end
43
+ end
data/spec/sms_spec.rb ADDED
@@ -0,0 +1,48 @@
1
+ require "spec_helper"
2
+
3
+ describe "phone number" do
4
+ it "should be 10 digits" do
5
+ expect { Mblox::Sms.new("2"*9, the_message) }.to raise_error(Mblox::SmsError, "Phone number must be ten digits")
6
+ expect { Mblox::Sms.new("2"*10, the_message) }.to_not raise_error
7
+ expect { Mblox::Sms.new("2"*11, the_message) }.to raise_error(Mblox::SmsError, "Phone number must be ten digits")
8
+ end
9
+
10
+ it "should not start with 0 or 1" do
11
+ expect { Mblox::Sms.new("1"+"2"*9, the_message) }.to raise_error(Mblox::SmsError, "Phone number cannot begin with 0 or 1")
12
+ expect { Mblox::Sms.new("0"+"2"*9, the_message) }.to raise_error(Mblox::SmsError, "Phone number cannot begin with 0 or 1")
13
+ end
14
+
15
+ it "should be safe from changing" do
16
+ number = TEST_NUMBER.to_s
17
+ mblox = Mblox::Sms.new(number,the_message)
18
+ number[1..3] = ''
19
+ expect(mblox.phone).to eq("1#{TEST_NUMBER}")
20
+ end
21
+ end
22
+
23
+ describe "message" do
24
+ it "cannot be blank" do
25
+ expect { Mblox::Sms.new("2"*10, "") }.to raise_error(Mblox::SmsError, "Message cannot be blank")
26
+ end
27
+
28
+ it "should be safe from changing" do
29
+ msg = the_message
30
+ mblox = Mblox::Sms.new(TEST_NUMBER,msg)
31
+ msg[1..3] = ''
32
+ expect(mblox.message).to eq(the_message)
33
+ end
34
+ end
35
+
36
+ describe "SMS messages" do
37
+ it "should be sent when the phone number is a Fixnum" do
38
+ expect(Mblox::Sms.new(TEST_NUMBER.to_i,the_message).send).to eq(result_ok)
39
+ end
40
+
41
+ it "should be sent when the phone number is a String" do
42
+ expect(Mblox::Sms.new(TEST_NUMBER.to_s,the_message).send).to eq(result_ok)
43
+ end
44
+
45
+ it "should fail when sent to a landline" do
46
+ expect(Mblox::Sms.new("6176354500",the_message).send).to eq(result_unroutable)
47
+ end
48
+ end
@@ -0,0 +1,32 @@
1
+ require "mblox"
2
+ require "yaml"
3
+
4
+ CONFIG = YAML::load(File.open('config.yml'))
5
+
6
+ def set_configuration
7
+ Mblox.configure do |config|
8
+ config.outbound_url = CONFIG['outbound_url']
9
+ config.profile_id = CONFIG['profile_id']
10
+ config.sender_id = CONFIG['sender_id']
11
+ config.password = CONFIG['password']
12
+ config.partner_name = CONFIG['partner_name']
13
+ config.tariff = CONFIG['tariff']
14
+ config.service_id = CONFIG['service_id']
15
+ end
16
+ end
17
+
18
+ set_configuration
19
+
20
+ TEST_NUMBER = CONFIG['test_number']
21
+
22
+ def the_message
23
+ "Mblox gem test sent at #{Time.now}"
24
+ end
25
+
26
+ def result_ok
27
+ "RequestResult: \"0:OK\" / SubscriberResult: \"0:OK\""
28
+ end
29
+
30
+ def result_unroutable
31
+ "RequestResult: \"0:OK\" / SubscriberResult: \"10:MsipRejectCode=29 Number unroutable:2e Do not retry:2e\""
32
+ end
metadata ADDED
@@ -0,0 +1,179 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mblox
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Isaac Betesh
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: activesupport
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: addressable
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: builder
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Send SMS messages
111
+ email:
112
+ - iybetesh@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .gitignore
118
+ - Gemfile
119
+ - LICENSE.txt
120
+ - README.md
121
+ - Rakefile
122
+ - STDOUT
123
+ - config.yml.example
124
+ - lib/mblox.rb
125
+ - lib/mblox/configuration.rb
126
+ - lib/mblox/sms.rb
127
+ - lib/mblox/sms_error.rb
128
+ - lib/mblox/version.rb
129
+ - mblox.gemspec
130
+ - spec/log_spec.rb
131
+ - spec/sms_spec.rb
132
+ - spec/spec_helper.rb
133
+ homepage: ''
134
+ licenses:
135
+ - MIT
136
+ post_install_message:
137
+ rdoc_options: []
138
+ require_paths:
139
+ - lib
140
+ required_ruby_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ required_rubygems_version: !ruby/object:Gem::Requirement
147
+ none: false
148
+ requirements:
149
+ - - ! '>='
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ requirements: []
153
+ rubyforge_project:
154
+ rubygems_version: 1.8.24
155
+ signing_key:
156
+ specification_version: 3
157
+ summary: ! '# Mblox This gem is for subscribers to Mblox to send SMS messages. ##
158
+ Installation Add this line to your application''s Gemfile: gem ''mblox'' And
159
+ then execute: $ bundle Or install it yourself as: $ gem install mblox ## Usage Configuration Mblox.configure
160
+ do |config| # Set all of these values, provided to you in your Mblox subscription
161
+ config.outbound_url = ... config.profile_id = ... config.sender_id = ... config.password
162
+ = ... config.partner_name = ... config.tariff = ... config.service_id = ... # You
163
+ can also configure some logging options # In a Rails environment, config.logger
164
+ will default to Rails.logger and config.log_at will default to :debug # config.log_at
165
+ means the level at which Mblox will log. # If config.log_at == :debug and your logger''s
166
+ log level is :info, logging will be suppressed because it is below the log level
167
+ of the logger. config.logger = Logger.new(STDOUT) config.log_at :info end Once
168
+ your application is configured, send messages: phone_number = 2225555555 # The
169
+ number you''re sending to. Must be a 10-digit number, including the area code. Can
170
+ be a String or Fixnum. Mblox::Sms.new(phone_number, "your message").send ## Testing Copy
171
+ config.yml.example to config.yml and set all the values in that file. Run: rspec You
172
+ should recieve 2 SMS messages to your phone within several seconds. ## Contributing 1.
173
+ Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit
174
+ your changes (`git commit -am ''Add some feature''`) 4. Push to the branch (`git
175
+ push origin my-new-feature`) 5. Create new Pull Request'
176
+ test_files:
177
+ - spec/log_spec.rb
178
+ - spec/sms_spec.rb
179
+ - spec/spec_helper.rb