esendex 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/esendex/application_controller.rb +4 -4
- data/app/controllers/esendex/inbound_messages_controller.rb +11 -11
- data/app/controllers/esendex/message_delivered_events_controller.rb +11 -11
- data/app/controllers/esendex/message_failed_events_controller.rb +11 -11
- data/app/controllers/esendex/push_notification_handler.rb +41 -41
- data/config/routes.rb +5 -5
- data/lib/esendex.rb +63 -62
- data/lib/esendex/account.rb +44 -44
- data/lib/esendex/api_connection.rb +29 -29
- data/lib/esendex/dispatcher_result.rb +25 -25
- data/lib/esendex/engine.rb +4 -4
- data/lib/esendex/exceptions.rb +25 -25
- data/lib/esendex/hash_serialisation.rb +23 -23
- data/lib/esendex/inbound_message.rb +30 -30
- data/lib/esendex/message.rb +38 -38
- data/lib/esendex/message_batch_submission.rb +50 -50
- data/lib/esendex/message_delivered_event.rb +29 -29
- data/lib/esendex/message_failed_event.rb +29 -29
- data/lib/esendex/railtie.rb +11 -11
- data/lib/esendex/version.rb +3 -3
- data/lib/esendex/voice_message.rb +25 -0
- data/lib/tasks/esendex.rake +29 -29
- data/licence.txt +23 -23
- data/readme.md +186 -186
- data/spec/account_spec.rb +211 -211
- data/spec/api_connection_spec.rb +78 -78
- data/spec/controllers/message_delivered_events_controller_spec.rb +29 -29
- data/spec/controllers/push_notification_handler_spec.rb +96 -96
- data/spec/dummy/README.rdoc +261 -261
- data/spec/dummy/Rakefile +7 -7
- data/spec/dummy/app/assets/javascripts/application.js +15 -15
- data/spec/dummy/app/assets/stylesheets/application.css +13 -13
- data/spec/dummy/app/controllers/application_controller.rb +3 -3
- data/spec/dummy/app/helpers/application_helper.rb +2 -2
- data/spec/dummy/app/views/layouts/application.html.erb +14 -14
- data/spec/dummy/config.ru +4 -4
- data/spec/dummy/config/application.rb +66 -66
- data/spec/dummy/config/boot.rb +9 -9
- data/spec/dummy/config/environment.rb +5 -5
- data/spec/dummy/config/environments/development.rb +37 -37
- data/spec/dummy/config/environments/production.rb +67 -67
- data/spec/dummy/config/environments/test.rb +39 -39
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -7
- data/spec/dummy/config/initializers/inflections.rb +15 -15
- data/spec/dummy/config/initializers/mime_types.rb +5 -5
- data/spec/dummy/config/initializers/secret_token.rb +7 -7
- data/spec/dummy/config/initializers/session_store.rb +8 -8
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -14
- data/spec/dummy/config/locales/en.yml +5 -5
- data/spec/dummy/config/routes.rb +4 -4
- data/spec/dummy/public/404.html +26 -26
- data/spec/dummy/public/422.html +26 -26
- data/spec/dummy/public/500.html +25 -25
- data/spec/dummy/script/rails +6 -6
- data/spec/hash_serialisation_spec.rb +52 -52
- data/spec/inbound_message_spec.rb +42 -42
- data/spec/message_batch_submission_spec.rb +54 -54
- data/spec/message_delivered_event_spec.rb +32 -32
- data/spec/message_failed_event_spec.rb +32 -32
- data/spec/message_spec.rb +49 -49
- data/spec/spec_helper.rb +41 -41
- data/spec/voice_message_spec.rb +29 -0
- metadata +12 -9
@@ -1,30 +1,30 @@
|
|
1
|
-
require 'nokogiri'
|
2
|
-
|
3
|
-
# <MessageFailed>
|
4
|
-
# <Id>{guid-of-push-notification}</Id>
|
5
|
-
# <MessageId>{guid-of-inbound-message}</MessageId>
|
6
|
-
# <AccountId>{guid-of-esendex-account-for-message}</AccountId>
|
7
|
-
# <OccurredAt>
|
8
|
-
# {the UTC DateTime (yyyy-MM-ddThh:mm:ss) the message failed}
|
9
|
-
# </OccurredAt>
|
10
|
-
# </MessageDelivered>
|
11
|
-
|
12
|
-
module Esendex
|
13
|
-
class MessageFailedEvent
|
14
|
-
include HashSerialisation
|
15
|
-
|
16
|
-
attr_accessor :id, :message_id, :account_id, :occurred_at
|
17
|
-
|
18
|
-
def self.from_xml(source)
|
19
|
-
doc = Nokogiri::XML source
|
20
|
-
event = MessageFailedEvent.new
|
21
|
-
event.id = doc.at_xpath("/MessageFailed/Id").content
|
22
|
-
event.message_id = doc.at_xpath("/MessageFailed/MessageId").content
|
23
|
-
event.account_id = doc.at_xpath("/MessageFailed/AccountId").content
|
24
|
-
occurred_at_s = doc.at_xpath("/MessageFailed/OccurredAt").content
|
25
|
-
event.occurred_at = DateTime.strptime(occurred_at_s, "%Y-%m-%dT%H:%M:%S").to_time
|
26
|
-
event
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
1
|
+
require 'nokogiri'
|
2
|
+
|
3
|
+
# <MessageFailed>
|
4
|
+
# <Id>{guid-of-push-notification}</Id>
|
5
|
+
# <MessageId>{guid-of-inbound-message}</MessageId>
|
6
|
+
# <AccountId>{guid-of-esendex-account-for-message}</AccountId>
|
7
|
+
# <OccurredAt>
|
8
|
+
# {the UTC DateTime (yyyy-MM-ddThh:mm:ss) the message failed}
|
9
|
+
# </OccurredAt>
|
10
|
+
# </MessageDelivered>
|
11
|
+
|
12
|
+
module Esendex
|
13
|
+
class MessageFailedEvent
|
14
|
+
include HashSerialisation
|
15
|
+
|
16
|
+
attr_accessor :id, :message_id, :account_id, :occurred_at
|
17
|
+
|
18
|
+
def self.from_xml(source)
|
19
|
+
doc = Nokogiri::XML source
|
20
|
+
event = MessageFailedEvent.new
|
21
|
+
event.id = doc.at_xpath("/MessageFailed/Id").content
|
22
|
+
event.message_id = doc.at_xpath("/MessageFailed/MessageId").content
|
23
|
+
event.account_id = doc.at_xpath("/MessageFailed/AccountId").content
|
24
|
+
occurred_at_s = doc.at_xpath("/MessageFailed/OccurredAt").content
|
25
|
+
event.occurred_at = DateTime.strptime(occurred_at_s, "%Y-%m-%dT%H:%M:%S").to_time
|
26
|
+
event
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
30
|
end
|
data/lib/esendex/railtie.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
require 'esendex'
|
2
|
-
require 'rails'
|
3
|
-
|
4
|
-
module Esendex
|
5
|
-
class Railtie < Rails::Railtie
|
6
|
-
railtie_name :esendex
|
7
|
-
|
8
|
-
rake_tasks do
|
9
|
-
load "tasks/esendex.rake"
|
10
|
-
end
|
11
|
-
end
|
1
|
+
require 'esendex'
|
2
|
+
require 'rails'
|
3
|
+
|
4
|
+
module Esendex
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
railtie_name :esendex
|
7
|
+
|
8
|
+
rake_tasks do
|
9
|
+
load "tasks/esendex.rake"
|
10
|
+
end
|
11
|
+
end
|
12
12
|
end
|
data/lib/esendex/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module Esendex
|
2
|
-
VERSION = "0.
|
3
|
-
end
|
1
|
+
module Esendex
|
2
|
+
VERSION = "0.5.0"
|
3
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Esendex
|
2
|
+
class VoiceMessage
|
3
|
+
attr_accessor :to, :body, :from
|
4
|
+
|
5
|
+
def initialize(to, body, from = nil)
|
6
|
+
@to = to
|
7
|
+
@body = body
|
8
|
+
@from = from
|
9
|
+
end
|
10
|
+
|
11
|
+
def xml_node
|
12
|
+
doc = Message.new(to, body, from).xml_node
|
13
|
+
|
14
|
+
lang = Nokogiri::XML::Node.new 'lang', doc
|
15
|
+
lang.content = 'en-GB'
|
16
|
+
doc.add_child(lang)
|
17
|
+
|
18
|
+
type = Nokogiri::XML::Node.new 'type', doc
|
19
|
+
type.content = 'Voice'
|
20
|
+
doc.add_child(type)
|
21
|
+
|
22
|
+
doc
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/tasks/esendex.rake
CHANGED
@@ -1,30 +1,30 @@
|
|
1
|
-
require_relative '../esendex'
|
2
|
-
|
3
|
-
namespace :esendex do
|
4
|
-
desc "Validates whether credentials are correct and returns message credit balance"
|
5
|
-
task :validate, [:username, :password, :account_reference] do |t, args|
|
6
|
-
begin
|
7
|
-
Esendex.configure do |config|
|
8
|
-
config.username = args.username
|
9
|
-
config.password = args.password
|
10
|
-
config.account_reference = args.account_reference
|
11
|
-
end
|
12
|
-
account = Esendex::Account.new
|
13
|
-
messages_remaining = account.messages_remaining
|
14
|
-
puts "Validated user #{Esendex.username} on account #{account.reference}. #{messages_remaining} messages remaining"
|
15
|
-
rescue => e
|
16
|
-
puts "Failed to validate credentials #{e.message}"
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
desc "Sends a message using the credentials specifed in the environment"
|
21
|
-
task :send_message, [:to, :body, :from] do |t, args|
|
22
|
-
begin
|
23
|
-
account = Esendex::Account.new
|
24
|
-
batch_id = account.send_message(args)
|
25
|
-
puts "Message sent to #{args.to}. Batch ID: #{batch_id}"
|
26
|
-
rescue => e
|
27
|
-
puts "Failed to send message #{e.message}"
|
28
|
-
end
|
29
|
-
end
|
1
|
+
require_relative '../esendex'
|
2
|
+
|
3
|
+
namespace :esendex do
|
4
|
+
desc "Validates whether credentials are correct and returns message credit balance"
|
5
|
+
task :validate, [:username, :password, :account_reference] do |t, args|
|
6
|
+
begin
|
7
|
+
Esendex.configure do |config|
|
8
|
+
config.username = args.username
|
9
|
+
config.password = args.password
|
10
|
+
config.account_reference = args.account_reference
|
11
|
+
end
|
12
|
+
account = Esendex::Account.new
|
13
|
+
messages_remaining = account.messages_remaining
|
14
|
+
puts "Validated user #{Esendex.username} on account #{account.reference}. #{messages_remaining} messages remaining"
|
15
|
+
rescue => e
|
16
|
+
puts "Failed to validate credentials #{e.message}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
desc "Sends a message using the credentials specifed in the environment"
|
21
|
+
task :send_message, [:to, :body, :from] do |t, args|
|
22
|
+
begin
|
23
|
+
account = Esendex::Account.new
|
24
|
+
batch_id = account.send_message(args)
|
25
|
+
puts "Message sent to #{args.to}. Batch ID: #{batch_id}"
|
26
|
+
rescue => e
|
27
|
+
puts "Failed to send message #{e.message}"
|
28
|
+
end
|
29
|
+
end
|
30
30
|
end
|
data/licence.txt
CHANGED
@@ -1,24 +1,24 @@
|
|
1
|
-
Copyright (c) 2013, Esendex Ltd.
|
2
|
-
All rights reserved.
|
3
|
-
|
4
|
-
Redistribution and use in source and binary forms, with or without
|
5
|
-
modification, are permitted provided that the following conditions are met:
|
6
|
-
* Redistributions of source code must retain the above copyright
|
7
|
-
notice, this list of conditions and the following disclaimer.
|
8
|
-
* Redistributions in binary form must reproduce the above copyright
|
9
|
-
notice, this list of conditions and the following disclaimer in the
|
10
|
-
documentation and/or other materials provided with the distribution.
|
11
|
-
* Neither the name of Esendex nor the
|
12
|
-
names of its contributors may be used to endorse or promote products
|
13
|
-
derived from this software without specific prior written permission.
|
14
|
-
|
15
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
16
|
-
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
17
|
-
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
18
|
-
DISCLAIMED. IN NO EVENT SHALL ESENDEX LTD BE LIABLE FOR ANY
|
19
|
-
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
20
|
-
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
21
|
-
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
22
|
-
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
23
|
-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
1
|
+
Copyright (c) 2013, Esendex Ltd.
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
* Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
* Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
* Neither the name of Esendex nor the
|
12
|
+
names of its contributors may be used to endorse or promote products
|
13
|
+
derived from this software without specific prior written permission.
|
14
|
+
|
15
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
16
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
17
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
18
|
+
DISCLAIMED. IN NO EVENT SHALL ESENDEX LTD BE LIABLE FOR ANY
|
19
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
20
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
21
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
22
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
23
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
24
24
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/readme.md
CHANGED
@@ -1,186 +1,186 @@
|
|
1
|
-
# Esendex Ruby SDK
|
2
|
-
|
3
|
-
Ruby Gem for interacting with the Esendex API. Full docs for the API can be found at [developers.esendex.com](http://developers.esendex.com).
|
4
|
-
|
5
|
-
This is in early stages of development but supports the primary requirements around sending and receiving messages as well as push notifications.
|
6
|
-
|
7
|
-
## Usage
|
8
|
-
|
9
|
-
### Setting up
|
10
|
-
|
11
|
-
From the command line
|
12
|
-
|
13
|
-
gem install esendex
|
14
|
-
|
15
|
-
or in your project's Gemfile.
|
16
|
-
|
17
|
-
gem 'esendex'
|
18
|
-
|
19
|
-
Before sending messages you need to configure the gem with your credentials. In a Rails app this would typically go into a file called `esendex.rb` in the *config/initializers*.
|
20
|
-
|
21
|
-
```ruby
|
22
|
-
Esendex.configure do |config|
|
23
|
-
config.username = "<username>"
|
24
|
-
config.password = "<password>"
|
25
|
-
config.account_reference = "<account_reference>"
|
26
|
-
end
|
27
|
-
```
|
28
|
-
|
29
|
-
You can omit account reference and specify it when you instantiate an account object if you're using multiple accounts.
|
30
|
-
|
31
|
-
You can also specify these using the environment variables `ESENDEX_USERNAME`, `ESENDEX_PASSWORD` and `ESENDEX_ACCOUNT`.
|
32
|
-
|
33
|
-
### Sending Messages
|
34
|
-
|
35
|
-
First instantiate an Account with the reference. You can omit the reference if you've already configured one to use in the *Esendex.configure* step.
|
36
|
-
Then, call the send method on the account object with a hash describing the message. The return value is a *DispatcherResult* which has the *batch_id* you can use to obtain the status of the *messages* you have sent.
|
37
|
-
|
38
|
-
```ruby
|
39
|
-
account = Account.new
|
40
|
-
result = account.send_message( to: "07777111222", body: "Saying hello to the world with the help of Esendex")
|
41
|
-
```
|
42
|
-
You can specify a different account to the default by passing the reference in as an initialization argument
|
43
|
-
|
44
|
-
```ruby
|
45
|
-
account = Account.new('EX23847')
|
46
|
-
```
|
47
|
-
|
48
|
-
Multiple messages are sent by passing an array of `Messages` to the `send_messages` method
|
49
|
-
|
50
|
-
```ruby
|
51
|
-
result = account.send_messages([Message.new("07777111222", "Hello"), Message.new("07777111333", "Hi")])
|
52
|
-
```
|
53
|
-
|
54
|
-
Sent messages can be retrieved by calling the `sent_messages` method. The return value is a *SentMessagesResult*
|
55
|
-
|
56
|
-
```ruby
|
57
|
-
result = account.sent_messages
|
58
|
-
puts result
|
59
|
-
result.messages.each do |message|
|
60
|
-
puts message
|
61
|
-
end
|
62
|
-
```
|
63
|
-
|
64
|
-
### Testing Configuration
|
65
|
-
|
66
|
-
The Esendex Gem ships with a couple of rake tasks that allow you to simply validate that all is well.
|
67
|
-
|
68
|
-
rake esendex:validate['<username>,'<password>','<account_reference>']
|
69
|
-
|
70
|
-
This will confirm that the credentials passed can access the account.
|
71
|
-
|
72
|
-
You can also send a message
|
73
|
-
|
74
|
-
rake esendex:send_message["<mobile_number>","<message_body>"]
|
75
|
-
|
76
|
-
You will need to set the credentials as enviroment variables which can also be done inline
|
77
|
-
|
78
|
-
rake esendex:send_message["<mobile_number>","<message_body>"] ESENDEX_USERNAME=<username> ESENDEX_PASSWORD=<password> ESENDEX_ACCOUNT=<account_reference>
|
79
|
-
|
80
|
-
|
81
|
-
## Push Notifications
|
82
|
-
|
83
|
-
You can configure your Esendex account to send Push Notifications when the following occur
|
84
|
-
|
85
|
-
+ MessageDeliveredEvent
|
86
|
-
+ MessageFailedEvent
|
87
|
-
+ InboundMessage
|
88
|
-
|
89
|
-
While it is possible to poll our API to check the status of outbound messages and to check for new inbound messages, the recommended approach is to allow the Esendex Platform to notify your system when these events occur.
|
90
|
-
|
91
|
-
To do this you need to setup web accessible end points to accept the notifications. These end points are configured in [Esendex Developer Tools](https://www.esendex.com/developertools).
|
92
|
-
|
93
|
-
Classes are provided in the gem for deserialising the notifications: `MessageDeliveredEvent`, `MessageFailedEvent` and `InboundMessage`. They all expose a `.from_xml` method to support this.
|
94
|
-
|
95
|
-
```ruby
|
96
|
-
message = InboundMessage.from_xml request.body
|
97
|
-
```
|
98
|
-
|
99
|
-
### Rails 3 / Rails 4
|
100
|
-
|
101
|
-
In order to simplify receipt of push notifications for Rails 3 users, the gem ships with mountable Rails controllers to handle the receipt of these notifications.
|
102
|
-
|
103
|
-
To mount the end points, add this to `routes.rb`
|
104
|
-
|
105
|
-
```ruby
|
106
|
-
RailsApp::Application.routes.draw do
|
107
|
-
...
|
108
|
-
mount Esendex::Engine => "/esendex"
|
109
|
-
end
|
110
|
-
```
|
111
|
-
|
112
|
-
To configure the behaviour in response to a notification, you need to configure a lambda to use.
|
113
|
-
|
114
|
-
```ruby
|
115
|
-
Esendex.message_delivered_event_handler = lambda { |notification|
|
116
|
-
# process the notification
|
117
|
-
}
|
118
|
-
```
|
119
|
-
|
120
|
-
Please be kind to us and don't perform anything potentially long running in this call back as our notifier may timeout the request and pass the notififation to the retry queue.
|
121
|
-
|
122
|
-
If you need to perform processing that may run for a longer time then using an async task system like [Resque](https://github.com/defunkt/resque) is recommended.
|
123
|
-
|
124
|
-
All notification classes expose a `.to_hash` method and can be initialised from the hash for serialisatiion and deserialisation.
|
125
|
-
|
126
|
-
For example:
|
127
|
-
|
128
|
-
```ruby
|
129
|
-
|
130
|
-
Esendex.inbound_message_handler = lambda { |notification|
|
131
|
-
Resque.enqueue InboundMessageProcessor, notification.to_hash
|
132
|
-
}
|
133
|
-
|
134
|
-
class InboundMessageProcessor
|
135
|
-
|
136
|
-
def self.perform(notification)
|
137
|
-
message = Esendex::InboundMessage.new notification
|
138
|
-
# do some processing of the message here
|
139
|
-
end
|
140
|
-
|
141
|
-
end
|
142
|
-
```
|
143
|
-
|
144
|
-
The handlers are defined as follows:
|
145
|
-
|
146
|
-
| End Point| Config Setting | Notification Class | Developer Tools |
|
147
|
-
| -------- | -------------- | ------------------ | --------------- |
|
148
|
-
| /esendex/inbound_messages | Esendex.inbound_message_handler | InboundMessage | SMS received |
|
149
|
-
| /esendex/message_delivered_events | Esendex.message_delivered_event_handler | MessageDeliveredEvent | SMS delivered |
|
150
|
-
| /esendex/message_failed_events | Esendex.message_failed_event_handler | MessageFailedEvent | SMS failed |
|
151
|
-
|
152
|
-
#### Errors
|
153
|
-
|
154
|
-
When an error occur in the handler lambdas then the controller returns a `500` status along with some error information back to the the Esendex Platform. This information is emailed to the technical notifications contact for the account.
|
155
|
-
|
156
|
-
The notification then enters the retry cycle.
|
157
|
-
|
158
|
-
Included by default is the backtrace for the error to assist you in identifying the issue. You can suppress backtrace with the following config option.
|
159
|
-
|
160
|
-
```ruby
|
161
|
-
Esendex.configure do |config|
|
162
|
-
config.suppress_error_backtrace = true
|
163
|
-
end
|
164
|
-
```
|
165
|
-
|
166
|
-
## Testing
|
167
|
-
|
168
|
-
bundle exec rspec
|
169
|
-
|
170
|
-
Will run specs. The spec folder contains a dummy Rails project for testing the Rails Engine.
|
171
|
-
|
172
|
-
This project also ships with a `Guardfile` so you can run tests continuously.
|
173
|
-
|
174
|
-
bundle exec guard
|
175
|
-
|
176
|
-
|
177
|
-
## Contributing
|
178
|
-
|
179
|
-
We really welcome any thoughts or guidance on how this gem could best provide the hooks you need to consume our services in your applicaiton. Please fork and raise pull requests for any features you would like us to add or raise an [issue]((https://github.com/esendex/esendex.gem/issues)
|
180
|
-
|
181
|
-
Customers with more pressing issues should contact our support teams via the usual local phone number or by email: [support@esendex.com](mailto:support@esendex.com).
|
182
|
-
|
183
|
-
## Copyright
|
184
|
-
|
185
|
-
Copyright (c) 2011-13 Esendex Ltd. See licence.txt for further details.
|
186
|
-
|
1
|
+
# Esendex Ruby SDK
|
2
|
+
|
3
|
+
Ruby Gem for interacting with the Esendex API. Full docs for the API can be found at [developers.esendex.com](http://developers.esendex.com).
|
4
|
+
|
5
|
+
This is in early stages of development but supports the primary requirements around sending and receiving messages as well as push notifications.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
### Setting up
|
10
|
+
|
11
|
+
From the command line
|
12
|
+
|
13
|
+
gem install esendex
|
14
|
+
|
15
|
+
or in your project's Gemfile.
|
16
|
+
|
17
|
+
gem 'esendex'
|
18
|
+
|
19
|
+
Before sending messages you need to configure the gem with your credentials. In a Rails app this would typically go into a file called `esendex.rb` in the *config/initializers*.
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
Esendex.configure do |config|
|
23
|
+
config.username = "<username>"
|
24
|
+
config.password = "<password>"
|
25
|
+
config.account_reference = "<account_reference>"
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
You can omit account reference and specify it when you instantiate an account object if you're using multiple accounts.
|
30
|
+
|
31
|
+
You can also specify these using the environment variables `ESENDEX_USERNAME`, `ESENDEX_PASSWORD` and `ESENDEX_ACCOUNT`.
|
32
|
+
|
33
|
+
### Sending Messages
|
34
|
+
|
35
|
+
First instantiate an Account with the reference. You can omit the reference if you've already configured one to use in the *Esendex.configure* step.
|
36
|
+
Then, call the send method on the account object with a hash describing the message. The return value is a *DispatcherResult* which has the *batch_id* you can use to obtain the status of the *messages* you have sent.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
account = Account.new
|
40
|
+
result = account.send_message( to: "07777111222", body: "Saying hello to the world with the help of Esendex")
|
41
|
+
```
|
42
|
+
You can specify a different account to the default by passing the reference in as an initialization argument
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
account = Account.new('EX23847')
|
46
|
+
```
|
47
|
+
|
48
|
+
Multiple messages are sent by passing an array of `Messages` to the `send_messages` method
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
result = account.send_messages([Message.new("07777111222", "Hello"), Message.new("07777111333", "Hi")])
|
52
|
+
```
|
53
|
+
|
54
|
+
Sent messages can be retrieved by calling the `sent_messages` method. The return value is a *SentMessagesResult*
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
result = account.sent_messages
|
58
|
+
puts result
|
59
|
+
result.messages.each do |message|
|
60
|
+
puts message
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
### Testing Configuration
|
65
|
+
|
66
|
+
The Esendex Gem ships with a couple of rake tasks that allow you to simply validate that all is well.
|
67
|
+
|
68
|
+
rake esendex:validate['<username>,'<password>','<account_reference>']
|
69
|
+
|
70
|
+
This will confirm that the credentials passed can access the account.
|
71
|
+
|
72
|
+
You can also send a message
|
73
|
+
|
74
|
+
rake esendex:send_message["<mobile_number>","<message_body>"]
|
75
|
+
|
76
|
+
You will need to set the credentials as enviroment variables which can also be done inline
|
77
|
+
|
78
|
+
rake esendex:send_message["<mobile_number>","<message_body>"] ESENDEX_USERNAME=<username> ESENDEX_PASSWORD=<password> ESENDEX_ACCOUNT=<account_reference>
|
79
|
+
|
80
|
+
|
81
|
+
## Push Notifications
|
82
|
+
|
83
|
+
You can configure your Esendex account to send Push Notifications when the following occur
|
84
|
+
|
85
|
+
+ MessageDeliveredEvent
|
86
|
+
+ MessageFailedEvent
|
87
|
+
+ InboundMessage
|
88
|
+
|
89
|
+
While it is possible to poll our API to check the status of outbound messages and to check for new inbound messages, the recommended approach is to allow the Esendex Platform to notify your system when these events occur.
|
90
|
+
|
91
|
+
To do this you need to setup web accessible end points to accept the notifications. These end points are configured in [Esendex Developer Tools](https://www.esendex.com/developertools).
|
92
|
+
|
93
|
+
Classes are provided in the gem for deserialising the notifications: `MessageDeliveredEvent`, `MessageFailedEvent` and `InboundMessage`. They all expose a `.from_xml` method to support this.
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
message = InboundMessage.from_xml request.body
|
97
|
+
```
|
98
|
+
|
99
|
+
### Rails 3 / Rails 4
|
100
|
+
|
101
|
+
In order to simplify receipt of push notifications for Rails 3 users, the gem ships with mountable Rails controllers to handle the receipt of these notifications.
|
102
|
+
|
103
|
+
To mount the end points, add this to `routes.rb`
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
RailsApp::Application.routes.draw do
|
107
|
+
...
|
108
|
+
mount Esendex::Engine => "/esendex"
|
109
|
+
end
|
110
|
+
```
|
111
|
+
|
112
|
+
To configure the behaviour in response to a notification, you need to configure a lambda to use.
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
Esendex.message_delivered_event_handler = lambda { |notification|
|
116
|
+
# process the notification
|
117
|
+
}
|
118
|
+
```
|
119
|
+
|
120
|
+
Please be kind to us and don't perform anything potentially long running in this call back as our notifier may timeout the request and pass the notififation to the retry queue.
|
121
|
+
|
122
|
+
If you need to perform processing that may run for a longer time then using an async task system like [Resque](https://github.com/defunkt/resque) is recommended.
|
123
|
+
|
124
|
+
All notification classes expose a `.to_hash` method and can be initialised from the hash for serialisatiion and deserialisation.
|
125
|
+
|
126
|
+
For example:
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
|
130
|
+
Esendex.inbound_message_handler = lambda { |notification|
|
131
|
+
Resque.enqueue InboundMessageProcessor, notification.to_hash
|
132
|
+
}
|
133
|
+
|
134
|
+
class InboundMessageProcessor
|
135
|
+
|
136
|
+
def self.perform(notification)
|
137
|
+
message = Esendex::InboundMessage.new notification
|
138
|
+
# do some processing of the message here
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
```
|
143
|
+
|
144
|
+
The handlers are defined as follows:
|
145
|
+
|
146
|
+
| End Point| Config Setting | Notification Class | Developer Tools |
|
147
|
+
| -------- | -------------- | ------------------ | --------------- |
|
148
|
+
| /esendex/inbound_messages | Esendex.inbound_message_handler | InboundMessage | SMS received |
|
149
|
+
| /esendex/message_delivered_events | Esendex.message_delivered_event_handler | MessageDeliveredEvent | SMS delivered |
|
150
|
+
| /esendex/message_failed_events | Esendex.message_failed_event_handler | MessageFailedEvent | SMS failed |
|
151
|
+
|
152
|
+
#### Errors
|
153
|
+
|
154
|
+
When an error occur in the handler lambdas then the controller returns a `500` status along with some error information back to the the Esendex Platform. This information is emailed to the technical notifications contact for the account.
|
155
|
+
|
156
|
+
The notification then enters the retry cycle.
|
157
|
+
|
158
|
+
Included by default is the backtrace for the error to assist you in identifying the issue. You can suppress backtrace with the following config option.
|
159
|
+
|
160
|
+
```ruby
|
161
|
+
Esendex.configure do |config|
|
162
|
+
config.suppress_error_backtrace = true
|
163
|
+
end
|
164
|
+
```
|
165
|
+
|
166
|
+
## Testing
|
167
|
+
|
168
|
+
bundle exec rspec
|
169
|
+
|
170
|
+
Will run specs. The spec folder contains a dummy Rails project for testing the Rails Engine.
|
171
|
+
|
172
|
+
This project also ships with a `Guardfile` so you can run tests continuously.
|
173
|
+
|
174
|
+
bundle exec guard
|
175
|
+
|
176
|
+
|
177
|
+
## Contributing
|
178
|
+
|
179
|
+
We really welcome any thoughts or guidance on how this gem could best provide the hooks you need to consume our services in your applicaiton. Please fork and raise pull requests for any features you would like us to add or raise an [issue]((https://github.com/esendex/esendex.gem/issues)
|
180
|
+
|
181
|
+
Customers with more pressing issues should contact our support teams via the usual local phone number or by email: [support@esendex.com](mailto:support@esendex.com).
|
182
|
+
|
183
|
+
## Copyright
|
184
|
+
|
185
|
+
Copyright (c) 2011-13 Esendex Ltd. See licence.txt for further details.
|
186
|
+
|