exetel 0.0.1

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 jkongie
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/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = exetel
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 jkongie. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "exetel"
8
+ gem.summary = "Ruby API wrapper for the Exetel SMS Service"
9
+ gem.description = "This gem wraps the SMS service provided by Exetel. You are required to have an Exetel account to use this service - http://www.exetel.com.au"
10
+ gem.email = "jkongie@gmail.com"
11
+ gem.homepage = "http://github.com/jkongie/exetel"
12
+ gem.authors = ["James Kong"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "exetel #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/exetel.gemspec ADDED
@@ -0,0 +1,61 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{exetel}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["James Kong"]
12
+ s.date = %q{2010-05-10}
13
+ s.description = %q{This gem wraps the SMS service provided by Exetel. You are required to have an Exetel account to use this service - http://www.exetel.com.au}
14
+ s.email = %q{jkongie@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "exetel.gemspec",
27
+ "lib/exetel.rb",
28
+ "lib/exetel/api.rb",
29
+ "lib/exetel/api/command.rb",
30
+ "lib/exetel/api/command_executor.rb",
31
+ "lib/exetel/api/error.rb",
32
+ "lib/exetel/api/message_status.rb",
33
+ "lib/exetel/response.rb",
34
+ "lib/exetel/response/type_keys.rb",
35
+ "test/helper.rb",
36
+ "test/test_exetel.rb"
37
+ ]
38
+ s.homepage = %q{http://github.com/jkongie/exetel}
39
+ s.rdoc_options = ["--charset=UTF-8"]
40
+ s.require_paths = ["lib"]
41
+ s.rubygems_version = %q{1.3.6}
42
+ s.summary = %q{Ruby API wrapper for the Exetel SMS Service}
43
+ s.test_files = [
44
+ "test/helper.rb",
45
+ "test/test_exetel.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
53
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
54
+ else
55
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
59
+ end
60
+ end
61
+
data/lib/exetel.rb ADDED
@@ -0,0 +1,8 @@
1
+ module Exetel end
2
+
3
+ %w(
4
+ exetel/api
5
+ exetel/response
6
+ ).each do |lib|
7
+ require File.join(File.dirname(__FILE__), lib)
8
+ end
data/lib/exetel/api.rb ADDED
@@ -0,0 +1,49 @@
1
+ module Exetel
2
+ # This module provides the core implementation of the Exetel SMS HTTP Service
3
+ class API
4
+ attr_accessor :auth_options
5
+
6
+ def initialize(auth_options={})
7
+ @auth_options = auth_options
8
+ end
9
+
10
+ # Sends a message +message_text+ to +recipient+.
11
+ #
12
+ # Additional options:
13
+ # :sender - the from number/name(default="")
14
+ # :msg_type - The encoding type of message(default=text)
15
+ # :ref_number - An optional reference number for local tracking
16
+ # Returns a new message ID if successful.
17
+ def send_message(recipient, message_text, opts={})
18
+ valid_options = { :sender => "", :messagetype => "Text" }
19
+ valid_options.merge!(:sender => opts[:sender]) if opts[:sender]
20
+ valid_options.merge!(:messagetype => opts[:msg_type]) if opts[:msg_type]
21
+ valid_options.merge!(:referencenumber => opts[:ref_number]) if opts[:ref_number]
22
+ recipient = recipient.join(",")if recipient.is_a?(Array)
23
+ response = execute_command('sms',
24
+ { :mobilenumber => recipient, :message => message_text}.merge(valid_options)
25
+ )
26
+ response = parse_response(:sms, response)
27
+ response.is_a?(Array) ? response.map { |r| r[:status] } : response[:status]
28
+ end
29
+
30
+ # Requests for the amount of credit remaining
31
+ #
32
+ # Returns a response hash
33
+ def credit_check
34
+ response = execute_command('sms_credit')
35
+ response = parse_response(:credit_check, response)
36
+ end
37
+
38
+ protected
39
+ def execute_command(command_name, parameters={}) #:nodoc:
40
+ executor = CommandExecutor.new(auth_options, true)
41
+ result = executor.execute(command_name, parameters)
42
+ end
43
+
44
+ def parse_response(response_type, raw_response) #:nodoc:
45
+ Exetel::Response.parse(response_type, raw_response)
46
+ end
47
+ end
48
+ end
49
+
@@ -0,0 +1,35 @@
1
+ require 'cgi'
2
+ module Exetel
3
+ class API
4
+ # Represents a Exetel HTTP gateway command in the form
5
+ # of a complete URL (the raw, low-level request).
6
+ class Command
7
+ API_SERVICE_HOST = 'smsgw.exetel.com.au'
8
+
9
+ def initialize(command_name, opts = {})
10
+ @command_name = command_name
11
+ @options = opts
12
+ end
13
+
14
+ # Returns a URL for the given parameters (a hash).
15
+ def with_params(param_hash)
16
+ param_string = '?' + param_hash.map { |key, value| "#{::CGI.escape(key.to_s)}=#{::CGI.escape(value.to_s)}" }.sort.join('&')
17
+ return URI.parse(File.join(api_service_uri, command_uri + param_string))
18
+ end
19
+
20
+ protected
21
+
22
+ # Return the api service uri
23
+ # - TODO - Allow overriding of the default service host
24
+ def api_service_uri
25
+ api_service_host = API_SERVICE_HOST
26
+ return "https://#{api_service_host}/sendsms/"
27
+ end
28
+
29
+ # Return the command part of the uri
30
+ def command_uri
31
+ return "/api_#{@command_name}.php"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,37 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+
4
+ module Exetel
5
+ class API
6
+
7
+ class CommandExecutor
8
+
9
+ def initialize(authentication_hash, debug=false)
10
+ @authentication_hash = authentication_hash
11
+ @debug = debug
12
+ end
13
+
14
+ # Builds a command object and sends it using HTTP GET.
15
+ # Will output URLs as they are requested to stdout when
16
+ # debugging is enabled.
17
+ def execute(command_name, parameters)
18
+ request_uri = command(command_name, parameters)
19
+ puts "[debug] Sending request to #{request_uri}" if @debug
20
+ get_response(request_uri).first
21
+ end
22
+
23
+ protected
24
+ def command(command_name, parameters)
25
+ Command.new(command_name).with_params(parameters.merge(@authentication_hash))
26
+ end
27
+
28
+ def get_response(uri)
29
+ http = Net::HTTP.new(uri.host, uri.port)
30
+ http.use_ssl = (uri.scheme == 'https')
31
+ http.start do |http|
32
+ resp, body = http.get([uri.path, uri.query].join('?'))
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,15 @@
1
+ module Exetel
2
+ class API
3
+
4
+ # Exetel API Error exception
5
+ class Error < StandardError
6
+ attr_reader :status, :message
7
+
8
+ def initialize(response)
9
+ @status = Exetel::API::MessageStatus[response[:status]]
10
+ @message = response[:exetel_notes]
11
+ puts "Error: #{@status} => #{@message}"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Exetel
2
+ class API
3
+ class MessageStatus
4
+ STATUS_MAP = {
5
+ 0 => "SMS Request Rejected",
6
+ 1 => "Sent",
7
+ 2 => "Failed"
8
+ }
9
+
10
+ def self.[](code)
11
+ STATUS_MAP[code.to_i]
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ module Exetel
2
+
3
+ # Used to parse HTTP responses returned from Exetel API calls.
4
+ class Response
5
+
6
+ class << self
7
+
8
+ # Returns the HTTP response body data as a hash.
9
+ # - TODO - CLean this up
10
+ def parse(response_type, http_response)
11
+ response_keys = Exetel::Response::TypeKeys[response_type]
12
+ body = http_response.body.gsub("\n", '')
13
+ response = Hash[*response_keys.zip(body.split("|")).flatten]
14
+ raise Exetel::API::Error.new(response) if response[:status] == "0"
15
+ response
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ module Exetel
2
+ class Response
3
+ class TypeKeys
4
+ TYPE_MAP = {
5
+ :sms => [:status, :number, :reference_number, :exetel_id, :exetel_notes],
6
+ :credit_check => [:status, :credit_limit, :exetel_notes]
7
+ }
8
+ def self.[](type)
9
+ TYPE_MAP[type]
10
+ end
11
+ end
12
+ end
13
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'exetel'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestExetel < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: exetel
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - James Kong
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-10 00:00:00 +10:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: thoughtbot-shoulda
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ description: This gem wraps the SMS service provided by Exetel. You are required to have an Exetel account to use this service - http://www.exetel.com.au
33
+ email: jkongie@gmail.com
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files:
39
+ - LICENSE
40
+ - README.rdoc
41
+ files:
42
+ - .document
43
+ - .gitignore
44
+ - LICENSE
45
+ - README.rdoc
46
+ - Rakefile
47
+ - VERSION
48
+ - exetel.gemspec
49
+ - lib/exetel.rb
50
+ - lib/exetel/api.rb
51
+ - lib/exetel/api/command.rb
52
+ - lib/exetel/api/command_executor.rb
53
+ - lib/exetel/api/error.rb
54
+ - lib/exetel/api/message_status.rb
55
+ - lib/exetel/response.rb
56
+ - lib/exetel/response/type_keys.rb
57
+ - test/helper.rb
58
+ - test/test_exetel.rb
59
+ has_rdoc: true
60
+ homepage: http://github.com/jkongie/exetel
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --charset=UTF-8
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ requirements: []
83
+
84
+ rubyforge_project:
85
+ rubygems_version: 1.3.6
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Ruby API wrapper for the Exetel SMS Service
89
+ test_files:
90
+ - test/helper.rb
91
+ - test/test_exetel.rb