esendex 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/esendex/application_controller.rb +4 -4
  3. data/app/controllers/esendex/inbound_messages_controller.rb +11 -11
  4. data/app/controllers/esendex/message_delivered_events_controller.rb +11 -11
  5. data/app/controllers/esendex/message_failed_events_controller.rb +11 -11
  6. data/app/controllers/esendex/push_notification_handler.rb +41 -41
  7. data/config/routes.rb +5 -5
  8. data/lib/esendex.rb +63 -62
  9. data/lib/esendex/account.rb +44 -44
  10. data/lib/esendex/api_connection.rb +29 -29
  11. data/lib/esendex/dispatcher_result.rb +25 -25
  12. data/lib/esendex/engine.rb +4 -4
  13. data/lib/esendex/exceptions.rb +25 -25
  14. data/lib/esendex/hash_serialisation.rb +23 -23
  15. data/lib/esendex/inbound_message.rb +30 -30
  16. data/lib/esendex/message.rb +38 -38
  17. data/lib/esendex/message_batch_submission.rb +50 -50
  18. data/lib/esendex/message_delivered_event.rb +29 -29
  19. data/lib/esendex/message_failed_event.rb +29 -29
  20. data/lib/esendex/railtie.rb +11 -11
  21. data/lib/esendex/version.rb +3 -3
  22. data/lib/esendex/voice_message.rb +25 -0
  23. data/lib/tasks/esendex.rake +29 -29
  24. data/licence.txt +23 -23
  25. data/readme.md +186 -186
  26. data/spec/account_spec.rb +211 -211
  27. data/spec/api_connection_spec.rb +78 -78
  28. data/spec/controllers/message_delivered_events_controller_spec.rb +29 -29
  29. data/spec/controllers/push_notification_handler_spec.rb +96 -96
  30. data/spec/dummy/README.rdoc +261 -261
  31. data/spec/dummy/Rakefile +7 -7
  32. data/spec/dummy/app/assets/javascripts/application.js +15 -15
  33. data/spec/dummy/app/assets/stylesheets/application.css +13 -13
  34. data/spec/dummy/app/controllers/application_controller.rb +3 -3
  35. data/spec/dummy/app/helpers/application_helper.rb +2 -2
  36. data/spec/dummy/app/views/layouts/application.html.erb +14 -14
  37. data/spec/dummy/config.ru +4 -4
  38. data/spec/dummy/config/application.rb +66 -66
  39. data/spec/dummy/config/boot.rb +9 -9
  40. data/spec/dummy/config/environment.rb +5 -5
  41. data/spec/dummy/config/environments/development.rb +37 -37
  42. data/spec/dummy/config/environments/production.rb +67 -67
  43. data/spec/dummy/config/environments/test.rb +39 -39
  44. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -7
  45. data/spec/dummy/config/initializers/inflections.rb +15 -15
  46. data/spec/dummy/config/initializers/mime_types.rb +5 -5
  47. data/spec/dummy/config/initializers/secret_token.rb +7 -7
  48. data/spec/dummy/config/initializers/session_store.rb +8 -8
  49. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -14
  50. data/spec/dummy/config/locales/en.yml +5 -5
  51. data/spec/dummy/config/routes.rb +4 -4
  52. data/spec/dummy/public/404.html +26 -26
  53. data/spec/dummy/public/422.html +26 -26
  54. data/spec/dummy/public/500.html +25 -25
  55. data/spec/dummy/script/rails +6 -6
  56. data/spec/hash_serialisation_spec.rb +52 -52
  57. data/spec/inbound_message_spec.rb +42 -42
  58. data/spec/message_batch_submission_spec.rb +54 -54
  59. data/spec/message_delivered_event_spec.rb +32 -32
  60. data/spec/message_failed_event_spec.rb +32 -32
  61. data/spec/message_spec.rb +49 -49
  62. data/spec/spec_helper.rb +41 -41
  63. data/spec/voice_message_spec.rb +29 -0
  64. metadata +12 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f9cb035158a9964f5fb7f6bb2719915755e0b6e6
