Created
August 10, 2010 06:37
-
-
Save toolmantim/516805 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# License | |
# | |
# Copyright (c) 2010 Tim Lucas <[email protected]> | |
# | |
# Permission to use, copy, modify, and/or distribute this software for any | |
# purpose with or without fee is hereby granted, provided that the above | |
# copyright notice and this permission notice appear in all copies. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
require 'net/https' | |
# A simple API wrapper for DirectSMS's HTTPS API (http://directsms.com.au/info/api/http.html) | |
class DirectSMS | |
class UnknownError < StandardError; end | |
class ErrorResponse < StandardError; end | |
def initialize(username, password) | |
@connection_id = post '/connect', :username => username, :password => password | |
end | |
def send(message, to, from) | |
post '/send_branded_message', :connectionid => @connection_id, | |
:message => message, | |
:to => Array(to).join(","), | |
:senderid => from | |
end | |
private | |
def post(path, params={}) | |
http = Net::HTTP.new('api.directsms.com.au', 443) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE # Heroku needed this :( | |
http.start do |http| | |
req = Net::HTTP::Post.new("/s3/https#{path}") | |
req.set_form_data(params) | |
resp, body = http.request(req) | |
raise UnknownError.new(body) unless resp.is_a?(Net::HTTPSuccess) | |
return parse(body) | |
end | |
end | |
def parse(body) | |
match = body.match(/(.*?):(.*)/) | |
raise UnknownError.new(body) if !match | |
type, content = body.match(/(.*?): (.*)/).captures | |
raise ErrorResponse.new(content) if type != "id" | |
content.strip | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Tim,
Thanks very much for sharing this code. I've tweaked it to work with the current directSMS API. See https://gist.github.com/keithpitty/5947010.
Keith