messagebird 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,50 @@
1
+ module MessageBird::HTTP
2
+ class Sender
3
+ class << self
4
+
5
+ attr_writer :response_factory
6
+
7
+ def deliver(sms, &block)
8
+ ensure_valid_sms!(sms)
9
+ send_sms(sms, &block)
10
+ end
11
+
12
+ private
13
+
14
+ def send_sms(sms, &block)
15
+ connection = create_connection(sms.uri)
16
+ request = create_request(sms.request_uri)
17
+
18
+ response = send_request(connection, request)
19
+
20
+ if block_given?
21
+ yield response
22
+ end
23
+ end
24
+
25
+ def send_request(connection, request)
26
+ response_factory.call( connection.request(request) )
27
+ end
28
+
29
+ def create_connection(uri)
30
+ Net::HTTP.new(uri.host, uri.port).tap do |http|
31
+ http.use_ssl = true
32
+ # http.verify_mode = OpenSSL::SSL::VERIFY_NONE
33
+ end
34
+ end
35
+
36
+ def create_request(request_uri)
37
+ Net::HTTP::Get.new(request_uri)
38
+ end
39
+
40
+ def ensure_valid_sms!(obj)
41
+ raise ArgumentError unless obj.is_a? MessageBird::HTTP::SMS
42
+ end
43
+
44
+ def response_factory
45
+ @response_factory ||= Response.public_method(:new)
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,77 @@
1
+ module MessageBird::HTTP
2
+ class SMS < MessageBird::Deliverable
3
+ include MessageBird::Helpers
4
+
5
+ attr_reader :originator, :recipients, :message
6
+ attr_writer :username, :api_url
7
+
8
+ def initialize(originator, recipients, message, options = {}, &block)
9
+ @originator = originator
10
+ @recipients = format_recipients(recipients)
11
+ @message = URI.escape(message)
12
+ @callback = block
13
+
14
+ set_optional_variables(options)
15
+ end
16
+
17
+ def deliver
18
+ Sender.deliver(self, &@callback)
19
+ end
20
+
21
+ def uri
22
+ @uri ||= URI.parse(url)
23
+ end
24
+
25
+ def request_uri
26
+ uri.request_uri
27
+ end
28
+
29
+ def url
30
+ @url ||= "#{api_url}?username=#{username}&password=#{password}&sender=#{originator}&test=#{test_mode}&destination=#{recipients}&body=#{message}"
31
+ end
32
+
33
+ def username
34
+ @username ||= escape MessageBird::Config.username
35
+ end
36
+
37
+ def password
38
+ @password ||= escape MessageBird::Config.password
39
+ end
40
+
41
+ def test_mode
42
+ # If test = 1, then the message is not actually sent or scheduled, and there will be no credits deducted
43
+ @test_mode ||= MessageBird::Config.fetch(:test_mode, true) ? 1 : 0
44
+ end
45
+
46
+ def api_url
47
+ @api_url ||= MessageBird::Config.api_url
48
+ end
49
+
50
+ private
51
+
52
+ def format_recipients(recipients)
53
+ # API accepts multiple phone numbers by seperating them with a comma ','
54
+ if recipients.is_a? Enumerable
55
+ recipients.join(',')
56
+ else
57
+ "#{recipients}"
58
+ end
59
+ end
60
+
61
+ def set_optional_variables(options = {})
62
+ options.each do |k,v|
63
+ send("#{k}=".to_sym, v)
64
+ end
65
+ end
66
+
67
+ def password=(str)
68
+ @password = escape(str)
69
+ end
70
+
71
+ def test_mode=(bool)
72
+ raise ArgumentError unless !!bool == bool
73
+ @test_mode = bool ? 1 : 0
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,23 @@
1
+ module MessageBird
2
+ class SMS
3
+ class << self
4
+ include Helpers
5
+
6
+ def deliver(originator, recipients, message, options={}, &block)
7
+ new(originator, recipients, message, options, &block).deliver
8
+ end
9
+
10
+ def new(originator, recipients, message, options={}, &block)
11
+ module_name = options.delete(:module) || config.module
12
+ klass_for(module_name).new(originator, recipients, message, options, &block)
13
+ end
14
+
15
+ private
16
+
17
+ def klass_for(module_name)
18
+ constantize("MessageBird::#{module_name.to_s.upcase}::SMS")
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,70 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: messagebird 0.1.1 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "messagebird"
9
+ s.version = "0.1.1"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.authors = ["Bram de Vries"]
13
+ s.date = "2014-01-22"
14
+ s.description = "Implementation of the MessageBird text (sms) service API"
15
+ s.email = "bram.devries@nedap.com"
16
+ s.executables = ["messagebird_test_connection"]
17
+ s.extra_rdoc_files = [
18
+ "README.md"
19
+ ]
20
+ s.files = [
21
+ ".travis.yml",
22
+ "Gemfile",
23
+ "LICENCE",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "bin/messagebird_test_connection",
28
+ "lib/messagebird.rb",
29
+ "lib/messagebird/config.rb",
30
+ "lib/messagebird/deliverable.rb",
31
+ "lib/messagebird/helpers.rb",
32
+ "lib/messagebird/http/response.rb",
33
+ "lib/messagebird/http/response_code.rb",
34
+ "lib/messagebird/http/sender.rb",
35
+ "lib/messagebird/http/sms.rb",
36
+ "lib/messagebird/sms.rb",
37
+ "messagebird.gemspec",
38
+ "test/helper.rb",
39
+ "test/messagebird/config_test.rb",
40
+ "test/messagebird/deliverable_test.rb",
41
+ "test/messagebird/helpers_test.rb",
42
+ "test/messagebird/http/response_code_test.rb",
43
+ "test/messagebird/http/response_test.rb",
44
+ "test/messagebird/http/sender_test.rb",
45
+ "test/messagebird/http/sms_test.rb",
46
+ "test/messagebird/sms_test.rb",
47
+ "test/messagebird_test.rb"
48
+ ]
49
+ s.homepage = "http://github.com/nedap/messagebird-sms-api-ruby"
50
+ s.licenses = ["MIT"]
51
+ s.require_paths = ["lib"]
52
+ s.rubygems_version = "2.1.11"
53
+ s.summary = "MessageBird API for Ruby"
54
+
55
+ if s.respond_to? :specification_version then
56
+ s.specification_version = 4
57
+
58
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
59
+ s.add_development_dependency(%q<jeweler>, ["~> 2.0.0"])
60
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
61
+ else
62
+ s.add_dependency(%q<jeweler>, ["~> 2.0.0"])
63
+ s.add_dependency(%q<simplecov>, [">= 0"])
64
+ end
65
+ else
66
+ s.add_dependency(%q<jeweler>, ["~> 2.0.0"])
67
+ s.add_dependency(%q<simplecov>, [">= 0"])
68
+ end
69
+ end
70
+
data/test/helper.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ # Use SimpleCov when available
5
+ begin
6
+ require 'simplecov'
7
+ SimpleCov.start
8
+ rescue LoadError
9
+ end
10
+
11
+ begin
12
+ Bundler.setup(:default, :development)
13
+ rescue Bundler::BundlerError => e
14
+ $stderr.puts e.message
15
+ $stderr.puts "Run `bundle install` to install missing gems"
16
+ exit e.status_code
17
+ end
18
+ require 'minitest/autorun'
19
+ require 'rr'
20
+
21
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
22
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
23
+ require 'messagebird'
24
+
25
+ class MiniTest::Unit::TestCase
26
+ end
27
+
28
+ MiniTest.autorun
@@ -0,0 +1,99 @@
1
+ # Based on https://github.com/jwkoelewijn/batsir/blob/master/spec/batsir/config_spec.rb
2
+ require 'helper'
3
+
4
+ describe MessageBird::Config do
5
+ let(:api_url_string){ 'https://api.messagebird.com/api/sms' }
6
+
7
+ describe "with respect to retrieving variables" do
8
+ it "can check if a key exists" do
9
+ MessageBird::Config.key?(:api_url).must_equal true
10
+ MessageBird::Config.key?(:non_existent).must_equal false
11
+ end
12
+
13
+ it "can use brackets" do
14
+ MessageBird::Config[:api_url].must_equal api_url_string
15
+ end
16
+
17
+ it "can use fetch method" do
18
+ MessageBird::Config.fetch(:api_url, nil).must_equal api_url_string
19
+ end
20
+
21
+ it "returns the default value when variable does not exist" do
22
+ MessageBird::Config.fetch(:non_existing_key, 'default').must_equal 'default'
23
+ end
24
+
25
+ it "can use a method with the variable's name" do
26
+ MessageBird::Config.test_mode.must_equal true
27
+ end
28
+
29
+ it "can use return the settings in a hash" do
30
+ MessageBird::Config[:dbase] = :test
31
+ hash = MessageBird::Config.to_hash
32
+ hash.keys.include?(:api_url).must_equal true
33
+ hash.keys.include?(:dbase).must_equal true
34
+ hash.values.include?(:test).must_equal true
35
+ end
36
+ end
37
+
38
+ describe "with respect to setting variables" do
39
+ it "can set a variable using brackets" do
40
+ MessageBird::Config[:testtest] = "testtest"
41
+ MessageBird::Config.testtest.must_equal "testtest"
42
+ end
43
+
44
+ it "can set a variable using a method with the variable's name" do
45
+ MessageBird::Config.testtest = "test1"
46
+ MessageBird::Config[:testtest].must_equal "test1"
47
+ end
48
+
49
+ describe "setting multiple variables at once" do
50
+ it "can use setup method" do
51
+ MessageBird::Config.setup({:test_var => "test1", :test_var2 => "test2"})
52
+ MessageBird::Config.test_var.must_equal "test1"
53
+ MessageBird::Config.test_var2.must_equal "test2"
54
+ end
55
+
56
+ it "merges given settings with default settings when using setup method" do
57
+ MessageBird::Config.setup({:test_var => "test1", :test_var2 => "test2"})
58
+ MessageBird::Config.api_url.must_equal api_url_string
59
+ end
60
+
61
+ describe "with block notation" do
62
+ it "uses yielding" do
63
+ MessageBird::Config.use do |config|
64
+ config[:tester1] = "test1"
65
+ config[:tester2] = "test2"
66
+ end
67
+ MessageBird::Config.api_url.must_equal api_url_string
68
+ MessageBird::Config.tester1.must_equal "test1"
69
+ MessageBird::Config.tester2.must_equal "test2"
70
+ end
71
+
72
+ it "uses a block" do
73
+ MessageBird::Config.configure do
74
+ tester3 "test3"
75
+ tester4 "test4"
76
+ end
77
+ MessageBird::Config.api_url.must_equal api_url_string
78
+ MessageBird::Config.tester3.must_equal "test3"
79
+ MessageBird::Config.tester4.must_equal "test4"
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ describe "with respect to deleting variables" do
86
+ it "deletes the given key" do
87
+ MessageBird::Config.api_url.nil?.must_equal false
88
+ MessageBird::Config.delete(:api_url)
89
+ MessageBird::Config.api_url.must_equal nil
90
+ end
91
+ end
92
+ end
93
+
94
+ describe MessageBird::Config, "with respect to resetting the configuration" do
95
+ it "resets properly" do
96
+ MessageBird::Config.reset
97
+ MessageBird::Config.to_hash.must_equal MessageBird::Config.defaults
98
+ end
99
+ end
@@ -0,0 +1,11 @@
1
+ require 'helper'
2
+
3
+ describe MessageBird::Deliverable do
4
+ subject{ MessageBird::Deliverable.new }
5
+
6
+ describe '#deliver' do
7
+ it 'raises an error' do
8
+ assert_raises( RuntimeError ){ subject.deliver }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,39 @@
1
+ require 'helper'
2
+
3
+ class MockObject
4
+ include MessageBird::Helpers
5
+ end
6
+
7
+ describe MessageBird::Helpers do
8
+
9
+ subject{ MockObject.new }
10
+
11
+ describe '#config' do
12
+ it 'returns the appropriate config object' do
13
+ subject.config.must_equal MessageBird::Config
14
+ end
15
+ end
16
+
17
+ describe '#contantize' do
18
+ it 'converts a string into its corresponding constant' do
19
+ subject.constantize('String').must_equal String
20
+ subject.constantize('Integer').must_equal Integer
21
+ subject.constantize('MessageBird::Helpers').must_equal MessageBird::Helpers
22
+ end
23
+
24
+ it 'raises a NameError when class is not found' do
25
+ assert_raises(NameError) { subject.constantize('NonExistentClass') }
26
+ end
27
+
28
+ it 'raises a NameError when string not a valid class name' do
29
+ assert_raises(NameError) { subject.constantize(':::String') }
30
+ end
31
+ end
32
+
33
+ describe '#escape' do
34
+ it 'URI escapes the given string' do
35
+ mock(URI).escape("teststring")
36
+ subject.escape "teststring"
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,49 @@
1
+ require 'helper'
2
+
3
+ describe MessageBird::HTTP::ResponseCode do
4
+
5
+ let(:klass){ MessageBird::HTTP::ResponseCode}
6
+
7
+ subject{ klass.new(:foo, 'bar') }
8
+
9
+ describe '#to_sym' do
10
+ it 'returns its symbol value' do
11
+ subject.to_sym.must_equal :foo
12
+ end
13
+ end
14
+
15
+ describe '#to_s' do
16
+ it 'returns its string value' do
17
+ subject.to_s.must_equal 'bar'
18
+ end
19
+ end
20
+
21
+ describe '#==' do
22
+ it 'compares Symbols with its symbol value' do
23
+ mock.proxy(subject).to_sym.times(2)
24
+ assert subject == :foo
25
+ refute subject == :bar
26
+ end
27
+
28
+ it 'compares other objects with its string value' do
29
+ mock.proxy(subject).to_s.times(2)
30
+ assert subject == 'bar'
31
+ refute subject == 'foo'
32
+ end
33
+ end
34
+
35
+ describe '#self.decode' do
36
+ it 'returns the matching ResponseCode' do
37
+ result = klass.decode("01")
38
+ assert result.is_a?(klass)
39
+ assert result == :success
40
+ end
41
+
42
+ it 'raises an error when not found' do
43
+ assert_raises(MessageBird::HTTP::ResponseCode::ResponseCodeNotFound){
44
+ klass.decode("We are the Borg")
45
+ }
46
+ end
47
+ end
48
+
49
+ end