4
- data.tar.gz: dd8655c31f2d0712ed387d596a714ad6901e5b3c
3
+ metadata.gz: ac1d98a5009c85639396fc251b7f1fe73c709321
4
+ data.tar.gz: 1a38f74ae0ef3da6fc6f98549b9dc5512f7b57d6
5
5
  SHA512:
6
- metadata.gz: 30b5201c1c965b9ae62a6dc148ffc42c9d14983a76ee635a42378279bb4e3d77984eb15d35c804316ba26e23b7dbd1407165af3152f5b7ca34627b2a26e7964b
7
- data.tar.gz: db9c603a303f20819bbfa00e0dbe6c453f53761aad3a76319289881a6897e01e9102a408f307f764652560cb71a8e4b3e7dad8af4d4120cb0993104ac9c35ee2
6
+ metadata.gz: 375d165b04e43b38e7607d7a077395c3070f99603797249fee0432da43eb48dcedcbc982b3c11c63a195824afa2e6e37e644b8948c05ed2c2d65800352bbee5d
7
+ data.tar.gz: 70e8d44120d20d906dea4e2102b8024254f2ad1ce530a5bdaefe13b768293cf415e8054b62f86533c80a8f43089d01c2f139f6ea36af83fa90dd519149a01716
@@ -1,4 +1,4 @@
1
- module Esendex
2
- class ApplicationController < ActionController::Base
3
- end
4
- end
1
+ module Esendex
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -1,12 +1,12 @@
1
- module Esendex
2
- class InboundMessagesController < ApplicationController
3
- include PushNotificationHandler
4
-
5
- def create
6
- process_notification :inbound_message, request.body
7
- render text: "OK"
8
- rescue => e
9
- render_error e
10
- end
11
- end
1
+ module Esendex
2
+ class InboundMessagesController < ApplicationController
3
+ include PushNotificationHandler
4
+
5
+ def create
6
+ process_notification :inbound_message, request.body
7
+ render text: "OK"
8
+ rescue => e
9
+ render_error e
10
+ end
11
+ end
12
12
  end
@@ -1,12 +1,12 @@
1
- module Esendex
2
- class MessageDeliveredEventsController < ApplicationController
3
- include PushNotificationHandler
4
-
5
- def create
6
- process_notification :message_delivered_event, request.body
7
- render text: "OK"
8
- rescue => e
9
- render_error e
10
- end
11
- end
1
+ module Esendex
2
+ class MessageDeliveredEventsController < ApplicationController
3
+ include PushNotificationHandler
4
+
5
+ def create
6
+ process_notification :message_delivered_event, request.body
7
+ render text: "OK"
8
+ rescue => e
9
+ render_error e
10
+ end
11
+ end
12
12
  end
@@ -1,12 +1,12 @@
1
- module Esendex
2
- class MessageFailedEventsController < ApplicationController
3
- include PushNotificationHandler
4
-
5
- def create
6
- process_notification :message_failed_event, request.body
7
- render text: "OK"
8
- rescue => e
9
- render_error e
10
- end
11
- end
1
+ module Esendex
2
+ class MessageFailedEventsController < ApplicationController
3
+ include PushNotificationHandler
4
+
5
+ def create
6
+ process_notification :message_failed_event, request.body
7
+ render text: "OK"
8
+ rescue => e
9
+ render_error e
10
+ end
11
+ end
12
12
  end
