luka-multiinfo 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 @@
1
+ .DS_Store
data/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ 2009-07-29 Version 0.0.1
2
+ - sending sms message
3
+ - receiving smsm message status
4
+ - cancel sending sms
5
+ - config enabled with yml file
6
+ - HTTPclient used for HTTPS with client cert comunication
data/License.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Łukasz Łuczak
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.
data/RDOC_README.txt ADDED
@@ -0,0 +1,49 @@
1
+ = Polkomtel MultiInfo HTTPS API wrapped into Ruby library
2
+
3
+ This gem is Ruby interface to the Polkomtel's MultiInfo SMS gateway service, based on Luke Redpath's clickatel library
4
+ To use this gem, you will need an access to Polkomtel's MultiInfo service. You need user, password, sercvice_id and certificate for ssl connection.
5
+ Contact Polkomtel sales reps for info how to obtain acces to MultiInfo service.
6
+
7
+ == Basic Usage
8
+
9
+ You will need your Mutliinfo credentials and SSL certificate to use this library.
10
+
11
+ require 'rubygems'
12
+ require 'multiinfo'
13
+
14
+ api = MultiInfo::API.new
15
+
16
+ # Example api calls
17
+ api.send_message('48661351024', 'Hello') # sends sms 'Hello' to 48 661 351 024 and returns message id
18
+ api.message_info('22') # gets info about message with id 22
19
+
20
+
21
+ == Conifguration options
22
+
23
+ Default path for config file and certificate files is $HOME/multiinfo. You should place following files in that directory:
24
+ multiinfo.yml # config file
25
+ multiinfo.crt # certificate
26
+ multiinfo.pem # RSA key
27
+
28
+ In multiinfo.yml you should specify at least:
29
+ login
30
+ password
31
+ service_id
32
+
33
+ If you want to use different names for certificate files speciify them with FULL PATH in config file under:
34
+
35
+ client_cert
36
+ client_key
37
+
38
+ See example in config_example.yml
39
+
40
+
41
+ To load your custom config file use following code
42
+ require 'rubygems'
43
+ require 'multiinfo'
44
+
45
+ config = MultiInfo::API.load_auth_options(CONFIG_FILE_FULL_PATH)
46
+ api = MultiInfo::API.new(config)
47
+
48
+
49
+
data/README.txt ADDED
@@ -0,0 +1,49 @@
1
+ == Polkomtel MultiInfo HTTPS API wrapped into Ruby library
2
+
3
+ This gem is Ruby interface to the Polkomtel's MultiInfo SMS gateway service, based on Luke Redpath's clickatel library
4
+ To use this gem, you will need an access to Polkomtel's MultiInfo service. You need user, password, sercvice_id and certificate for ssl connection.
5
+ Contact Polkomtel sales reps for info how to obtain acces to MultiInfo service.
6
+
7
+ == Basic Usage
8
+
9
+ You will need your Mutliinfo credentials and SSL certificate to use this library.
10
+
11
+ require 'rubygems'
12
+ require 'multiinfo'
13
+
14
+ api = MultiInfo::API.new
15
+
16
+ # Example api calls
17
+ api.send_message('48661351024', 'Hello') # sends sms 'Hello' to 48 661 351 024 and returns message id
18
+ api.message_info('22') # gets info about message with id 22
19
+
20
+
21
+ == Conifguration options
22
+
23
+ Default path for config file and certificate files is $HOME/multiinfo. You should place following files in that directory:
24
+ multiinfo.yml # config file
25
+ multiinfo.crt # certificate
26
+ multiinfo.pem # RSA key
27
+
28
+ In multiinfo.yml you should specify at least:
29
+ login
30
+ password
31
+ service_id
32
+
33
+ If you want to use different names for certificate files speciify them with FULL PATH in config file under:
34
+
35
+ client_cert
36
+ client_key
37
+
38
+ See example in config_example.yml
39
+
40
+
41
+ To load your custom config file use following code
42
+ require 'rubygems'
43
+ require 'multiinfo'
44
+
45
+ config = MultiInfo::API.load_auth_options(CONFIG_FILE_FULL_PATH)
46
+ api = MultiInfo::API.new(config)
47
+
48
+
49
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,8 @@
1
+ # example config file for authentication MultiInfo HTTPS API
2
+ login: luka
3
+ password: test
4
+ service_id: 1
5
+
6
+ # Optionally you can specify full path to certificate files
7
+ client_cert: /home/luka/weblify.cert
8
+ client_key: /home/luka/weblify.key
@@ -0,0 +1,23 @@
1
+ class Hash
2
+ # Returns a new hash containing only the keys specified
3
+ # that exist in the current hash.
4
+ #
5
+ # {:a => '1', :b => '2', :c => '3'}.only(:a, :c)
6
+ # # => {:a => '1', :c => '3'}
7
+ #
8
+ # Keys that do not exist in the original hash are ignored.
9
+ def only(*keys)
10
+ inject( {} ) do |new_hash, (key, value)|
11
+ new_hash[key] = value if keys.include?(key)
12
+ new_hash
13
+ end
14
+ end
15
+
16
+ def symbolize_keys
17
+ inject({}) do |options, (key, value)|
18
+ options[(key.to_sym rescue key) || key] = value
19
+ options
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,9 @@
1
+ class String
2
+ def camelize(first_letter_in_uppercase = true)
3
+ if first_letter_in_uppercase
4
+ self.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
5
+ else
6
+ self[0].chr.downcase + camelize[1..-1]
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,22 @@
1
+ require "cgi"
2
+ module MultiInfo
3
+ class API
4
+ API_SERVICE_HOST = 'https://www.multiinfo.plus.pl/'
5
+ API_NAME = 'smsapi3'
6
+ API_SCRIPT_TYPE = 'aspx'
7
+
8
+ class Command
9
+
10
+ def initialize(command_name)
11
+ @command_name = command_name
12
+ end
13
+
14
+ def with_params(param_hash)
15
+ param_string = '?' + param_hash.map { |key, value| "#{::CGI.escape(key.to_s.camelize(false))}=#{::CGI.escape(value.to_s)}" }.sort.join('&')
16
+ URI.parse(File.join("#{API_SERVICE_HOST}#{API_NAME}/", "#{@command_name}.#{API_SCRIPT_TYPE}" + param_string))
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ module MultiInfo
2
+ class API
3
+
4
+ # MultiInfo API Error exception.
5
+ class Error < StandardError
6
+ attr_reader :code, :message
7
+
8
+ def initialize(code, message)
9
+ @code, @message = code, message
10
+ end
11
+
12
+ # Creates a new Error from a MultiInfo HTTP response string
13
+ # e.g.:
14
+ #
15
+ # Error.parse("ERR: 001, Authentication error")
16
+ # # => #<MultiInfo::API::Error code='001' message='Authentication error'>
17
+ def self.parse(error_arr)
18
+ code, message = error_arr
19
+ self.new(code, message)
20
+ end
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,67 @@
1
+ require "rubygems"
2
+ require 'httpclient'
3
+
4
+
5
+ module MultiInfo
6
+ class API
7
+ DEFAULT_CERT_FILE = 'multiinfo.crt'
8
+ DEFAULT_KEY_FILE = 'multiinfo.pem'
9
+
10
+ class FakeHttpResponse
11
+ "test mode"
12
+ end
13
+
14
+ class Executor
15
+ def initialize(authentication_hash, certificate_hash, debug=false, test_mode=false)
16
+ @authentication_hash = authentication_hash
17
+ @client_cert_file = {
18
+ :cert => certificate_hash[:client_cert] || File.join(DEFAULT_CONFIG_PATH, DEFAULT_CERT_FILE),
19
+ :rsa_key => certificate_hash[:client_key] || File.join(DEFAULT_CONFIG_PATH, DEFAULT_KEY_FILE)
20
+ }
21
+ @debug = debug
22
+ @test_mode = test_mode
23
+
24
+ allow_request_recording if @test_mode
25
+ end
26
+
27
+ def execute(command_name, parameters={})
28
+ request_uri = command(command_name, parameters)
29
+ puts "[debug] Executing command '#{command_name}': #{request_uri}" if @debug
30
+ [command_name, get_response(request_uri)]
31
+ end
32
+
33
+ def in_test_mode?
34
+ @test_mode
35
+ end
36
+
37
+ protected
38
+
39
+ def command(command_name, parameters)
40
+ Command.new(command_name).with_params( parameters.merge(@authentication_hash) )
41
+ end
42
+
43
+ def get_response(uri)
44
+ if in_test_mode?
45
+ sms_requests << uri
46
+ FakeHttpResponse.new
47
+ else
48
+ clnt = HTTPClient.new
49
+ clnt.ssl_config.set_client_cert_file(@client_cert_file[:cert], @client_cert_file[:rsa_key])
50
+ clnt.get_content(uri)
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def allow_request_recording
57
+ class << self
58
+ define_method :sms_requests do
59
+ @sms_requests ||= []
60
+ end
61
+ end
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,66 @@
1
+ module MultiInfo
2
+ class API
3
+
4
+ # Used to parse HTTP responses returned from MultiInfo API calls.
5
+ class Response
6
+ class << self
7
+
8
+ # Returns the HTTP response body data as a hash.
9
+ def parse(response_arr)
10
+ return { :status => 'OK' } if API.test_mode
11
+ command, http_response = response_arr
12
+ response_rows = http_response.split("\r\n")
13
+ response_status, response_body = response_rows[0], response_rows[1..-1]
14
+ if response_status.to_i < 0
15
+ raise MultiInfo::API::Error.new(response_status, response_body.first)
16
+ else
17
+ response_hash(command, response_body).merge({:status => response_status})
18
+ end
19
+ end
20
+
21
+ private
22
+ def response_hash(command, response)
23
+ resp_hash = {}
24
+ API_RESPONSES[command].each_with_index { |message_key, i| resp_hash[message_key] = response[i] }
25
+ resp_hash
26
+ end
27
+
28
+ public
29
+ API_RESPONSES = {
30
+ # Hash keys for each response lines.
31
+ # Line no 1 is skipped as it always is 'status'
32
+
33
+ 'sendsms' => [
34
+ :sms_id # 2
35
+ ],
36
+
37
+ 'cancelsms' => [
38
+ :cancel_status # 2
39
+ ],
40
+
41
+ 'infosms' => [
42
+ :sms_id, # 2
43
+ :message_type, # 3
44
+ :message_body, # 4
45
+ :protocol_id, # 5
46
+ :coding_scheme, # 6
47
+ :service_id, # 7
48
+ :conector_id, # 8
49
+ :originator_sms_id, # 9
50
+ :priority, # 10
51
+ :send_date, # 11
52
+ :valid_to, # 12
53
+ :deliv_notif_request, # 13
54
+ :originator, # 14
55
+ :destination, # 15
56
+ :message_status, # 16
57
+ :status_date # 17
58
+ ]
59
+ }
60
+
61
+
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,103 @@
1
+ module MultiInfo
2
+ # This module provides the core implementation of the Clickatell
3
+ # HTTP service.
4
+ class API
5
+ # Defaults for config file location
6
+ DEFAULT_CONFIG_PATH = File.join(ENV['HOME'], 'multiinfo')
7
+ DEFAULT_CONFIG_FILE = 'multiinfo.yml'
8
+
9
+ attr_accessor :auth_options
10
+
11
+ class << self
12
+ # Set to true to enable debugging (off by default)
13
+ attr_accessor :debug_mode
14
+
15
+ # Set to true to test message sending; this will not actually send
16
+ # messages but will collect sent messages in a testable collection.
17
+ # (off by default)
18
+ attr_accessor :test_mode
19
+
20
+ # Load credentials from config file
21
+ def load_auth_options(config_location = nil)
22
+ config_file = File.open(config_location || File.join(DEFAULT_CONFIG_PATH, DEFAULT_CONFIG_FILE))
23
+ auth_options = YAML.load(config_file).symbolize_keys
24
+ raise MultiInfo::API::Error.new(-9999, 'Missing config params') if auth_options.only(:login, :password, :service_id).size != 3
25
+ auth_options
26
+ end
27
+
28
+ end
29
+ self.debug_mode = false
30
+ self.test_mode = false
31
+
32
+ # Creates a new API instance using the specified +auth_options+.
33
+ # +auth_options+ is a hash containing :username, :password, :service_id and
34
+ # optionally :client_cert, :client_key
35
+ #
36
+ def initialize(auth_options = nil)
37
+ @auth_options = auth_options || self.class.load_auth_options
38
+ end
39
+
40
+ # Sends a message +message_text+ to +recipient+. Recipient
41
+ # number should have an international dialing prefix
42
+ def send_message(recipient, message_text, opts={})
43
+ valid_options = opts.only(:valid_to, :deliv_notif_request).merge(service_id)
44
+ response = execute_command( 'sendsms', {:text => message_text, :dest => recipient}.merge(valid_options) )
45
+ parse_response(response)
46
+ end
47
+
48
+
49
+ # Returns the status of a message. Use sms ID returned
50
+ # from original send_message call.
51
+ def message_info(sms_id)
52
+ response = execute_command('infosms', :sms_id => sms_id)
53
+ parse_response(response)
54
+ end
55
+
56
+ # Returns the status of a message. Use sms ID returned
57
+ # from original send_message call.
58
+ def cancel_message(sms_id)
59
+ response = execute_command('cancelsms', :sms_id => sms_id)
60
+ parse_response(response)
61
+ end
62
+
63
+
64
+ def sms_requests #:nodoc:
65
+ @sms_requests ||= []
66
+ end
67
+
68
+ protected
69
+ def execute_command(command_name, parameters={}) #:nodoc:
70
+ executor = Executor.new(credentials, certificate, self.class.debug_mode, self.class.test_mode)
71
+ result = executor.execute(command_name, parameters)
72
+
73
+ (sms_requests << executor.sms_requests).flatten! if self.class.test_mode
74
+
75
+ result
76
+ end
77
+
78
+ def parse_response(raw_response) #:nodoc:
79
+ Response.parse(raw_response)
80
+ end
81
+
82
+ def credentials #:nodoc:
83
+ @credenitals ||= @auth_options.only(:login, :password)
84
+ end
85
+
86
+ def certificate #:nodoc:
87
+ @certificate ||= @auth_options.only(:client_cert, :client_key)
88
+ end
89
+
90
+ def service_id #:nodoc:
91
+ @service_id ||= @auth_options.only(:service_id)
92
+ end
93
+
94
+ end
95
+ end
96
+
97
+ %w( api/command
98
+ api/error
99
+ api/executor
100
+ api/response
101
+ ).each do |lib|
102
+ require File.join(File.dirname(__FILE__), lib)
103
+ end
@@ -0,0 +1,13 @@
1
+ module MultiInfo #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ TINY = 1
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+
9
+ def self.to_s
10
+ STRING
11
+ end
12
+ end
13
+ end
data/lib/multiinfo.rb ADDED
@@ -0,0 +1,8 @@
1
+ module MultiInfo end
2
+
3
+ %w( core-ext/hash
4
+ core-ext/string
5
+ multiinfo/api
6
+ ).each do |lib|
7
+ require File.join(File.dirname(__FILE__), lib)
8
+ end
data/multiinfo.gemspec ADDED
@@ -0,0 +1,50 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{multiinfo}
5
+ s.version = "0.0.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Josh Nichols"]
9
+ s.date = %q{2009-07-28}
10
+ s.description = %q{This gem is Ruby interface to the Polkomtel's MultiInfo SMS gateway service, based on Luke Redpath's clickatel library. To use this gem, you will need an access to Polkomtel's MultiInfo service. You need user, password, sercvice_id and certificate for ssl connection. Contact Polkomtel sales reps for info how to obtain acces to MultiInfo service.}
11
+ s.email = %q{luka@weblify.pl}
12
+ s.extra_rdoc_files = [
13
+ "README.txt"
14
+ ]
15
+ s.files = [
16
+ ".gitignore",
17
+ "History.txt",
18
+ "License.txt",
19
+ "RDOC_README.txt",
20
+ "README.txt",
21
+ "VERSION",
22
+ "config_example.yml",
23
+ "lib/core-ext/hash.rb",
24
+ "lib/core-ext/string.rb",
25
+ "lib/multiinfo.rb",
26
+ "lib/multiinfo/api.rb",
27
+ "lib/multiinfo/api/command.rb",
28
+ "lib/multiinfo/api/error.rb",
29
+ "lib/multiinfo/api/executor.rb",
30
+ "lib/multiinfo/api/response.rb",
31
+ "lib/multiinfo/version.rb",
32
+ "multiinfo.gemspec"
33
+ ]
34
+ s.has_rdoc = true
35
+ s.homepage = %q{http://github.com/luka/multiinfo}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.1}
39
+ s.summary = %q{Ruby interface to the Polkomtel's MultiInfo SMS gateway service, based on Luke Redpath's clickatel library}
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 2
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ else
47
+ end
48
+ else
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: luka-multiinfo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Josh Nichols
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-28 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: This gem is Ruby interface to the Polkomtel's MultiInfo SMS gateway service, based on Luke Redpath's clickatel library. To use this gem, you will need an access to Polkomtel's MultiInfo service. You need user, password, sercvice_id and certificate for ssl connection. Contact Polkomtel sales reps for info how to obtain acces to MultiInfo service.
17
+ email: luka@weblify.pl
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.txt
24
+ files:
25
+ - .gitignore
26
+ - History.txt
27
+ - License.txt
28
+ - RDOC_README.txt
29
+ - README.txt
30
+ - VERSION
31
+ - config_example.yml
32
+ - lib/core-ext/hash.rb
33
+ - lib/core-ext/string.rb
34
+ - lib/multiinfo.rb
35
+ - lib/multiinfo/api.rb
36
+ - lib/multiinfo/api/command.rb
37
+ - lib/multiinfo/api/error.rb
38
+ - lib/multiinfo/api/executor.rb
39
+ - lib/multiinfo/api/response.rb
40
+ - lib/multiinfo/version.rb
41
+ - multiinfo.gemspec
42
+ has_rdoc: true
43
+ homepage: http://github.com/luka/multiinfo
44
+ licenses:
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --charset=UTF-8
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ version:
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ requirements: []
63
+
64
+ rubyforge_project:
65
+ rubygems_version: 1.3.5
66
+ signing_key:
67
+ specification_version: 2
68
+ summary: Ruby interface to the Polkomtel's MultiInfo SMS gateway service, based on Luke Redpath's clickatel library
69
+ test_files: []
70
+