Permalink
Browse files

Moves everything into the Mobilant namespace

This commit also adds a constant named Mobilant to the Pling::Gateway module - just for convenience
  • Loading branch information...
t6d committed Oct 28, 2011
1 parent b6d4522 commit 32a0926ed43a68db3e016f41e309be14be045ec7
View
@@ -11,5 +11,5 @@ task :default => :spec
desc "Open an irb session"
task :console do
- sh "bundle exec irb -rubygems -I lib -r pling/gateway/mobilant.rb"
+ sh "bundle exec irb -rubygems -I lib -r pling/mobilant"
end
@@ -1,84 +1 @@
-require 'faraday'
-
-require 'pling/mobilant'
-require 'pling/mobilant/message_helper'
-require 'pling/mobilant/device_helper'
-
-module Pling
- module Gateway
- class Mobilant < Base
-
- handles :sms, :mobilant, :mobile
-
- def initialize(configuration)
- setup_configuration(configuration, :require => [:key])
- end
-
- protected
-
- def _deliver(message, device)
- params = {}
-
- # require url parameter
- params[:message] = message.body
- params[:to] = sanitize_identifier(device.identifier)
- params[:route] = route
- params[:key] = configuration[:key]
-
- # optional url parameter
- params[:from] = source if source
- params[:debug] = debug if debug
-
- response = connection.get do |request|
- request.url(configuration[:delivery_url], params)
- end
-
- response_code = response.body.lines.first
-
- if error = ::Pling::Mobilant.error_by_response_code(response_code)
- raise error
- else
- nil
- end
- end
-
- private
-
- def default_configuration
- super.merge({
- :delivery_url => 'https://gw.mobilant.net/',
- :adapter => :net_http,
- :connection => {},
- :route => :lowcost
- })
- end
-
- def connection
- @connection ||= Faraday.new(configuration[:connection]) do |builder|
- builder.use Faraday::Request::UrlEncoded
- builder.use Faraday::Response::Logger if configuration[:debug]
- builder.adapter(configuration[:adapter])
- end
- end
-
- def route
- route = (configuration[:route] || :lowcost)
- [:lowcost, :lowcostplus, :direct, :directplus].include?(route) ? route : raise(::Pling::Mobilant::InvalidRoute, "Invalid route")
- end
-
- def debug
- configuration[:debug] ? 1 : nil
- end
-
- def source
- return nil unless configuration[:source]
- configuration[:source].to_s.strip
- end
-
- def sanitize_identifier(identifier)
- identifier.gsub(/^\+/, "00").gsub(/\D/, '')
- end
-
- end
- end
-end
+require 'pling/mobilant'
View
@@ -1,7 +1,7 @@
require 'pling'
+require 'pling/mobilant/gateway'
module Pling
-
module Mobilant
class InvalidRecipient < Error; end
@@ -40,5 +40,8 @@ def self.error_by_response_code(code)
end
end
-
+
+ module Gateway
+ Mobilant = ::Pling::Mobilant::Gateway
+ end
end
@@ -0,0 +1,82 @@
+require 'faraday'
+
+require 'pling/mobilant'
+
+module Pling
+ module Mobilant
+ class Gateway < ::Pling::Gateway::Base
+
+ handles :sms, :mobilant, :mobile
+
+ def initialize(configuration)
+ setup_configuration(configuration, :require => [:key])
+ end
+
+ protected
+
+ def _deliver(message, device)
+ params = {}
+
+ # require url parameter
+ params[:message] = message.body
+ params[:to] = sanitize_identifier(device.identifier)
+ params[:route] = route
+ params[:key] = configuration[:key]
+
+ # optional url parameter
+ params[:from] = source if source
+ params[:debug] = debug if debug
+
+ response = connection.get do |request|
+ request.url(configuration[:delivery_url], params)
+ end
+
+ response_code = response.body.lines.first
+
+ if error = ::Pling::Mobilant.error_by_response_code(response_code)
+ raise error
+ else
+ nil
+ end
+ end
+
+ private
+
+ def default_configuration
+ super.merge({
+ :delivery_url => 'https://gw.mobilant.net/',
+ :adapter => :net_http,
+ :connection => {},
+ :route => :lowcost
+ })
+ end
+
+ def connection
+ @connection ||= Faraday.new(configuration[:connection]) do |builder|
+ builder.use Faraday::Request::UrlEncoded
+ builder.use Faraday::Response::Logger if configuration[:debug]
+ builder.adapter(configuration[:adapter])
+ end
+ end
+
+ def route
+ route = (configuration[:route] || :lowcost)
+ [:lowcost, :lowcostplus, :direct, :directplus].include?(route) ? route : raise(::Pling::Mobilant::InvalidRoute, "Invalid route")
+ end
+
+ def debug
+ configuration[:debug] ? 1 : nil
+ end
+
+ def source
+ return nil unless configuration[:source]
+ configuration[:source].to_s.strip
+ end
+
+ def sanitize_identifier(identifier)
+ identifier.gsub(/^\+/, "00").gsub(/\D/, '')
+ end
+
+ end
+ end
+end
@@ -1,14 +1,14 @@
require 'spec_helper'
-require 'pling/gateway/mobilant'
+require 'pling/mobilant'
module Pling
- module Gateway
- describe Mobilant do
+ module Mobilant
+ describe Gateway do
context 'when created with no key' do
it "should raise an error" do
- expect { Pling::Gateway::Mobilant.new({}) }.to raise_error(ArgumentError, /key is missing/)
+ expect { Pling::Mobilant::Gateway.new({}) }.to raise_error(ArgumentError, /key is missing/)
end
end
@@ -45,7 +45,7 @@ module Gateway
end
subject do
- Pling::Gateway::Mobilant.new(:key => key)
+ Pling::Mobilant::Gateway.new(:key => key)
end
it "should deliver the message if both, message and device, are valid and the gateway is configured properly" do

0 comments on commit 32a0926

Please sign in to comment.