@@ -1,42 +1,42 @@
1
- # Common behaviour for all push notification controllers
2
- #
3
- module Esendex
4
- module PushNotificationHandler
5
-
6
- # Used by controllers to handle incoming push notification
7
- #
8
- # type - Symbol indicating type
9
- # source - String request body of the notification
10
- #
11
- # Returns nothing
12
- def process_notification(type, source)
13
-
14
- if handler = Esendex.send("#{type}_handler")
15
- notification_class = Esendex.const_get(type.to_s.camelize)
16
- notification = notification_class.from_xml source
17
- handler.call notification
18
- else
19
- logger.info "Received #{type} push notification but no handler configured" if defined?(logger)
20
- end
21
-
22
- end
23
-
24
- def render_error(error)
25
- lines = []
26
- lines << "Path: #{request.path}"
27
- request.body.rewind
28
- lines << "Notification XML:\r\n#{request.body.read}"
29
- lines << "Error: #{error.class.name} #{error.message}"
30
- if Esendex.suppress_error_backtrace
31
- lines << "[backtrace suppressed]"
32
- else
33
- lines << error.backtrace.join("\r\n")
34
- end
35
- lines << "---"
36
- lines << "Ruby #{RUBY_VERSION}"
37
- lines << "Rails #{Rails::VERSION::STRING}"
38
- lines << Esendex.user_agent
39
- render text: lines.join("\r\n"), status: 500
40
- end
41
- end
1
+ # Common behaviour for all push notification controllers
2
+ #
3
+ module Esendex
4
+ module PushNotificationHandler
5
+
6
+ # Used by controllers to handle incoming push notification
7
+ #
8
+ # type - Symbol indicating type
9
+ # source - String request body of the notification
10
+ #
11
+ # Returns nothing
12
+ def process_notification(type, source)
13
+
14
+ if handler = Esendex.send("#{type}_handler")
15
+ notification_class = Esendex.const_get(type.to_s.camelize)
16
+ notification = notification_class.from_xml source
17
+ handler.call notification
18
+ else
19
+ logger.info "Received #{type} push notification but no handler configured" if defined?(logger)
20
+ end
21
+
22
+ end
23
+
24
+ def render_error(error)
25
+ lines = []
26
+ lines << "Path: #{request.path}"
27
+ request.body.rewind
28
+ lines << "Notification XML:\r\n#{request.body.read}"
29
+ lines << "Error: #{error.class.name} #{error.message}"
30
+ if Esendex.suppress_error_backtrace
31
+ lines << "[backtrace suppressed]"
32
+ else
33
+ lines << error.backtrace.join("\r\n")
34
+ end
35
+ lines << "---"
36
+ lines << "Ruby #{RUBY_VERSION}"
37
+ lines << "Rails #{Rails::VERSION::STRING}"
38
+ lines << Esendex.user_agent
39
+ render text: lines.join("\r\n"), status: 500
40
+ end
41
+ end
42
42
  end
