pling-mobilant 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ *.gem
2
+ .bundle
3
+ .rspec
4
+ .rvmrc
5
+ .yardoc/
6
+ doc/
7
+ Gemfile.lock
8
+ pkg/*
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ rvm:
2
+ - ruby-head
3
+ - 1.9.2
4
+ - 1.8.7
5
+ - ree
6
+ - jruby
7
+ - rbx
8
+ - rbx-2.0.0pre
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in pling-mobilant.gemspec
4
+ gemspec
5
+
6
+ gem "pling", :git => "git://github.com/flinc/pling.git"
7
+ gem "growl_notify" if RUBY_PLATFORM =~ /darwin/
8
+ gem "guard"
9
+ gem "guard-rspec"
data/Guardfile ADDED
@@ -0,0 +1,10 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
8
+ watch('spec/spec_helper.rb') { "spec" }
9
+ end
10
+
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2011 by Firstname Lastname
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # Pling Gateway to Mobilant
2
+
3
+ ## Requirements
4
+
5
+ * [Pling](https://github.com/flinc/pling)
6
+
7
+ ## Install
8
+
9
+ gem 'pling-mobilant', :require => 'pling/mobilant'
10
+
11
+ ## Build Status
12
+
13
+ pling-mobilant is on [Travis](http://travis-ci.org/flinc/pling-mobilant) running the specs on Ruby 1.8.7, Ruby Enterprise Edition, Ruby 1.9.2, Ruby HEAD, JRuby, Rubinius and Rubinius 2.
14
+
15
+
16
+ ## Known issues
17
+
18
+ See [the issue tracker on GitHub](https://github.com/flinc/pling-mobilant/issues).
19
+
20
+
21
+ ## Repository
22
+
23
+ See [the repository on GitHub](https://github.com/flinc/pling-mobilant) and feel free to fork it!
24
+
25
+
26
+ ## Contributors
27
+
28
+ See a list of all contributors on [GitHub](https://github.com/flinc/pling-mobilant/contributors). Thanks a lot everyone!
29
+
30
+
31
+ ## Copyright
32
+
33
+ Copyright (c) 2011 [flinc AG](https://flinc.org/). See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+ require 'yard'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ YARD::Rake::YardocTask.new(:doc)
9
+
10
+ task :default => :spec
11
+
12
+ desc "Open an irb session"
13
+ task :console do
14
+ sh "bundle exec irb -rubygems -I lib -r pling/mobilant"
15
+ end
@@ -0,0 +1 @@
1
+ require 'pling/mobilant'
@@ -0,0 +1,47 @@
1
+ require 'pling'
2
+ require 'pling/mobilant/gateway'
3
+
4
+ module Pling
5
+ module Mobilant
6
+
7
+ class InvalidRecipient < Error; end
8
+
9
+ class InvalidSender < Error; end
10
+
11
+ class InvalidRoute < Error; end
12
+
13
+ class InvalidMessageText < Error; end
14
+
15
+ class InvalidMessageType < Error; end
16
+
17
+ class InvalidRoute < Error; end
18
+
19
+ class InsufficientCredits < Error; end
20
+
21
+ class NetworkNotSupportedByRoute < Error; end
22
+
23
+ class FeatureNotSupportedByRoute < Error; end
24
+
25
+ def self.error_by_response_code(code)
26
+ case code.to_s.to_i
27
+ when 10 then InvalidRecipient
28
+ when 20 then InvalidSender
29
+ when 30 then InvalidMessageText
30
+ when 31 then InvalidMessageType
31
+ when 40 then InvalidRoute
32
+ when 50 then AuthenticationFailed
33
+ when 60 then InsufficientCredits
34
+ when 70 then NetworkNotSupportedByRoute
35
+ when 71 then FeatureNotSupportedByRoute
36
+ when 80 then DeliveryFailed
37
+ else
38
+ nil
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ module Gateway
45
+ Mobilant = ::Pling::Mobilant::Gateway
46
+ end
47
+ end
@@ -0,0 +1,80 @@
1
+ require 'faraday'
2
+
3
+ require 'pling/mobilant'
4
+
5
+ module Pling
6
+ module Mobilant
7
+ class Gateway < ::Pling::Gateway::Base
8
+
9
+ handles :sms, :mobilant, :mobile
10
+
11
+ def initialize(configuration)
12
+ setup_configuration(configuration, :require => [:key])
13
+ end
14
+
15
+ def deliver!(message, device)
16
+ params = {}
17
+
18
+ # require url parameter
19
+ params[:message] = message.body
20
+ params[:to] = sanitize_identifier(device.identifier)
21
+ params[:route] = route
22
+ params[:key] = configuration[:key]
23
+
24
+ # optional url parameter
25
+ params[:from] = source if source
26
+ params[:debug] = debug if debug
27
+
28
+ response = connection.get do |request|
29
+ request.url(configuration[:delivery_url], params)
30
+ end
31
+
32
+ response_code = response.body.lines.first
33
+
34
+ if error = ::Pling::Mobilant.error_by_response_code(response_code)
35
+ raise error
36
+ else
37
+ nil
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def default_configuration
44
+ super.merge({
45
+ :delivery_url => 'https://gw.mobilant.net/',
46
+ :adapter => :net_http,
47
+ :connection => {},
48
+ :route => :lowcost
49
+ })
50
+ end
51
+
52
+ def connection
53
+ @connection ||= Faraday.new(configuration[:connection]) do |builder|
54
+ builder.use Faraday::Request::UrlEncoded
55
+ builder.use Faraday::Response::Logger if configuration[:debug]
56
+ builder.adapter(configuration[:adapter])
57
+ end
58
+ end
59
+
60
+ def route
61
+ route = (configuration[:route] || :lowcost)
62
+ [:lowcost, :lowcostplus, :direct, :directplus].include?(route) ? route : raise(::Pling::Mobilant::InvalidRoute, "Invalid route")
63
+ end
64
+
65
+ def debug
66
+ configuration[:debug] ? 1 : nil
67
+ end
68
+
69
+ def source
70
+ return nil unless configuration[:source]
71
+ configuration[:source].to_s.strip
72
+ end
73
+
74
+ def sanitize_identifier(identifier)
75
+ identifier.gsub(/^\+/, "00").gsub(/\D/, '')
76
+ end
77
+
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "pling-mobilant"
6
+ s.version = "0.1.0"
7
+ s.authors = ["benedikt", "t6d", "fabrik42"]
8
+ s.email = ["benedikt@synatic.net", "me@t6d.de", "fabrik42@gmail.com"]
9
+ s.homepage = "http://flinc.github.com/pling-mobilant"
10
+ s.summary = "Pling Gateway to Mobilant"
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
15
+ s.require_paths = ["lib"]
16
+
17
+ # specify any dependencies here; for example:
18
+ s.add_development_dependency "rspec", "~> 2.7"
19
+ s.add_development_dependency "yard", ">= 0.7"
20
+ s.add_development_dependency "rake", ">= 0.9"
21
+
22
+ s.add_runtime_dependency "pling"
23
+ end
@@ -0,0 +1,126 @@
1
+ require 'spec_helper'
2
+ require 'pling/mobilant'
3
+
4
+ module Pling
5
+ module Mobilant
6
+ describe Gateway do
7
+
8
+ context 'when created with no key' do
9
+
10
+ it "should raise an error" do
11
+ expect { Pling::Mobilant::Gateway.new({}) }.to raise_error(ArgumentError, /key is missing/)
12
+ end
13
+
14
+ end
15
+
16
+ context "when delivering a message to a device" do
17
+
18
+ let(:message) { Message.new(:body => "Hello World") }
19
+
20
+ let(:device) { Device.new(:identifier => "+49 170 1234567") }
21
+
22
+ let(:key) { 'X' * 23 }
23
+
24
+ let(:request) do
25
+ mock('Faraday request').tap do |mock|
26
+ mock.stub(:url).with(an_instance_of(String), an_instance_of(Hash))
27
+ end
28
+ end
29
+
30
+ let(:response) do
31
+ mock('Faraday response').tap do |mock|
32
+ mock.stub(:status).and_return(200)
33
+ mock.stub(:body).and_return('100')
34
+ end
35
+ end
36
+
37
+ let(:connection) do
38
+ mock('Fraday connection').tap do |mock|
39
+ mock.stub(:get).and_yield(request).and_return(response)
40
+ end
41
+ end
42
+
43
+ before do
44
+ Faraday.stub(:new).and_return(connection)
45
+ end
46
+
47
+ subject do
48
+ Pling::Mobilant::Gateway.new(:key => key)
49
+ end
50
+
51
+ it "should deliver the message if both, message and device, are valid and the gateway is configured properly" do
52
+ request.should_receive(:url).with("https://gw.mobilant.net/", {
53
+ :message => 'Hello World',
54
+ :to => "00491701234567",
55
+ :route => :lowcost,
56
+ :key => key
57
+ })
58
+
59
+ subject.deliver(message, device)
60
+ end
61
+
62
+ it "should raise an exception when the provider reports an invalid recipient" do
63
+ response.should_receive(:body).and_return("10")
64
+ expect { subject.deliver(message, device) }.
65
+ to raise_error(::Pling::Mobilant::InvalidRecipient)
66
+ end
67
+
68
+ it "should raise an exception when the provider reports an invalid sender" do
69
+ response.should_receive(:body).and_return("20")
70
+ expect { subject.deliver(message, device) }.
71
+ to raise_error(::Pling::Mobilant::InvalidSender)
72
+ end
73
+
74
+ it "should raise an exception when the provider reports an message text" do
75
+ response.should_receive(:body).and_return("30")
76
+ expect { subject.deliver(message, device) }.
77
+ to raise_error(::Pling::Mobilant::InvalidMessageText)
78
+ end
79
+
80
+ it "should raise an exception when the provider reports an message type" do
81
+ response.should_receive(:body).and_return("31")
82
+ expect { subject.deliver(message, device) }.
83
+ to raise_error(::Pling::Mobilant::InvalidMessageType)
84
+ end
85
+
86
+ it "should raise an exception when the provider reports an invalid route" do
87
+ response.should_receive(:body).and_return("40")
88
+ expect { subject.deliver(message, device) }.
89
+ to raise_error(::Pling::Mobilant::InvalidRoute)
90
+ end
91
+
92
+ it "should raise an exception when the provider reports that the authentication failed" do
93
+ response.should_receive(:body).and_return("50")
94
+ expect { subject.deliver(message, device) }.
95
+ to raise_error(::Pling::AuthenticationFailed)
96
+ end
97
+
98
+ it "should raise an exception when the provider reports insufficient credits" do
99
+ response.should_receive(:body).and_return("60")
100
+ expect { subject.deliver(message, device) }.
101
+ to raise_error(::Pling::Mobilant::InsufficientCredits)
102
+ end
103
+
104
+ it "should raise an exception when the provider reports an invalid recipient" do
105
+ response.should_receive(:body).and_return("70")
106
+ expect { subject.deliver(message, device) }.
107
+ to raise_error(::Pling::Mobilant::NetworkNotSupportedByRoute)
108
+ end
109
+
110
+ it "should raise an exception when the provider reports an invalid recipient" do
111
+ response.should_receive(:body).and_return("71")
112
+ expect { subject.deliver(message, device) }.
113
+ to raise_error(::Pling::Mobilant::FeatureNotSupportedByRoute)
114
+ end
115
+
116
+ it "should raise an exception when the provider reports an invalid recipient" do
117
+ response.should_receive(:body).and_return("80")
118
+ expect { subject.deliver(message, device) }.
119
+ to raise_error(::Pling::DeliveryFailed)
120
+ end
121
+
122
+ end
123
+
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,6 @@
1
+ require 'bundler'
2
+ Bundler.require
3
+
4
+ RSpec.configure do |config|
5
+
6
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pling-mobilant
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - benedikt
9
+ - t6d
10
+ - fabrik42
11
+ autorequire:
12
+ bindir: bin
13
+ cert_chain: []
14
+ date: 2011-10-28 00:00:00.000000000Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rspec
18
+ requirement: &70304756887460 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: '2.7'
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: *70304756887460
27
+ - !ruby/object:Gem::Dependency
28
+ name: yard
29
+ requirement: &70304756886600 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0.7'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: *70304756886600
38
+ - !ruby/object:Gem::Dependency
39
+ name: rake
40
+ requirement: &70304756884820 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0.9'
46
+ type: :development
47
+ prerelease: false
48
+ version_requirements: *70304756884820
49
+ - !ruby/object:Gem::Dependency
50
+ name: pling
51
+ requirement: &70304756883300 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: *70304756883300
60
+ description:
61
+ email:
62
+ - benedikt@synatic.net
63
+ - me@t6d.de
64
+ - fabrik42@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - .travis.yml
72
+ - Gemfile
73
+ - Guardfile
74
+ - LICENSE
75
+ - README.md
76
+ - Rakefile
77
+ - lib/pling/gateway/mobilant.rb
78
+ - lib/pling/mobilant.rb
79
+ - lib/pling/mobilant/gateway.rb
80
+ - pling-mobilant.gemspec
81
+ - spec/pling/mobilant/gateway_spec.rb
82
+ - spec/spec_helper.rb
83
+ homepage: http://flinc.github.com/pling-mobilant
84
+ licenses: []
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.10
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Pling Gateway to Mobilant
107
+ test_files:
108
+ - spec/pling/mobilant/gateway_spec.rb
109
+ - spec/spec_helper.rb
110
+ has_rdoc: