epsilon 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,21 @@
1
+ Epsilon
2
+ =======
3
+
4
+ Interface to Real-Time Messaging (RTM) of DREAMmail’s high-volume email
5
+ solution.
6
+
7
+ Please configure like this (if you're using this within an Rails-application,
8
+ you can place this to config/initializers/epsilon.rb):
9
+ ::Epsilon::Api.url = 'http://rtm.us.epidm.net/weblet/weblet.dll'
10
+ ::Epsilon::Api.servername = 'YSN1'
11
+ ::Epsilon::Api.username = 'Username'
12
+ ::Epsilon::Api.password = 'Password'
13
+ ::Epsilon::Api.configuration = { :client_name => 'ClientName' }
14
+ #::Epsilon::Api.logger = ::Rails.logger
15
+ ::Epsilon::Api.enabled = true
16
+ #::Epsilon::Api.proxy_url = 'http://proxy:8080/'
17
+
18
+ To send a email, use this call:
19
+ ::Epsilon::Api.deliver('some@email.com', 'Campaign', 'TemplateName',
20
+ { 'param1' => 'value1', 'param2' => 'value2', ... })
21
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ desc 'Default: run unit tests.'
5
+ task :default => :test
6
+
7
+ desc 'Test the plugin.'
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.libs << 'lib'
10
+ t.pattern = 'test/**/*_test.rb'
11
+ t.verbose = true
12
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ $:.unshift "#{File.dirname(__FILE__)}/lib"
2
+ require 'epsilon'
@@ -0,0 +1,147 @@
1
+ require 'builder'
2
+ require 'net/https'
3
+ require 'uri'
4
+ require 'rexml/document'
5
+
6
+ module Epsilon
7
+ module Api
8
+ class << self
9
+
10
+ # These values need to be set with each request in the HTTP-Header.
11
+ attr_accessor :servername, :username, :password, :enabled, :logger, :message
12
+
13
+ # Retrieving the configuration.
14
+ attr_accessor :configuration
15
+
16
+ def deliver(email, campaign, template, attributes = {}, configuration = {})
17
+ if enabled
18
+ handle_result(post(xml(email, campaign, template, attributes, configuration)))
19
+ else
20
+ logger && logger.info("Sending email [#{campaign}/#{template}] via Epsilon::Api to #{email}")
21
+ end
22
+ end
23
+
24
+ def url=(url)
25
+ @uri = URI.parse(url)
26
+ end
27
+
28
+ def url
29
+ @uri ||= nil
30
+ end
31
+
32
+ def proxy_url=(url)
33
+ @proxy_uri = URI.parse(url)
34
+ end
35
+
36
+ def proxy_uri
37
+ @proxy_uri ||= ENV['http_proxy'] ? URI.parse(ENV['http_proxy']) : nil
38
+ end
39
+
40
+ def proxy_url=(url)
41
+ @@proxy_uri = URI.parse(url)
42
+ end
43
+
44
+ def proxy_uri
45
+ @@proxy_uri ||= ENV['http_proxy'] ? URI.parse(ENV['http_proxy']) : nil
46
+ end
47
+
48
+ private
49
+
50
+ def http
51
+ @http ||= proxy_uri ?
52
+ Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port) :
53
+ Net::HTTP
54
+ end
55
+
56
+ def post(xml)
57
+ http.start(uri.host, uri.port) do |agent|
58
+ agent.use_ssl = (uri.scheme == 'https') # SSL?
59
+ agent.post(uri.path, xml, {'Content-type' => 'text/xml',
60
+ 'Accept' => 'text/xml',
61
+ 'ServerName' => servername,
62
+ 'UserName' => username,
63
+ 'Password' => password})
64
+ end
65
+ end
66
+
67
+ def handle_result(result)
68
+ if(Net::HTTPOK === result)
69
+ doc = REXML::Document.new(result.body)
70
+ # Is there a better way than using REXML::Xpath.match to find text-node within XML?
71
+ case REXML::XPath.match(doc, '//DMResponse/Code/text()').first.to_s
72
+ when '1' # Success
73
+ self.message = result.message
74
+ REXML::XPath.match(doc, '//DMResponse/ResultData/TransactionID//text()').map(&:to_s)
75
+ else # Raise using Description
76
+ self.message = REXML::XPath.match(doc, '//DMResponse/Description/text()').first.to_s
77
+ false
78
+ end
79
+ else # resulting in HTTP-Message
80
+ self.message = result.message
81
+ false
82
+ end
83
+ end
84
+
85
+ # Retrieving the XML for the POST-Request
86
+ def xml(email, campaign, template, attributes = {}, configuration = {})
87
+ xml = Builder::XmlMarkup.new
88
+ xml.instruct!
89
+ xml.comment!('Created by Epsilon::Api')
90
+ xml.RTMWeblet do |weblet|
91
+ weblet.RTMEmailToEmailAddress do |email_to_email_address|
92
+ # XXX Acknowledgements yet disabled
93
+ #acknowledgements_to(email_to_email_address, configuration)
94
+ template_info(email_to_email_address, campaign, template, configuration)
95
+ email_to_email_address.ToEmailAddress do |to_email_address|
96
+ to_email_address.EventEmailAddress do |event_email_address|
97
+ event_email_address.EmailAddress(email)
98
+ event_variables(event_email_address, attributes)
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+
105
+ # Creates XML for Acknowledgements
106
+ def acknowledgements_to(xml, configuration)
107
+ if(acknowledgements_email = configuration.delete('acknowledgements_to'))
108
+ xml.AcknowledgementsTo do |acknowledgements_to|
109
+ acknowledgements_to.mailAddress(acknowledgements_email)
110
+ end
111
+ end
112
+ end
113
+
114
+ def template_info(xml, campaign, template, configuration)
115
+ conf = self.configuration.merge(configuration).merge({ :campaign_name => campaign })
116
+ # Handle non-symbolic hash-keys
117
+ conf.keys.each do |key|
118
+ if key.is_a?(String)
119
+ conf[key.to_sym] = conf.delete(key)
120
+ end
121
+ end
122
+ { :client_name => 'ClientName',
123
+ :site_name => 'SiteName',
124
+ :campaign_name => 'CampaignName' }.each do |key,value|
125
+ if(var = conf[key])
126
+ xml.tag!(value, var)
127
+ end
128
+ end
129
+ xml.MailingName(template)
130
+ end
131
+
132
+ def event_variables(xml, variables)
133
+ unless variables.empty?
134
+ xml.EventVariables do |event_variables|
135
+ variables.each do |key, value|
136
+ event_variables.Variable do |variable|
137
+ variable.Name("eventvar:#{key.to_s}")
138
+ variable.Value(value.to_s)
139
+ end
140
+ end
141
+ end
142
+ end
143
+ end
144
+
145
+ end
146
+ end
147
+ end
data/lib/epsilon.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'epsilon/api'
2
+
3
+ module Epsilon
4
+ end
@@ -0,0 +1,119 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'mocha'
4
+
5
+ require "#{File.expand_path(File.dirname(__FILE__))}/../init.rb"
6
+
7
+ class EpsilonApiTest < Test::Unit::TestCase
8
+
9
+ def setup
10
+ ::Epsilon::Api.servername = 'EpsilonServer'
11
+ ::Epsilon::Api.username = 'EpsilonUser'
12
+ ::Epsilon::Api.password = 'EpsilonSecret'
13
+ ::Epsilon::Api.url = 'http://rtm.na.epidm.net/weblet/weblet.dll'
14
+ end
15
+
16
+ def test_classes_are_loaded
17
+ assert_nothing_raised do
18
+ ::Epsilon
19
+ ::Epsilon::Api
20
+ end
21
+ end
22
+
23
+ # PASSWORD
24
+ def test_can_set_and_retrieve_password
25
+ assert_nothing_raised do
26
+ ::Epsilon::Api.password = 'test.pass.word'
27
+ assert_equal 'test.pass.word', ::Epsilon::Api.password
28
+ end
29
+ end
30
+
31
+ # USERNAME
32
+ def test_can_set_and_retrieve_username
33
+ assert_nothing_raised do
34
+ ::Epsilon::Api.username = 'test.user.name'
35
+ assert_equal 'test.user.name', ::Epsilon::Api.username
36
+ end
37
+ end
38
+
39
+ # SERVERNAME
40
+ def test_can_set_and_retrieve_servername
41
+ assert_nothing_raised do
42
+ ::Epsilon::Api.servername = 'test.server.name'
43
+ assert_equal 'test.server.name', ::Epsilon::Api.servername
44
+ end
45
+ end
46
+
47
+ # CONFIGURATION
48
+
49
+ def test_default_configuration_is_hash
50
+ assert ::Epsilon::Api.configuration.is_a?(Hash)
51
+ end
52
+
53
+ # CONFIGURATION=
54
+
55
+ def test_configuration_takes_an_empty_hash
56
+ assert_nothing_raised do
57
+ ::Epsilon::Api.configuration = {}
58
+ end
59
+ end
60
+
61
+ # POST
62
+ #def test_post_does_succeed
63
+ # puts ::Epsilon::Api.deliver('some@email.com')
64
+ #end
65
+
66
+ # URL
67
+
68
+ def test_url_fills_uri
69
+ ::Epsilon::Api.url = 'http://github.com/rjung/epsilon'
70
+ assert_equal 'github.com', ::Epsilon::Api.url.host
71
+ assert_equal '/rjung/epsilon', ::Epsilon::Api.url.path
72
+ end
73
+
74
+ # XML
75
+
76
+ def test_xml_does_contain_xml_instruction
77
+ # Need a better way to test this.
78
+ xml = ::Epsilon::Api.send(:xml, 'some@email.com', 'Campaign', 'Template')
79
+ assert /<\?xml/.match(xml), 'XML does not contain XML-Instructions'
80
+ end
81
+
82
+ def test_handle_results_false_when_result_is_not_200_OK
83
+ ::Epsilon::Api.expects(:post).with(anything).returns(Net::HTTPBadRequest.new(nil, 200, 'Bad Request'))
84
+ enable_epsilon do
85
+ assert_nothing_raised do
86
+ ::Epsilon::Api.deliver('some@email.com', 'Campaign', 'Template')
87
+ end
88
+ end
89
+ end
90
+
91
+ def test_handle_fills_message_when_result_is_not_200_OK
92
+ ::Epsilon::Api.expects(:post).with(anything).returns(Net::HTTPBadRequest.new(nil, 200, 'Bad Request'))
93
+ enable_epsilon do
94
+ ::Epsilon::Api.deliver('some@email.com', 'Campaign', 'Template')
95
+ assert_equal 'Bad Request', ::Epsilon::Api.message
96
+ end
97
+ end
98
+
99
+ def test_xml_does_contain_site_name_if_its_given_with_symbol
100
+ # Need a better way to test this.
101
+ xml = ::Epsilon::Api.send(:xml, 'some@email.com', 'Campaign', 'Template', {}, {:site_name => 'Bla'})
102
+ assert /<SiteName>/.match(xml), 'XML does not contain SiteName'
103
+ end
104
+
105
+ def test_xml_does_contain_site_name_if_its_given_with_string
106
+ xml = ::Epsilon::Api.send(:xml, 'some@email.com', 'Campaign', 'Template', {}, {'site_name' => 'Bla'})
107
+ assert /<SiteName>/.match(xml), "XML does not contain SiteName #{xml}"
108
+ end
109
+
110
+ private
111
+
112
+ def enable_epsilon(&block)
113
+ enabled_before = ::Epsilon::Api.enabled
114
+ ::Epsilon::Api.enabled = true
115
+ block.call
116
+ ::Epsilon::Api.enabled = enabled_before
117
+ end
118
+
119
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: epsilon
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors: []
13
+
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-16 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: builder
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 15
30
+ segments:
31
+ - 2
32
+ - 1
33
+ - 2
34
+ version: 2.1.2
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: API to DREAMmail Real Time Messaging
38
+ email:
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - README
47
+ - Rakefile
48
+ - init.rb
49
+ - lib/epsilon.rb
50
+ - lib/epsilon/api.rb
51
+ - test/epsilon_api_test.rb
52
+ has_rdoc: true
53
+ homepage:
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.3.7
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: API to DREAMmail Real Time Messaging
86
+ test_files:
87
+ - test/epsilon_api_test.rb