@@ -1,5 +1,5 @@
1
- Esendex::Engine.routes.draw do
2
- resources :message_delivered_events, only: [:create]
3
- resources :message_failed_events, only: [:create]
4
- resources :inbound_messages, only: [:create]
5
- end
1
+ Esendex::Engine.routes.draw do
2
+ resources :message_delivered_events, only: [:create]
3
+ resources :message_failed_events, only: [:create]
4
+ resources :inbound_messages, only: [:create]
5
+ end
@@ -1,62 +1,63 @@
1
- module Esendex
2
- require_relative 'esendex/version'
3
- require_relative 'esendex/exceptions'
4
- require_relative 'esendex/api_connection'
5
- require_relative 'esendex/hash_serialisation'
6
-
7
- require_relative 'esendex/account'
8
- require_relative 'esendex/dispatcher_result'
9
- require_relative 'esendex/inbound_message'
10
- require_relative 'esendex/message'
11
- require_relative 'esendex/message_batch_submission'
12
- require_relative 'esendex/message_delivered_event'
13
- require_relative 'esendex/message_failed_event'
14
-
15
- # Load Rails extensions if Rails present
16
- if defined?(Rails)
17
- require_relative 'esendex/railtie'
18
- require_relative 'esendex/engine'
19
- end
20
-
21
- API_NAMESPACE = 'http://api.esendex.com/ns/'
22
- API_HOST = 'https://api.esendex.com'
23
-
24
- # Public - used to configure the gem prior to use
25
- #
26
- # Esendex.configure do |config|
27
- # config.username = 'username'
28
- # config.password = 'password'
29
- # config.account_reference = 'account reference'
30
- # end
31
- #
32
- def self.configure
33
- yield self if block_given?
34
- end
35
-
36
- class << self
37
- # credentials for authentication
38
- attr_writer :account_reference, :username, :password
39
-
40
- # lambdas for handling push notifications
41
- attr_accessor :message_delivered_event_handler, :message_failed_event_handler, :inbound_message_handler
42
-
43
- # behaviour config
44
- attr_accessor :suppress_error_backtrace
45
-
46
- def account_reference
47
- @account_reference ||= ENV['ESENDEX_ACCOUNT']
48
- end
49
-
50
- def username
51
- @username ||= ENV['ESENDEX_USERNAME']
52
- end
53
-
54
- def password
55
- @password ||= ENV['ESENDEX_PASSWORD']
56
- end
57
- end
58
-
59
- def self.user_agent
60
- "EsendexRubyGem/#{Esendex::VERSION}"
61
- end
62
- end
1
+ module Esendex
2
+ require_relative 'esendex/version'
3
+ require_relative 'esendex/exceptions'
4
+ require_relative 'esendex/api_connection'
5
+ require_relative 'esendex/hash_serialisation'
6
+
7
+ require_relative 'esendex/account'
8
+ require_relative 'esendex/dispatcher_result'
9
+ require_relative 'esendex/inbound_message'
10
+ require_relative 'esendex/message'
11
+ require_relative 'esendex/voice_message'
12
+ require_relative 'esendex/message_batch_submission'
13
+ require_relative 'esendex/message_delivered_event'
14
+ require_relative 'esendex/message_failed_event'
15
+
16
+ # Load Rails extensions if Rails present
17
+ if defined?(Rails)
18
+ require_relative 'esendex/railtie'
19
+ require_relative 'esendex/engine'
20
+ end
21
+
22
+ API_NAMESPACE = 'http://api.esendex.com/ns/'
23
+ API_HOST = 'https://api.esendex.com'
24
+
25
+ # Public - used to configure the gem prior to use
26
+ #
27
+ # Esendex.configure do |config|
28
+ # config.username = 'username'
29
+ # config.password = 'password'
30
+ # config.account_reference = 'account reference'
31
+ # end
32
+ #
33
+ def self.configure
34
+ yield self if block_given?
35
+ end
36
+
37
+ class << self
38
+ # credentials for authentication
39
+ attr_writer :account_reference, :username, :password
40
+
41
+ # lambdas for handling push notifications
42
+ attr_accessor :message_delivered_event_handler, :message_failed_event_handler, :inbound_message_handler
43
+
44
+ # behaviour config
45
+ attr_accessor :suppress_error_backtrace
46
+
47
+ def account_reference
48
+ @account_reference ||= ENV['ESENDEX_ACCOUNT']
49
+ end
50
+
51
+ def username
52
+ @username ||= ENV['ESENDEX_USERNAME']
53
+ end
54
+
55
+ def password
56
+ @password ||= ENV['ESENDEX_PASSWORD']
57
+ end
58
+ end
59
+
60
+ def self.user_agent
61
+ "EsendexRubyGem/#{Esendex::VERSION}"
62
+ end
63
+ end
@@ -1,44 +1,44 @@
1
- require 'nokogiri'
2
-
3
- module Esendex
4
- class Account
5
- DEFAULT_DATE_OFFSET = 90
6
-
7
- attr_accessor :reference
8
-
9
- def initialize(account_reference = Esendex.account_reference)
10
- @reference = account_reference
11
- end
12
-
13
- def api_connection
14
- @api_connection ||= ApiConnection.new
15
- end
16
-
17
- def messages_remaining
18
- response = api_connection.get "/v1.0/accounts"
19
- doc = Nokogiri::XML(response.body)
20
- node = doc.at_xpath("//api:account[api:reference='#{@reference}']/api:messagesremaining", 'api' => Esendex::API_NAMESPACE)
21
- raise AccountReferenceError.new if node.nil?
22
- node.content.to_i
23
- end
24
-
25
- def send_message(args={})
26
- raise ArgumentError.new(":to required") unless args[:to]
27
- raise ArgumentError.new(":body required") unless args[:body]
28
-
29
- send_messages [Message.new(args[:to], args[:body], args[:from])]
30
- end
31
-
32
- def send_messages(messages)
33
- batch_submission = MessageBatchSubmission.new(@reference, messages)
34
- response = api_connection.post("/v1.0/messagedispatcher", batch_submission.to_s)
35
- DispatcherResult.from_xml response.body
36
- end
37
-
38
- def sent_messages(args={})
39
- SentMessageClient
40
- .new(api_connection)
41
- .get_messages(args.merge(account_reference: reference))
42
- end
43
- end
44
- end
1
+ require 'nokogiri'
2
+
3
+ module Esendex
4
+ class Account
5
+ DEFAULT_DATE_OFFSET = 90
6
+
7
+ attr_accessor :reference
8
+
9
+ def initialize(account_reference = Esendex.account_reference)
10
+ @reference = account_reference
11
+ end
12
+
13
+ def api_connection
14
+ @api_connection ||= ApiConnection.new
15
+ end
16
+
17
+ def messages_remaining
18
+ response = api_connection.get "/v1.0/accounts"
19
+ doc = Nokogiri::XML(response.body)
20
+ node = doc.at_xpath("//api:account[api:reference='#{@reference}']/api:messagesremaining", 'api' => Esendex::API_NAMESPACE)
21
+ raise AccountReferenceError.new if node.nil?
22
+ node.content.to_i
23
+ end
24
+
25
+ def send_message(args={})
26
+ raise ArgumentError.new(":to required") unless args[:to]
27
+ raise ArgumentError.new(":body required") unless args[:body]
28
+
29
+ send_messages [Message.new(args[:to], args[:body], args[:from])]
30
+ end
31
+
32
+ def send_messages(messages)
33
+ batch_submission = MessageBatchSubmission.new(@reference, messages)
34
+ response = api_connection.post("/v1.0/messagedispatcher", batch_submission.to_s)
35
+ DispatcherResult.from_xml response.body
36
+ end
37
+
38
+ def sent_messages(args={})
39
+ SentMessageClient
40
+ .new(api_connection)
41
+ .get_messages(args.merge(account_reference: reference))
42
+ end
43
+ end
44
+ end
@@ -1,29 +1,29 @@
1
- require 'nestful'
2
-
3
- module Esendex
4
- class ApiConnection
5
-
6
- def initialize
7
- @connection = Nestful::Connection.new(Esendex::API_HOST)
8
- @connection.user = Esendex.username
9
- @connection.password = Esendex.password
10
- @connection.auth_type = :basic
11
- end
12
-
13
- def default_headers
14
- { 'User-Agent' => Esendex.user_agent }
15
- end
16
-
17
- def get(url)
18
- @connection.get url, default_headers
19
- rescue => e
20
- raise Esendex::ApiErrorFactory.new.get_api_error(e)
21
- end
22
-
23
- def post(url, body)
24
- @connection.post url, body, default_headers
25
- rescue => e
26
- raise Esendex::ApiErrorFactory.new.get_api_error(e)
27
- end
28
- end
29
- end
1
+ require 'nestful'
2
+
3
+ module Esendex
4
+ class ApiConnection
5
+
6
+ def initialize
7
+ @connection = Nestful::Connection.new(Esendex::API_HOST)
8
+ @connection.user = Esendex.username
9
+ @connection.password = Esendex.password
10
+ @connection.auth_type = :basic
11
+ end
12
+
13
+ def default_headers
14
+ { 'User-Agent' => Esendex.user_agent }
15
+ end
16
+
17
+ def get(url)
18
+ @connection.get url, default_headers
19
+ rescue => e
20
+ raise Esendex::ApiErrorFactory.new.get_api_error(e)
21
+ end
22
+
23
+ def post(url, body)
24
+ @connection.post url, body, default_headers
25
+ rescue => e
26
+ raise Esendex::ApiErrorFactory.new.get_api_error(e)
27
+ end
28
+ end
29
+ end