sms_service 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 933f0ca03ae403c4e066f5faa82a52be7423e1e4
4
+ data.tar.gz: f9a35cd8c129a41417b878e76e3f946fd7ce0516
5
+ SHA512:
6
+ metadata.gz: d9d59cd8bd35f777f0d61880c01198a5938d2a470e3088688a643490c9415b2b08b3b38cb1a8437750c242819889a76e7695292995d201aed79881805bada6da
7
+ data.tar.gz: d51a4ec6ef8931a0c0291c41fbbf697b5d9455e1236fbfb020b21efc49aaa73bfa0b255b7ebea022f3ca093ad126b5eca05d0f115d10c9cb20406e2f000d8aa4
File without changes
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2006 Kamran Qureshi
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,99 @@
1
+ # Configure Services
2
+ For Rails project you can create sms_services.rb file and place it into initializers directory.
3
+
4
+
5
+ ```sh
6
+ # MUST set default sms service provider to use, can have one of these three symbols i.e. :textmarketer,
7
+ # :bulksms, :infobip. You can change service anytime using the SmsService.configure method
8
+
9
+ # configure single service provider
10
+ SmsService.configure(
11
+ :infobip=> {
12
+ :username=>"username",
13
+ :password=>"password",
14
+ :from=>"sender"
15
+ },
16
+ :service=>:infobip
17
+ )
18
+
19
+ #configfure multiple service providers
20
+ SmsService.configure(
21
+ :textmarketer=> {
22
+ :username=>"username",
23
+ :password=>"password",
24
+ :orig=>"sender"
25
+ },
26
+ :bulksms=> {
27
+ :username=>"username",
28
+ :password=>"password",
29
+ :allow_concat_text_sms=>1,
30
+ :concat_text_sms_max_parts=>3,
31
+ :repliable=>0,
32
+ :routing_group=>2
33
+ #:eapi_url=>"http://www.usa.bulksms.com:5567", if differet from default eapi
34
+ },
35
+ :infobip=> {
36
+ :username=>"username",
37
+ :password=>"password",
38
+ :from=>"sender"
39
+ },
40
+ :service=>:bulksms
41
+ )
42
+ ```
43
+ # Send SMS
44
+
45
+ ### Single message
46
+ ```sh
47
+ SmsService.send("Hello world!","44786544321")
48
+ SmsService.send({:message=>"Hello world!",:to=>"44786544321"})
49
+ ```
50
+ ### Single message to multiple recepients
51
+ ```sh
52
+ SmsService.send("Hello world!","44786544321,44787652309")
53
+ ```
54
+ #Multiple messages
55
+ ```sh
56
+ SmsService.send(["Hello world! to team A","44786544321"],["Hello world! to team A","44787652309"])
57
+ SmsService.send({:to=>"44786544321",:message=>"Hello world! to team A"},
58
+ {:message=>"Hello world! to team B",:to=>"44787652309"})
59
+ ```
60
+
61
+ ### Textmarketer
62
+ ```sh
63
+ #set service to textmarketer if not set already or using different provider
64
+ SmsService.configure(:service=>:textmarketer)
65
+ #send sms
66
+ results=SmsService.send("Hello world!","44786544321")
67
+ results.each do |result|
68
+ result.response
69
+ result.status
70
+ result.message_id
71
+ result.credits_left
72
+ result.credits_used
73
+ end
74
+ ```
75
+ ### Bulksms
76
+ ```sh
77
+ results =SmsService.send(["Hello world! to team A","44786544321"],["Hello world! to team A","44787652309"])
78
+ results.each do |result|
79
+ result.response
80
+ result.status_code
81
+ result.status_description
82
+ result.batch_id
83
+ end
84
+ ```
85
+
86
+ ### Infobip
87
+ ```sh
88
+ results=SmsService.send({:to=>"44786544321",:message=>"I am first hash"},
89
+ {:message=>"I am second hash",:to=>"44787652309,44787549510"})
90
+ results.each do |result|
91
+ result.response
92
+ result.messages
93
+ result.message
94
+ result.total_credits_used
95
+ result.credits_used
96
+ result.bulkid
97
+ result.message_id
98
+ end
99
+ ```
@@ -0,0 +1,95 @@
1
+ # Configure Services
2
+ #For Rails project you can create sms_services.rb file and place it into initializers directory.
3
+
4
+
5
+
6
+ # MUST set default sms service provider to use, can have one of these three symbols i.e. :textmarketer, :bulksms, :infobip. You can change service anytime using the SmsService.configure method
7
+
8
+ # configure single service provider
9
+ SmsService.configure(
10
+ :infobip=> {
11
+ :username=>"username",
12
+ :password=>"password",
13
+ :from=>"sender"
14
+ },
15
+ :service=>:infobip
16
+ )
17
+
18
+ #configfure multiple service providers
19
+ SmsService.configure(
20
+ :textmarketer=> {
21
+ :username=>"username",
22
+ :password=>"password",
23
+ :orig=>"sender"
24
+ },
25
+ :bulksms=> {
26
+ :username=>"username",
27
+ :password=>"password",
28
+ :allow_concat_text_sms=>1,
29
+ :concat_text_sms_max_parts=>3,
30
+ :repliable=>0,
31
+ :routing_group=>2
32
+ #:eapi_url=>"http://www.usa.bulksms.com:5567", if differet from default eapi
33
+ },
34
+ :infobip=> {
35
+ :username=>"username",
36
+ :password=>"password",
37
+ :from=>"sender"
38
+ },
39
+ :service=>:bulksms
40
+ )
41
+
42
+ # Send SMS
43
+
44
+ ### Single message
45
+
46
+ SmsService.send("Hello world!","44786544321")
47
+ SmsService.send({:message=>"Hello world!",:to=>"44786544321"})
48
+
49
+ ### Single message to multiple recepients
50
+
51
+ SmsService.send("Hello world!","44786544321,44787652309")
52
+
53
+ #Multiple messages
54
+
55
+ SmsService.send(["Hello world! to team A","44786544321"],["Hello world! to team A","44787652309"])
56
+ SmsService.send({:to=>"44786544321",:message=>"Hello world! to team A"},{:message=>"Hello world! to team B",:to=>"44787652309"})
57
+
58
+
59
+ ### Textmarketer
60
+
61
+ #set service to textmarketer if not set already or using different provider
62
+ SmsService.configure(:service=>:textmarketer)
63
+ #send sms
64
+ results=SmsService.send("Hello world!","44786544321")
65
+ results.each do |result|
66
+ result.response
67
+ result.status
68
+ result.message_id
69
+ result.credits_left
70
+ result.credits_used
71
+ end
72
+
73
+ ### Bulksms
74
+
75
+ results =SmsService.send(["Hello world! to team A","44786544321"],["Hello world! to team A","44787652309"])
76
+ results.each do |result|
77
+ result.response
78
+ result.status_code
79
+ result.status_description
80
+ result.batch_id
81
+ end
82
+
83
+
84
+ ### Infobip
85
+
86
+ results=SmsService.send({:to=>"44786544321",:message=>"I am first hash"},{:message=>"I am second hash",:to=>"44787652309,44787549510"})
87
+ results.each do |result|
88
+ result.response
89
+ result.messages
90
+ result.message
91
+ result.total_credits_used
92
+ result.credits_used
93
+ result.bulkid
94
+ result.message_id
95
+ end
@@ -0,0 +1,74 @@
1
+ #
2
+ # = Hash Recursive Merge
3
+ #
4
+ # Merges a Ruby Hash recursively, Also known as deep merge.
5
+ # Recursive version of Hash#merge and Hash#merge!.
6
+ #
7
+ # Category:: Ruby
8
+ # Package:: Hash
9
+ # Author:: Simone Carletti <weppos@weppos.net>
10
+ # Copyright:: 2007-2008 The Authors
11
+ # License:: MIT License
12
+ # Link:: http://www.simonecarletti.com/
13
+ # Source:: http://gist.github.com/gists/6391/
14
+ #
15
+ module HashRecursiveMerge
16
+
17
+ #
18
+ # Recursive version of Hash#merge!
19
+ #
20
+ # Adds the contents of +other_hash+ to +hsh+,
21
+ # merging entries in +hsh+ with duplicate keys with those from +other_hash+.
22
+ #
23
+ # Compared with Hash#merge!, this method supports nested hashes.
24
+ # When both +hsh+ and +other_hash+ contains an entry with the same key,
25
+ # it merges and returns the values from both arrays.
26
+ #
27
+ # h1 = {"a" => 100, "b" => 200, "c" => {"c1" => 12, "c2" => 14}}
28
+ # h2 = {"b" => 254, "c" => {"c1" => 16, "c3" => 94}}
29
+ # h1.rmerge!(h2) #=> {"a" => 100, "b" => 254, "c" => {"c1" => 16, "c2" => 14, "c3" => 94}}
30
+ #
31
+ # Simply using Hash#merge! would return
32
+ #
33
+ # h1.merge!(h2) #=> {"a" => 100, "b" = >254, "c" => {"c1" => 16, "c3" => 94}}
34
+ #
35
+ def rmerge!(other_hash)
36
+ merge!(other_hash) do |key, oldval, newval|
37
+ oldval.class == self.class ? oldval.rmerge!(newval) : newval
38
+ end
39
+ end
40
+
41
+ #
42
+ # Recursive version of Hash#merge
43
+ #
44
+ # Compared with Hash#merge!, this method supports nested hashes.
45
+ # When both +hsh+ and +other_hash+ contains an entry with the same key,
46
+ # it merges and returns the values from both arrays.
47
+ #
48
+ # Compared with Hash#merge, this method provides a different approch
49
+ # for merging nasted hashes.
50
+ # If the value of a given key is an Hash and both +other_hash+ abd +hsh
51
+ # includes the same key, the value is merged instead replaced with
52
+ # +other_hash+ value.
53
+ #
54
+ # h1 = {"a" => 100, "b" => 200, "c" => {"c1" => 12, "c2" => 14}}
55
+ # h2 = {"b" => 254, "c" => {"c1" => 16, "c3" => 94}}
56
+ # h1.rmerge(h2) #=> {"a" => 100, "b" => 254, "c" => {"c1" => 16, "c2" => 14, "c3" => 94}}
57
+ #
58
+ # Simply using Hash#merge would return
59
+ #
60
+ # h1.merge(h2) #=> {"a" => 100, "b" = >254, "c" => {"c1" => 16, "c3" => 94}}
61
+ #
62
+ def rmerge(other_hash)
63
+ r = {}
64
+ merge(other_hash) do |key, oldval, newval|
65
+ r[key] = oldval.class == self.class ? oldval.rmerge(newval) : newval
66
+ end
67
+ end
68
+
69
+ end
70
+
71
+
72
+ class Hash
73
+ include HashRecursiveMerge
74
+ end
@@ -0,0 +1,28 @@
1
+ require 'hash_recursive_merge'
2
+ require 'sms_service/configuration'
3
+ require 'sms_service/service'
4
+ require 'sms_service/services/message'
5
+ module SmsService
6
+ #
7
+ # merge options into @data and if block given mege those options as well
8
+ #
9
+ def self.configure(options = nil)
10
+ service=Configuration.instance
11
+ service.configure(options) unless options.nil?
12
+ yield service unless !block_given?
13
+ service.data
14
+ end
15
+ #
16
+ # Read configuraion data
17
+ #
18
+ def self.config
19
+ Configuration.instance.data
20
+ end
21
+ #
22
+ # Send SMS
23
+ #
24
+ def self.send(*sms)
25
+ service=SmsService::Service.get Configuration.service
26
+ service.send(sms)
27
+ end
28
+ end
@@ -0,0 +1,65 @@
1
+ require 'singleton'
2
+ module SmsService
3
+ #
4
+ # default parameters for sms service. It will be overridden by the
5
+ # parameters given in configuration file or given at method call
6
+ # This class can have only one instance
7
+ #
8
+ class Configuration
9
+ include Singleton
10
+ attr_accessor :data
11
+
12
+ OPTIONS=[
13
+ :service,
14
+ :username,
15
+ :password,
16
+ :sender
17
+ ]
18
+
19
+ # on initialize pouplate @data with blank config
20
+ def initialize
21
+ @data={}
22
+ end
23
+
24
+ # merge dafault with the user given options specified using Smsservice.configure method
25
+ def configure(options)
26
+ @data.rmerge!(options)
27
+ end
28
+
29
+ #
30
+ # define instance level getter/setter methods
31
+ # for each configuration/option so that we can do
32
+ # Configuration.instance.service
33
+ # and Configuration.instance.service=
34
+ #
35
+ OPTIONS.each do |option|
36
+ define_method option do
37
+ @data[option]
38
+ end
39
+ define_method "#{option}=" do |value|
40
+ @data[option]=value
41
+ end
42
+ end
43
+ #
44
+ # define class level getter/setter methods
45
+ # for each configuration/option so that we can do
46
+ # Configuration.username
47
+ # and Configuration.username=
48
+ #
49
+ instance_eval(
50
+ OPTIONS.map do |option|
51
+ o = option.to_s
52
+ <<-EOS
53
+ def #{o}
54
+ instance.data[:#{o}]
55
+ end
56
+ def #{o}=(value)
57
+ instance.data[:#{o}] = value
58
+ end
59
+ EOS
60
+ end.join("\n\n")
61
+ )
62
+
63
+ end# end configuraion class
64
+
65
+ end #end Module
@@ -0,0 +1,19 @@
1
+ require 'nokogiri'
2
+ module SmsService::Result
3
+ class Bulksms
4
+ attr_reader :status_code,:status_description,:batch_id
5
+
6
+ def initialize response
7
+ @response=response.inspect
8
+ @status_code,@status_description,@batch_id=response.shift,response.shift,response.shift
9
+ end
10
+
11
+ def self.fetch_response str
12
+ new str.split('|')
13
+ end
14
+
15
+ def response
16
+ @response
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,49 @@
1
+ module SmsService::Result
2
+ class Infobip
3
+
4
+ def initialize xml,complte_xml
5
+ @xml=xml;@message=message;@complete_xml=complte_xml;
6
+ end
7
+
8
+ def self.fetch_response xml
9
+ xml=Nokogiri::XML xml;messages=[];
10
+ xml.xpath("//message").each { |node| messages << (new node.to_s,xml)}
11
+ messages
12
+ end
13
+
14
+ def response
15
+ @complete_xml
16
+ end
17
+
18
+ def message
19
+ Nokogiri::XML @xml
20
+ end
21
+
22
+ def bulkid
23
+ b_l=@complete_xml.at("bulkId")
24
+ b_l==nil ? "" : b_l.text
25
+ end
26
+
27
+ def message_id
28
+ msg_id=@message.xpath("//messageId")
29
+ msg_id==nil ? "" : msg_id.text
30
+ end
31
+
32
+ def messages
33
+ m_l=@complete_xml.at("messages")
34
+ m_l==nil ? "" : m_l
35
+ end
36
+
37
+ def credits_used
38
+ c_u=@message.xpath("//smsCount")
39
+ c_u==nil ? "" : c_u.text
40
+ end
41
+
42
+ def total_credits_used
43
+ c_u=@complete_xml.xpath("//smsCount")
44
+ t_c=c_u.inject(0) {|r,n| r=r+n.text.to_i}
45
+ t_c==nil ? "" : t_c
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,36 @@
1
+ module SmsService::Result
2
+ class Textmarketer
3
+
4
+ def initialize xml
5
+ @xml=xml
6
+ @response=response
7
+ end
8
+
9
+ def self.fetch_response xml
10
+ new xml
11
+ end
12
+
13
+ def response
14
+ (Nokogiri::XML @xml).at("response")
15
+ end
16
+
17
+ def status
18
+ @response.attr("status")
19
+ end
20
+
21
+ def message_id
22
+ @response.attr("id")
23
+ end
24
+
25
+ def credits_left
26
+ c_l=@response.at("credits")
27
+ c_l==nil ? "" : c_l.text
28
+ end
29
+
30
+ def credits_used
31
+ c_u=@response.at("credits_used")
32
+ c_u==nil ? "" : c_u.text
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,23 @@
1
+ module SmsService
2
+
3
+ module Service
4
+ extend self
5
+ def all_services
6
+ %i(textmarketer bulksms infobip)
7
+ end
8
+
9
+ def get(name)
10
+ if all_services.include?(name)
11
+ require "sms_service/services/#{name.to_s}"
12
+ Service.const_get("#{name.capitalize}").new Configuration.instance.data[name]
13
+ else
14
+ valid_services=all_services.map(&:inspect).join(",")
15
+ #raise ConfigurationError, "Please specify a valid service provider for SmsService " +
16
+ # "(#{name.inspect} is not one of: #{valid_services})."
17
+ puts "Please specify a valid service provider for SmsService (#{name.inspect} is not one of the #{valid_services})."
18
+ end
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,46 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'uri'
4
+ require 'json'
5
+ require 'nokogiri'
6
+ module SmsService::Service
7
+
8
+ class Base
9
+ def query_url(query)
10
+ fail
11
+ end
12
+
13
+ def convert_keys_to_s options
14
+ result = {}
15
+ options.each do |key,value|
16
+ result[key.to_s] = value
17
+ end
18
+ result
19
+ end
20
+
21
+ def http_request uri,headers={}
22
+ Net::HTTP::Post.new(uri,headers)
23
+ end
24
+
25
+ def basic_auth?
26
+ false
27
+ end
28
+
29
+ def headers
30
+ {}
31
+ end
32
+
33
+ def call_api uri,form_data={}
34
+ uri=URI.parse(uri)
35
+ http = Net::HTTP.new(uri.host, uri.port)
36
+ http.use_ssl = true if uri.scheme == "https"
37
+ request = http_request uri,headers
38
+ request.basic_auth params[:username], params[:password] unless !basic_auth?
39
+ set_form_data(request,form_data) unless request.class.to_s=="Net::HTTP::Get"
40
+ response=http.request(request)
41
+ response.body
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -0,0 +1,88 @@
1
+ require 'sms_service/services/base'
2
+ require "sms_service/results/bulksms"
3
+ module SmsService::Service
4
+ class Bulksms < Base
5
+
6
+ def initialize params
7
+ @params=params
8
+ self.class.set_api_url self.class.default_api
9
+ unless self.class.url_address == nil
10
+ set_api_url url_address
11
+ @params.delete :eapi_url
12
+ end
13
+ end
14
+
15
+ def self.name
16
+ "BulkSMS"
17
+ end
18
+
19
+ def self.default_api
20
+ "http://www.bulksms.co.uk:5567"
21
+ end
22
+
23
+ def self.set_api_url host
24
+ uri=URI.parse host
25
+ @api_url="#{uri.scheme}://#{uri.host}#{gateway_for_single_sms}"
26
+ end
27
+
28
+ def self.api_url_for_single_sms
29
+ uri=URI.parse(@api_url)
30
+ @api_url="#{uri.scheme}://#{uri.host}#{gateway_for_single_sms}"
31
+ end
32
+
33
+ def self.api_url_for_batch
34
+ uri=URI.parse(@api_url)
35
+ @api_url="#{uri.scheme}://#{uri.host}#{gateway_for_batch_sms}"
36
+ end
37
+
38
+ def self.gateway_for_single_sms
39
+ "/eapi/submission/send_sms/2/2.0"
40
+ end
41
+
42
+ def self.gateway_for_batch_sms
43
+ "/eapi/submission/send_batch/1/1.0"
44
+ end
45
+
46
+ def api_url
47
+ @api_url
48
+ end
49
+
50
+ def self.url_address
51
+ SmsService::Configuration.instance.data[:bulksms][:eapi_url]
52
+ end
53
+
54
+ def sms_url params
55
+ query=@params.merge! params
56
+ query=@params.map {|k,v| "#{k}=#{v}"}.join('&')
57
+ api_url+"?"+query
58
+ end
59
+
60
+ def send message
61
+ results=[]
62
+ messages=(Message.new message).extract
63
+ messages.length == 1 ? make_request(messages.first) : make_request(messages,'multi')
64
+ end
65
+
66
+ def make_request sms,sms_type='single'
67
+ results=[]
68
+ @api_url=(sms_type=='multi' ? self.class.api_url_for_batch : self.class.api_url_for_single_sms)
69
+ append_query=(sms_type=='multi' ? {:batch_data=>to_batch(sms)} : {:msisdn=>sms.to,:message=>sms.text})
70
+ form_data=convert_keys_to_s @params.merge! append_query
71
+ result=call_api api_url,form_data
72
+ #result='0|IN_PROGRESS|887906255'
73
+ response = SmsService::Result::Bulksms.fetch_response result
74
+ results << response
75
+ results
76
+ end
77
+
78
+ def set_form_data request, form_data
79
+ request.set_form_data(form_data)
80
+ request.body=request.body.gsub("%2C",",") if request.body.to_s.include?("batch_data")
81
+ end
82
+
83
+ def to_batch messages
84
+ messages.inject("msisdn,message\n") {|result,sms| result+"\"#{sms.to.to_s}\",\"#{sms.text.to_s}\"\n" }
85
+ end
86
+
87
+ end
88
+ end
@@ -0,0 +1,127 @@
1
+ require "base64"
2
+ require 'sms_service/services/base'
3
+ require "sms_service/results/infobip"
4
+ module SmsService::Service
5
+ class Infobip < Base
6
+ attr_reader :params
7
+
8
+ def initialize params
9
+ @params=params
10
+ self.class.set_api_url self.class.default_api
11
+ end
12
+
13
+ def self.name
14
+ "Infobip"
15
+ end
16
+
17
+ def basic_auth?
18
+ true
19
+ end
20
+
21
+ def headers
22
+ { 'content-type'=> 'application/json','accept'=>'application/xml'}
23
+ end
24
+
25
+ def self.default_api
26
+ "https://api.infobip.com"
27
+ end
28
+
29
+ def self.set_api_url host
30
+ uri=URI.parse host
31
+ @api_url="#{uri.scheme}://#{uri.host}#{gateway_for_single_sms}"
32
+ end
33
+
34
+ def self.api_url_for_single_sms
35
+ uri=URI.parse(@api_url)
36
+ @api_url="#{uri.scheme}://#{uri.host}#{gateway_for_single_sms}"
37
+ end
38
+
39
+ def self.api_url_for_batch
40
+ uri=URI.parse(@api_url)
41
+ @api_url="#{uri.scheme}://#{uri.host}#{gateway_for_batch_sms}"
42
+ end
43
+
44
+ def self.gateway_for_single_sms
45
+ "/sms/1/text/single"
46
+ end
47
+
48
+ def self.gateway_for_batch_sms
49
+ "/sms/1/text/multi"
50
+ end
51
+
52
+ def api_url
53
+ @api_url
54
+ end
55
+
56
+ def sms_url params
57
+ #puts sms
58
+ query=@params.merge! params
59
+ query=@params.map {|k,v| "#{k}=#{v}"}.join('&')
60
+ api_url+"?"+query
61
+ end
62
+
63
+ def send message
64
+ results=[]
65
+ messages=(Message.new message).extract
66
+ messages.length == 1 ? make_request(messages.first) : make_request(messages,'multi')
67
+ end
68
+
69
+ def single_textual_messages sms
70
+ @api_url=self.class.api_url_for_single_sms
71
+ query={:to=>sms.to.split(','),:text=>sms.text}
72
+ query.merge!(:from=>@params[:from]) unless @params[:from]==nil
73
+ convert_keys_to_s(query).to_json
74
+ end
75
+
76
+ def multi_textual_messages sms
77
+ @api_url=self.class.api_url_for_batch
78
+ convert_keys_to_s({:messages=>to_batch(sms)}).to_json
79
+ end
80
+
81
+ def make_request sms,sms_type='single'
82
+ results=[]
83
+ form_data=sms_type=='single' ? single_textual_messages(sms) : multi_textual_messages(sms)
84
+ params=@params
85
+ result=call_api api_url,form_data
86
+ response = SmsService::Result::Infobip.fetch_response result
87
+ response.each {|res| results << res}
88
+ results
89
+ end
90
+
91
+ def set_form_data request, form_data
92
+ request.body=form_data
93
+ end
94
+
95
+ def send_single_sms sms
96
+ results=[]
97
+ @api_url=self.class.api_url_for_single_sms
98
+ append_query={:to=>sms.to,:text=>sms.text}
99
+ params=@params
100
+ form_data=convert_keys_to_s params.merge! append_query
101
+ result=call_api api_url,form_data.to_json
102
+ response = SmsService::Result::Infobip.fetch_response result
103
+ response.each {|res| results << res}
104
+ results
105
+ end
106
+
107
+ def send_batch messages
108
+ results=[]
109
+ @api_url=api_url_for_batch
110
+ batch_data="msisdn,message\n#{to_batch messages}"
111
+ append_query={:batch_data=>batch_data}
112
+ params=convert_keys_to_s @params.merge! append_query
113
+ result=call_api_with_post params
114
+ response = SmsService::Result::Bulksms.fetch_response result
115
+ results << response
116
+ results
117
+ end
118
+
119
+ def to_batch messages
120
+ from,message_s=@params[:from],[]
121
+ (from==nil || from == '') ? from={} : from={:from=>from}
122
+ messages.inject({}) {|result,sms| message_s << {:to=>sms.to.split(','),:text=>sms.text.to_s}.merge(from)}
123
+ message_s
124
+ end
125
+
126
+ end
127
+ end
@@ -0,0 +1,56 @@
1
+ module SmsService::Service
2
+ ###Message####################################################
3
+ class Message
4
+ def initialize msg
5
+ @message=msg
6
+ @sms=[]
7
+ end
8
+
9
+ def extract
10
+ is_single? ? push_single : push_multi
11
+ @sms
12
+ end
13
+
14
+ def is_single?
15
+ @message.first.is_a?(String) && @message.size==2 ? true : false
16
+ end
17
+
18
+ def push_single
19
+ @sms << (SMS.new @message.first,@message.last)
20
+ end
21
+
22
+ def push_multi
23
+ @message.each do |msg|
24
+ if msg.is_a?(Hash)
25
+ @sms << msg.to_sms
26
+ elsif msg.is_a?(Array)
27
+ @sms << msg.to_sms
28
+ end
29
+ end
30
+ end
31
+ end
32
+ ###SMS###################################################################
33
+ class SMS
34
+ attr_accessor :text,:to
35
+ def initialize text,to
36
+ @text,@to,=text,to
37
+ end
38
+ end
39
+
40
+ end ###module => SmsService::Service
41
+ ###Hash#################################################################
42
+ class Hash
43
+ def to_sms
44
+ unless empty?
45
+ msg=self[:message]
46
+ to=self[:to]
47
+ SmsService::Service::SMS.new msg.to_s,to.to_s unless msg.empty? && to.emtp?
48
+ end
49
+ end
50
+ end
51
+ ###Array################################################################
52
+ class Array
53
+ def to_sms
54
+ SmsService::Service::SMS.new first.to_s,last.to_s if size==2
55
+ end
56
+ end
@@ -0,0 +1,47 @@
1
+ require "sms_service/services/base"
2
+ require "sms_service/results/textmarketer"
3
+ module SmsService::Service
4
+ class Textmarketer < Base
5
+
6
+ def initialize parameters
7
+ @params=parameters
8
+ end
9
+
10
+ def self.name
11
+ "Textmarketer"
12
+ end
13
+
14
+
15
+ def self.api_url
16
+ "http://www.textmarketer.biz/gateway/"
17
+ end
18
+
19
+ def sms_url params
20
+ query=@params.merge! params
21
+ query=@params.map {|k,v| "#{k}=#{v}"}.join('&')
22
+ self.class.api_url+"?"+query
23
+ end
24
+
25
+ def http_request uri,headers={}
26
+ Net::HTTP::Get.new(uri,headers)
27
+ end
28
+
29
+ def send message
30
+ results=[]
31
+ messages=(Message.new message).extract
32
+ messages.each do |sms|
33
+ append_query={:number=>sms.to,:message=>URI.escape(sms.text),:option=>"xml"}
34
+ params=@params.merge! append_query
35
+ uri_with_query_string=sms_url append_query
36
+ xml=call_api uri_with_query_string
37
+ #xml = '<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE response SYSTEM "http://www.textmarketer.biz/dtd/api_response.dtd"><response status="success" id="257470055" ><credits>188</credits><credits_used>1</credits_used></response>'
38
+ response = SmsService::Result::Textmarketer.fetch_response xml
39
+ results << response
40
+ end
41
+ results
42
+ end
43
+
44
+
45
+
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module SmsService
2
+ VERSION = "0.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sms_service
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kamran Qureshi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A ruby gem that enables you to send SMS using multiple vendors like Infobipo,
14
+ BulkSMS and Textmarketer
15
+ email: kami_ravian@yahoo.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - LICENSE
22
+ - README.md
23
+ - examples/send_sms.rb
24
+ - lib/hash_recursive_merge.rb
25
+ - lib/sms_service.rb
26
+ - lib/sms_service/configuration.rb
27
+ - lib/sms_service/results/bulksms.rb
28
+ - lib/sms_service/results/infobip.rb
29
+ - lib/sms_service/results/textmarketer.rb
30
+ - lib/sms_service/service.rb
31
+ - lib/sms_service/services/base.rb
32
+ - lib/sms_service/services/bulksms.rb
33
+ - lib/sms_service/services/infobip.rb
34
+ - lib/sms_service/services/message.rb
35
+ - lib/sms_service/services/textmarketer.rb
36
+ - lib/sms_service/version.rb
37
+ homepage: https://github.com/kzq/SmsService
38
+ licenses:
39
+ - MIT
40
+ metadata: {}
41
+ post_install_message:
42
+ rdoc_options: []
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ requirements: []
56
+ rubyforge_project:
57
+ rubygems_version: 2.4.8
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Sending SMS different SMS service providers
61
+ test_files: []