mediaburst 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/ISC-LICENSE +13 -0
- data/README.md +63 -0
- data/lib/mediaburst/api.rb +99 -0
- data/lib/mediaburst/version.rb +3 -0
- data/lib/mediaburst.rb +8 -0
- metadata +98 -0
data/ISC-LICENSE
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Copyright (c) 2011, Matthew Hall <matt@codebeef.com>
|
2
|
+
|
3
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
4
|
+
purpose with or without fee is hereby granted, provided that the above
|
5
|
+
copyright notice and this permission notice appear in all copies.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
8
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
9
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
10
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
11
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
12
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
13
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Mediaburst API Gem
|
2
|
+
|
3
|
+
The Mediaburst Gem is the fastest way of integrating the [Mediaburst API][2] into your own ruby applications.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install the gem as normal:
|
8
|
+
|
9
|
+
sudo gem install mediaburst
|
10
|
+
|
11
|
+
The Mediaburst gem also relies on [Nokogiri][1] for XML parsing.
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
Create an instance of the Mediaburst client:
|
16
|
+
|
17
|
+
client = Mediaburst::API.new('username', 'password')
|
18
|
+
|
19
|
+
Send an SMS:
|
20
|
+
|
21
|
+
response = client.send_message('441234567890', 'My test message')
|
22
|
+
|
23
|
+
Send a message to several numbers:
|
24
|
+
|
25
|
+
response = client.send_message(['441234567890', '441234567891', '441234567892'], 'My test message')
|
26
|
+
|
27
|
+
Send a long message to several numbers using the [concat option][8]:
|
28
|
+
|
29
|
+
options = {
|
30
|
+
:concat => 3
|
31
|
+
}
|
32
|
+
|
33
|
+
response = client.send_message(['441234567890', '441234567891', '441234567892', '44'], SOME_LONG_MESSAGE, options)
|
34
|
+
|
35
|
+
Check the response for errors:
|
36
|
+
|
37
|
+
y response
|
38
|
+
=> ---
|
39
|
+
"441234567890": true
|
40
|
+
"441234567891": true
|
41
|
+
"441234567892": true
|
42
|
+
"44": 10
|
43
|
+
|
44
|
+
On error, the value of the key referenced by the number will equal the [error code][5] returned from Mediaburst.
|
45
|
+
|
46
|
+
|
47
|
+
## Development
|
48
|
+
|
49
|
+
The source for the gem is [hosted on GitHub][3] for you to peruse, fork and contribute to.
|
50
|
+
|
51
|
+
To enable us to easily manage your contributions, please submit your pull requests as single commits that include tests to show your changes working as normal. Please do not change the version number - we'll take care of that as we merge your changes.
|
52
|
+
|
53
|
+
## License
|
54
|
+
|
55
|
+
Copyright © 2011 [Matthew Hall][4], released under the ISC license. All trademarks and IP remain the property of their respective owners.
|
56
|
+
|
57
|
+
|
58
|
+
[1]:http://nokogiri.org/
|
59
|
+
[2]:http://www.mediaburst.co.uk/api
|
60
|
+
[3]:https://github.com/mediaburst/ruby-mediaburst-sms
|
61
|
+
[4]:http://codebeef.com/portfolio/mediaburst
|
62
|
+
[5]:http://www.mediaburst.co.uk/api/reference/error-codes/
|
63
|
+
[8]:http://www.mediaburst.co.uk/api/sending-a-message/parameters/#param-concat
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
module Mediaburst
|
5
|
+
ENDPOINT = 'http://sms.message-platform.com/xml/send.aspx'
|
6
|
+
|
7
|
+
# Thrown when an invalid request is made,
|
8
|
+
# e.g invalid credentials were used
|
9
|
+
class InvalidRequest < Exception
|
10
|
+
end
|
11
|
+
|
12
|
+
# Thrown when we don't get a succesful response
|
13
|
+
# from the mediaburst server
|
14
|
+
class ServerError < Exception
|
15
|
+
end
|
16
|
+
|
17
|
+
class API
|
18
|
+
def initialize(u, p)
|
19
|
+
@auth = {:username => u, :password => p}
|
20
|
+
end
|
21
|
+
|
22
|
+
# Takes a number or array of numbers and a content string
|
23
|
+
# and sends to the mediaburst SMS API endpoint.
|
24
|
+
#
|
25
|
+
# numbers - a string or array of strings
|
26
|
+
# content - the string to send
|
27
|
+
# options - a hash of options to send to the API
|
28
|
+
#
|
29
|
+
# Returns a hash in the format:
|
30
|
+
# "phone number" => true on success or an error number on failure
|
31
|
+
def send_message(numbers, content, options ={})
|
32
|
+
numbers = [numbers] unless numbers.class == Array
|
33
|
+
self.process_response(self.send_request(self.create_xml(numbers, content, options)))
|
34
|
+
end
|
35
|
+
|
36
|
+
# Get the xml for the request
|
37
|
+
def create_xml(numbers, content, options)
|
38
|
+
# Note that the username and password should be the first elements passed
|
39
|
+
builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
|
40
|
+
xml.message {
|
41
|
+
xml.Username @auth[:username]
|
42
|
+
xml.Password @auth[:password]
|
43
|
+
|
44
|
+
options.each do |k, v|
|
45
|
+
xml.send(k.to_s, v)
|
46
|
+
end
|
47
|
+
|
48
|
+
numbers.each do |number|
|
49
|
+
xml.SMS {
|
50
|
+
xml.To number
|
51
|
+
xml.Content content
|
52
|
+
}
|
53
|
+
end
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
builder.to_xml
|
58
|
+
end
|
59
|
+
|
60
|
+
# Send a request to the endpoint
|
61
|
+
def send_request(request_body)
|
62
|
+
# Create and send the request
|
63
|
+
uri = URI.parse(ENDPOINT)
|
64
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
65
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
66
|
+
request.body = request_body
|
67
|
+
request["Content-Type"] = "text/xml"
|
68
|
+
|
69
|
+
http.request(request)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Process the received response
|
73
|
+
def process_response(response)
|
74
|
+
# Make sure we get a successful response
|
75
|
+
case response
|
76
|
+
when Net::HTTPSuccess
|
77
|
+
# Parse the response
|
78
|
+
response_xml = Nokogiri::XML(response.body)
|
79
|
+
|
80
|
+
if response_xml.xpath('//SMS_Resp').empty?
|
81
|
+
raise Mediaburst::InvalidRequest, "ERROR: #{response_xml.xpath('//ErrDesc').inner_text}"
|
82
|
+
else
|
83
|
+
responses = {}
|
84
|
+
response_xml.xpath('//SMS_Resp').each do |sms_resp|
|
85
|
+
if sms_resp.xpath('ErrDesc').empty?
|
86
|
+
responses[sms_resp.xpath('To').inner_text] = true
|
87
|
+
else
|
88
|
+
responses[sms_resp.xpath('To').inner_text] = sms_resp.xpath('ErrNo').inner_text.to_i
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
else
|
93
|
+
raise Mediaburst::ServerError, "Request failed: #{response}"
|
94
|
+
end
|
95
|
+
|
96
|
+
responses
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
data/lib/mediaburst.rb
ADDED
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mediaburst
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 15
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: "1.0"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Matt Hall
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-02-16 00:00:00 +00:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: nokogiri
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: webmock
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
48
|
+
description: Wrapper for the Mediaburst SMS sending API
|
49
|
+
email:
|
50
|
+
- matt@codebeef.com
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files: []
|
56
|
+
|
57
|
+
files:
|
58
|
+
- lib/mediaburst/api.rb
|
59
|
+
- lib/mediaburst/version.rb
|
60
|
+
- lib/mediaburst.rb
|
61
|
+
- ISC-LICENSE
|
62
|
+
- README.md
|
63
|
+
has_rdoc: true
|
64
|
+
homepage: http://www.mediaburst.co.uk/api
|
65
|
+
licenses:
|
66
|
+
- ISC
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.3.7
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: Ruby wrapper for the Mediaburst SMS API
|
97
|
+
test_files: []
|
98
|
+
|