clickatell 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ == 0.1.0 2007-08-17
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Luke Redpath
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,29 @@
1
+ History.txt
2
+ License.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ bin/sms
7
+ lib/clickatell.rb
8
+ lib/clickatell/api.rb
9
+ lib/clickatell/connection.rb
10
+ lib/clickatell/response.rb
11
+ lib/clickatell/utility.rb
12
+ lib/clickatell/utility/options.rb
13
+ lib/clickatell/version.rb
14
+ scripts/txt2html
15
+ setup.rb
16
+ spec/api_spec.rb
17
+ spec/connection_spec.rb
18
+ spec/response_spec.rb
19
+ spec/spec.opts
20
+ spec/spec_helper.rb
21
+ website/images/footer_bg.gif
22
+ website/index.html
23
+ website/index.txt
24
+ website/javascripts/codehighlighter/code_highlighter.js
25
+ website/javascripts/codehighlighter/ruby.js
26
+ website/javascripts/rounded_corners_lite.inc.js
27
+ website/stylesheets/limechoc.css
28
+ website/stylesheets/screen.css
29
+ website/template.rhtml
@@ -0,0 +1,36 @@
1
+ == Basic Usage
2
+
3
+ To use this gem, you will need sign up for an account at www.clickatell.com.
4
+ Once you are registered and logged into your account centre, you should add
5
+ an HTTP API connection to your account. This will give you your API_ID.
6
+
7
+ You can now use the library directly. You will need your API_ID as well as your
8
+ account username and password.
9
+
10
+ require 'rubygems'
11
+ require 'clickatell'
12
+
13
+ connection = Clickatell::Connection.new(
14
+ 'your_api_id',
15
+ 'your_username',
16
+ 'your_password'
17
+ )
18
+ connection.send_message('447771234567', 'Hello from clickatell')
19
+
20
+
21
+ == Command-line SMS Utility
22
+
23
+ The Clickatell gem also comes with a command-line utility that will allow you
24
+ to send an SMS directly from the command-line.
25
+
26
+ You will need to create a YAML configuration file in your home directory, in a
27
+ file called .clickatell that resembles the following:
28
+
29
+ # ~/.clickatell
30
+ api_key: your_api_id
31
+ username: your_username
32
+ password: your_password
33
+
34
+ You can then use the sms utility to send a message to a single recipient:
35
+
36
+ sms 447771234567 'Hello from clickatell'
@@ -0,0 +1,145 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/clean'
4
+ require 'rake/testtask'
5
+ require 'rake/packagetask'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/rdoctask'
8
+ require 'rake/contrib/rubyforgepublisher'
9
+ require 'fileutils'
10
+ require 'hoe'
11
+ begin
12
+ require 'spec/rake/spectask'
13
+ rescue LoadError
14
+ puts 'To use rspec for testing you must install rspec gem:'
15
+ puts '$ sudo gem install rspec'
16
+ exit
17
+ end
18
+
19
+ include FileUtils
20
+ require File.join(File.dirname(__FILE__), 'lib', 'clickatell', 'version')
21
+
22
+ AUTHOR = 'Luke Redpath'
23
+ EMAIL = "contact[AT]lukeredpath.co.uk"
24
+ DESCRIPTION = "Ruby interface to the Clickatell SMS gateway service."
25
+ GEM_NAME = 'clickatell'
26
+
27
+ @config_file = "~/.rubyforge/user-config.yml"
28
+ @config = nil
29
+ def rubyforge_username
30
+ unless @config
31
+ begin
32
+ @config = YAML.load(File.read(File.expand_path(@config_file)))
33
+ rescue
34
+ puts <<-EOS
35
+ ERROR: No rubyforge config file found: #{@config_file}"
36
+ Run 'rubyforge setup' to prepare your env for access to Rubyforge
37
+ - See http://newgem.rubyforge.org/rubyforge.html for more details
38
+ EOS
39
+ exit
40
+ end
41
+ end
42
+ @rubyforge_username ||= @config["username"]
43
+ end
44
+
45
+ RUBYFORGE_PROJECT = 'clickatell' # The unix name for your project
46
+ HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"
47
+ DOWNLOAD_PATH = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
48
+
49
+ NAME = "clickatell"
50
+ REV = nil
51
+ # UNCOMMENT IF REQUIRED:
52
+ # REV = `svn info`.each {|line| if line =~ /^Revision:/ then k,v = line.split(': '); break v.chomp; else next; end} rescue nil
53
+ VERS = Clickatell::VERSION::STRING + (REV ? ".#{REV}" : "")
54
+ CLEAN.include ['**/.*.sw?', '*.gem', '.config', '**/.DS_Store']
55
+ RDOC_OPTS = ['--quiet', '--title', 'clickatell documentation',
56
+ "--opname", "index.html",
57
+ "--line-numbers",
58
+ "--main", "README",
59
+ "--inline-source"]
60
+
61
+ class Hoe
62
+ def extra_deps
63
+ @extra_deps.reject { |x| Array(x).first == 'hoe' }
64
+ end
65
+ end
66
+
67
+ # Generate all the Rake tasks
68
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
69
+ hoe = Hoe.new(GEM_NAME, VERS) do |p|
70
+ p.author = AUTHOR
71
+ p.description = DESCRIPTION
72
+ p.email = EMAIL
73
+ p.summary = DESCRIPTION
74
+ p.url = HOMEPATH
75
+ p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
76
+ p.test_globs = ["test/**/test_*.rb"]
77
+ p.clean_globs |= CLEAN #An array of file patterns to delete on clean.
78
+
79
+ # == Optional
80
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
81
+ #p.extra_deps = [] # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
82
+ #p.spec_extras = {} # A hash of extra values to set in the gemspec.
83
+ end
84
+
85
+ CHANGES = hoe.paragraphs_of('History.txt', 0..1).join("\n\n")
86
+ PATH = (RUBYFORGE_PROJECT == GEM_NAME) ? RUBYFORGE_PROJECT : "#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
87
+ hoe.remote_rdoc_dir = File.join(PATH.gsub(/^#{RUBYFORGE_PROJECT}\/?/,''), 'rdoc')
88
+
89
+ desc 'Generate website files'
90
+ task :website_generate do
91
+ Dir['website/**/*.txt'].each do |txt|
92
+ sh %{ ruby scripts/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
93
+ end
94
+ end
95
+
96
+ desc 'Upload website files to rubyforge'
97
+ task :website_upload do
98
+ host = "#{rubyforge_username}@rubyforge.org"
99
+ remote_dir = "/var/www/gforge-projects/#{PATH}/"
100
+ local_dir = 'website'
101
+ sh %{rsync -aCv #{local_dir}/ #{host}:#{remote_dir}}
102
+ end
103
+
104
+ desc 'Generate and upload website files'
105
+ task :website => [:website_generate, :website_upload, :publish_docs]
106
+
107
+ desc 'Release the website and new gem version'
108
+ task :deploy => [:check_version, :website, :release] do
109
+ puts "Remember to create SVN tag:"
110
+ puts "svn copy svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/trunk " +
111
+ "svn+ssh://#{rubyforge_username}@rubyforge.org/var/svn/#{PATH}/tags/REL-#{VERS} "
112
+ puts "Suggested comment:"
113
+ puts "Tagging release #{CHANGES}"
114
+ end
115
+
116
+ desc 'Runs tasks website_generate and install_gem as a local deployment of the gem'
117
+ task :local_deploy => [:website_generate, :install_gem]
118
+
119
+ task :check_version do
120
+ unless ENV['VERSION']
121
+ puts 'Must pass a VERSION=x.y.z release version'
122
+ exit
123
+ end
124
+ unless ENV['VERSION'] == VERS
125
+ puts "Please update your version.rb to match the release version, currently #{VERS}"
126
+ exit
127
+ end
128
+ end
129
+
130
+ namespace :spec do
131
+ desc "Run the specs under spec"
132
+ Spec::Rake::SpecTask.new('all') do |t|
133
+ t.spec_opts = ['--options', "spec/spec.opts"]
134
+ t.spec_files = FileList['spec/*_spec.rb']
135
+ end
136
+
137
+ desc "Run the specs under spec in specdoc format"
138
+ Spec::Rake::SpecTask.new('doc') do |t|
139
+ t.spec_opts = ['--format', "specdoc"]
140
+ t.spec_files = FileList['spec/*_spec.rb']
141
+ end
142
+ end
143
+
144
+ desc "Default task is to run specs"
145
+ task :default => 'spec:all'
data/bin/sms ADDED
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pp'
3
+
4
+ local_libs = [
5
+ File.join(File.dirname(__FILE__), *%w[../lib/clickatell]),
6
+ File.join(File.dirname(__FILE__), *%w[../lib/clickatell/utility])
7
+ ]
8
+
9
+ if File.exist?(local_libs.first)
10
+ local_libs.each { |lib| require lib }
11
+ else
12
+ require 'rubygems'
13
+ require 'clickatell'
14
+ require 'clickatell/utility'
15
+ end
16
+
17
+ options = Clickatell::Utility::Options.parse(ARGV)
18
+ connection = Clickatell::Connection.new(options.api_key, options.username, options.password)
19
+
20
+ puts "Sending '#{options.message}' to #{options.recipient}."
21
+ connection.send_message(options.recipient, options.message)
22
+ puts "Done."
23
+ exit 0
@@ -0,0 +1,7 @@
1
+ module Clickatell
2
+ end
3
+
4
+ require File.join(File.dirname(__FILE__), *%w[clickatell/version])
5
+ require File.join(File.dirname(__FILE__), *%w[clickatell/api])
6
+ require File.join(File.dirname(__FILE__), *%w[clickatell/response])
7
+ require File.join(File.dirname(__FILE__), *%w[clickatell/connection])
@@ -0,0 +1,108 @@
1
+ require 'net/http'
2
+
3
+ module Clickatell
4
+ # This module provides the core implementation of the Clickatell
5
+ # HTTP service.
6
+ module API
7
+
8
+ class << self
9
+
10
+ # Authenticates using the specified credentials. Returns
11
+ # a session_id if successful which can be used in subsequent
12
+ # API calls.
13
+ def authenticate(api_id, username, password)
14
+ response = execute_command('auth',
15
+ :api_id => api_id,
16
+ :user => username,
17
+ :password => password
18
+ )
19
+ parse_response(response)['OK']
20
+ end
21
+
22
+ # Pings the service with the specified session_id to keep the
23
+ # session alive.
24
+ def ping(session_id)
25
+ execute_command('ping', :session_id => session_id)
26
+ end
27
+
28
+ # Sends a message +message_text+ to +recipient+. Recipient
29
+ # number should have an international dialing prefix and
30
+ # no leading zeros (unless you have set a default prefix
31
+ # in your clickatell account centre).
32
+ #
33
+ # +auth_options+: a hash of credentials to be used in this
34
+ # API call. Either api_id/username/password or session_id
35
+ # for an existing authenticated session.
36
+ #
37
+ # Returns a new message ID if successful.
38
+ def send_message(recipient, message_text, auth_options)
39
+ response = execute_command('sendmsg', {
40
+ :to => recipient,
41
+ :text => message_text
42
+ }.merge( auth_hash(auth_options) ))
43
+ parse_response(response)['ID']
44
+ end
45
+
46
+ # Returns the status of a message. Use message ID returned
47
+ # from original send_message call. See send_message() for
48
+ # auth_options.
49
+ def message_status(message_id, auth_options)
50
+ response = execute_command('querymsg', {
51
+ :apimsgid => message_id
52
+ }.merge( auth_hash(auth_options) ))
53
+ parse_response(response)['Status']
54
+ end
55
+
56
+ protected
57
+ # Builds a command and sends it via HTTP GET.
58
+ def execute_command(command_name, parameters)
59
+ Net::HTTP.get_response(
60
+ Command.new(command_name).with_params(parameters)
61
+ )
62
+ end
63
+
64
+ def parse_response(raw_response) #:nodoc:
65
+ Clickatell::Response.parse(raw_response)
66
+ end
67
+
68
+ def auth_hash(options) #:nodoc:
69
+ if options[:session_id]
70
+ return {
71
+ :session_id => options[:session_id]
72
+ }
73
+ else
74
+ return {
75
+ :user => options[:username],
76
+ :password => options[:password],
77
+ :api_id => options[:api_key]
78
+ }
79
+ end
80
+ end
81
+
82
+ end
83
+
84
+ # Represents a Clickatell HTTP gateway command in the form
85
+ # of a complete URL (the raw, low-level request).
86
+ class Command
87
+ API_SERVICE_HOST = 'api.clickatell.com'
88
+
89
+ def initialize(command_name, opts={})
90
+ @command_name = command_name
91
+ @options = { :secure => false }.merge(opts)
92
+ end
93
+
94
+ # Returns a URL for the given parameters (a hash).
95
+ def with_params(param_hash)
96
+ param_string = '?' + param_hash.map { |key, value| "#{key}=#{value}" }.sort.join('&')
97
+ return URI.parse(File.join(api_service_uri, @command_name + URI.encode(param_string)))
98
+ end
99
+
100
+ protected
101
+ def api_service_uri
102
+ protocol = @options[:secure] ? 'https' : 'http'
103
+ return "#{protocol}://#{API_SERVICE_HOST}/http/"
104
+ end
105
+ end
106
+
107
+ end
108
+ end
@@ -0,0 +1,60 @@
1
+ module Clickatell
2
+
3
+ # The Connection object provides a high-level interface to the
4
+ # Clickatell API, handling authentication across multiple API
5
+ # requests.
6
+ #
7
+ # Example:
8
+ # conn = Clickatell::Connection.new('your_api_key', 'joebloggs', 'secret')
9
+ # message_id = conn.send_message('44777123456', 'This is a test message')
10
+ # status = conn.message_status(message_id)
11
+ #
12
+ # The Connection object provides all of the public methods implemented in
13
+ # Clickatell::API but handles the passing of authentication options
14
+ # automatically so it is not required to pass any authentication_option hashes
15
+ # into API methods that require them.
16
+ class Connection
17
+
18
+ # Returns the current Clickatell session ID.
19
+ attr_reader :session_id
20
+
21
+ # +api_key+: Your Clickatell API ID/key.
22
+ # +username+: Your Clickatell account username.
23
+ # +password+: Your Clickatell account password.
24
+ def initialize(api_key, username, password)
25
+ @api_key = api_key
26
+ @username = username
27
+ @password = password
28
+ end
29
+
30
+ # Manual authentication. Will create a new Clickatell session
31
+ # and store the session ID.
32
+ def authenticate!
33
+ @session_id = API.authenticate(@api_key, @username, @password)
34
+ end
35
+
36
+ protected
37
+ # Executes the given +api_method+ by delegating to the API
38
+ # module, using the current session_id for authentication.
39
+ def execute_api_call(api_method, params)
40
+ params << {:session_id => current_session_id}
41
+ API.send(api_method, *params)
42
+ end
43
+
44
+ # Returns the current_session_id, authenticating if one doesn't exist
45
+ def current_session_id
46
+ authenticate! if session_id.nil?
47
+ session_id
48
+ end
49
+
50
+ # Dispatch any API methods to the API module.
51
+ def method_missing(method, *args, &block)
52
+ if API.respond_to?(method)
53
+ execute_api_call(method, args)
54
+ else
55
+ super(method, args, &block)
56
+ end
57
+ end
58
+ end
59
+
60
+ end
@@ -0,0 +1,19 @@
1
+ require 'yaml'
2
+
3
+ module Clickatell
4
+
5
+ # Used to parse HTTP responses returned from Clickatell API calls.
6
+ class Response
7
+
8
+ class << self
9
+ PARSE_REGEX = /[A-Za-z0-9]+:.*?(?:(?=[A-Za-z0-9]+:)|$)/
10
+
11
+ # Returns the HTTP response body data as a hash.
12
+ def parse(http_response)
13
+ YAML.load(http_response.body.scan(PARSE_REGEX).join("\n"))
14
+ end
15
+
16
+ end
17
+
18
+ end
19
+ end