bronto 0.1.4 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/bronto.gemspec +2 -2
- data/lib/bronto.rb +12 -12
- data/lib/bronto/base.rb +21 -58
- data/lib/bronto/contact.rb +2 -8
- data/lib/bronto/delivery.rb +3 -5
- data/lib/bronto/list.rb +3 -20
- data/lib/bronto/version.rb +1 -1
- data/test/contact_test.rb +1 -1
- data/test/delivery_test.rb +3 -4
- data/test/field_test.rb +1 -1
- data/test/list_test.rb +14 -1
- data/test/message_test.rb +1 -1
- data/test/test_helper.rb +2 -10
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b01f64270c336075cec3e434fd8a71380e5bc112
|
4
|
+
data.tar.gz: d5d1df226d643bb5f584656ef7dd07e6db02243e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 435c05d68a57d9373f7f508bba5cb89a48fe955642c2b333ba3b5f4ee22a882fd3e96dcc04949a88c8f49e27b6c3c51ded734dc3ef50ec414863ccbabef7a9e2
|
7
|
+
data.tar.gz: f2101130f91052286675ba0003180a18f7f55f0861b693dc1d3e595eeb1ff173128e356d355667b05f1174a68ad8b64eb501c05a4192af43fb16560d17c6bac3
|
data/README.md
CHANGED
@@ -61,17 +61,21 @@ Let's setup some contacts, add them to a list, and then send them a message.
|
|
61
61
|
|
62
62
|
4. Create a new message and add content:
|
63
63
|
|
64
|
+
```
|
64
65
|
message = Bronto::Message.new(name: "Test Message")
|
65
66
|
message.add_content("html", "HTML Subject", "HTML Content")
|
66
67
|
message.add_content("text", "Text Subject", "Text Content")
|
67
68
|
message.save
|
69
|
+
```
|
68
70
|
|
69
71
|
5. Create a new delivery with a message and recipients and send it ASAP:
|
70
72
|
|
73
|
+
```
|
71
74
|
delivery = Bronto::Delivery.new(start: Time.now, type: "normal", from_name: "Test", from_email: "test@example.com")
|
72
75
|
delivery.message_id = message.id
|
73
76
|
delivery.add_recipient(list)
|
74
77
|
delivery.save
|
78
|
+
```
|
75
79
|
|
76
80
|
## Contributing
|
77
81
|
|
data/bronto.gemspec
CHANGED
@@ -15,9 +15,9 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = Bronto::VERSION
|
17
17
|
|
18
|
-
gem.add_dependency "savon"
|
18
|
+
gem.add_dependency "savon"
|
19
19
|
|
20
|
-
gem.add_development_dependency "
|
20
|
+
gem.add_development_dependency "byebug"
|
21
21
|
gem.add_development_dependency "turn"
|
22
22
|
gem.add_development_dependency "shoulda"
|
23
23
|
|
data/lib/bronto.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
require "savon"
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
3
|
+
require_relative "bronto/base"
|
4
|
+
require_relative "bronto/contact"
|
5
|
+
require_relative "bronto/delivery"
|
6
|
+
require_relative "bronto/field"
|
7
|
+
require_relative "bronto/filter"
|
8
|
+
require_relative "bronto/list"
|
9
|
+
require_relative "bronto/message"
|
10
|
+
require_relative "bronto/version"
|
11
|
+
|
12
|
+
require_relative "core_ext/array"
|
13
|
+
require_relative "core_ext/object"
|
14
|
+
require_relative "core_ext/string"
|
15
15
|
|
16
16
|
module Bronto
|
17
17
|
class Error < StandardError
|
data/lib/bronto/base.rb
CHANGED
@@ -2,7 +2,7 @@ module Bronto
|
|
2
2
|
|
3
3
|
# According to Bronto's API documentation, the session credential returned by the
|
4
4
|
# login() API call remains active for 20 minutes. In addition, the expiration time
|
5
|
-
# is reset after each successful use. We will trigger a refresh before 20 minutes
|
5
|
+
# is reset after each successful use. We will trigger a refresh before 20 minutes
|
6
6
|
# to be on the safe side
|
7
7
|
SESSION_REUSE_SECONDS = 120
|
8
8
|
|
@@ -26,60 +26,39 @@ module Bronto
|
|
26
26
|
end
|
27
27
|
|
28
28
|
# The primary method used to interface with the SOAP API.
|
29
|
-
# This method automatically adds the required session header and returns the actual response section of the SOAP response body.
|
30
29
|
#
|
31
30
|
# If a symbol is passed in, it is converted to "method_plural_class_name" (e.g., :read => read_lists). A string
|
32
31
|
# method is used as-is.
|
33
|
-
#
|
34
|
-
def self.request(method,
|
35
|
-
_soap_header = self.soap_header(api_key, refresh_header)
|
32
|
+
# The message is a hash and becomes the body of the SOAP request
|
33
|
+
def self.request(method, message = {})
|
36
34
|
api_key = api_key || self.api_key
|
37
35
|
|
38
36
|
method = "#{method}_#{plural_class_name}" if method.is_a? Symbol
|
39
37
|
|
40
|
-
resp = api.
|
41
|
-
soap.header = _soap_header
|
42
|
-
instance_eval(&_block) if _block # See Savon::Client#evaluate; necessary to preserve scope.
|
43
|
-
end
|
38
|
+
resp = api(api_key).call(method.to_sym, message: message)
|
44
39
|
|
45
40
|
@last_used = Time.now
|
46
41
|
|
47
42
|
resp.body["#{method}_response".to_sym]
|
48
43
|
end
|
49
44
|
|
50
|
-
# Sets up the Savon SOAP client object
|
51
|
-
def self.api
|
52
|
-
return @api unless @api.nil?
|
53
|
-
|
54
|
-
@api = Savon::Client.new do
|
55
|
-
wsdl.endpoint = "https://api.bronto.com/v4"
|
56
|
-
wsdl.namespace = "http://api.bronto.com/v4"
|
57
|
-
end
|
58
|
-
|
59
|
-
# Give Bronto up to 10 minutes to reply
|
60
|
-
@api.http.read_timeout = 600
|
61
|
-
|
62
|
-
@api
|
63
|
-
end
|
64
|
-
|
65
|
-
# Helper method to retrieve the session ID and return a SOAP header.
|
66
|
-
# Will return a header with the same initial session ID unless the `refresh`
|
67
|
-
# argument is `true`.
|
68
|
-
def self.soap_header(api_key, refresh = false)
|
69
|
-
return @soap_header if !refresh and @soap_header.present? and !session_expired
|
45
|
+
# Sets up the Savon SOAP client object, including sessionHeaders and returns the client.
|
46
|
+
def self.api(api_key, refresh = false)
|
47
|
+
return @api unless refresh || session_expired || @api.nil?
|
70
48
|
|
71
|
-
|
72
|
-
|
73
|
-
end
|
49
|
+
client = Savon.client(wsdl: 'https://api.bronto.com/v4?wsdl')
|
50
|
+
resp = client.call(:login, message: { api_token: api_key })
|
74
51
|
|
75
|
-
@
|
76
|
-
|
52
|
+
@api = Savon.client(wsdl: 'https://api.bronto.com/v4?wsdl', soap_header: {
|
53
|
+
"tns:sessionHeader" => { session_id: resp.body[:login_response][:return] }
|
54
|
+
},
|
55
|
+
read_timeout: 600) # Give Bronto up to 10 minutes to reply
|
77
56
|
end
|
78
57
|
|
79
58
|
# returns true if a cached session identifier is missing or is too old
|
80
59
|
def self.session_expired
|
81
60
|
return true if (@last_used == nil)
|
82
|
-
return true if (Time.now.tv_sec - @last_used.tv_sec > SESSION_REUSE_SECONDS)
|
61
|
+
return true if (Time.now.tv_sec - @last_used.tv_sec > SESSION_REUSE_SECONDS)
|
83
62
|
|
84
63
|
false
|
85
64
|
end
|
@@ -104,9 +83,7 @@ module Bronto
|
|
104
83
|
def self.find(filter = Bronto::Filter.new, page_number = 1, api_key = nil)
|
105
84
|
api_key = api_key || self.api_key
|
106
85
|
|
107
|
-
resp = request(:read,
|
108
|
-
soap.body = { filter: filter.to_hash, page_number: page_number }
|
109
|
-
end
|
86
|
+
resp = request(:read, { filter: filter.to_hash, page_number: page_number })
|
110
87
|
|
111
88
|
Array.wrap(resp[:return]).map { |hash| new(hash) }
|
112
89
|
end
|
@@ -121,11 +98,7 @@ module Bronto
|
|
121
98
|
objs = objs.flatten
|
122
99
|
api_key = objs.first.is_a?(String) ? objs.shift : self.api_key
|
123
100
|
|
124
|
-
resp = request(:add,
|
125
|
-
soap.body = {
|
126
|
-
plural_class_name => objs.map(&:to_hash)
|
127
|
-
}
|
128
|
-
end
|
101
|
+
resp = request(:add, {plural_class_name => objs.map(&:to_hash)})
|
129
102
|
|
130
103
|
objs.each { |o| o.errors.clear }
|
131
104
|
|
@@ -147,11 +120,7 @@ module Bronto
|
|
147
120
|
objs = objs.flatten
|
148
121
|
api_key = objs.first.is_a?(String) ? objs.shift : self.api_key
|
149
122
|
|
150
|
-
resp = request(:update,
|
151
|
-
soap.body = {
|
152
|
-
plural_class_name => objs.map(&:to_hash)
|
153
|
-
}
|
154
|
-
end
|
123
|
+
resp = request(:update, {plural_class_name => objs.map(&:to_hash)})
|
155
124
|
|
156
125
|
objs.each { |o| o.errors.clear }
|
157
126
|
objs
|
@@ -167,11 +136,7 @@ module Bronto
|
|
167
136
|
objs = objs.flatten
|
168
137
|
api_key = objs.first.is_a?(String) ? objs.shift : self.api_key
|
169
138
|
|
170
|
-
resp = request(:delete,
|
171
|
-
soap.body = {
|
172
|
-
plural_class_name => objs.map { |o| { id: o.id }}
|
173
|
-
}
|
174
|
-
end
|
139
|
+
resp = request(:delete, {plural_class_name => objs.map { |o| { id: o.id }}})
|
175
140
|
|
176
141
|
Array.wrap(resp[:return][:results]).each_with_index do |result, i|
|
177
142
|
if result[:is_error]
|
@@ -197,8 +162,8 @@ module Bronto
|
|
197
162
|
end
|
198
163
|
|
199
164
|
# Convenience instance method that calls the class `request` method.
|
200
|
-
def request(method,
|
201
|
-
self.class.request(method,
|
165
|
+
def request(method, message = {})
|
166
|
+
self.class.request(method, message)
|
202
167
|
end
|
203
168
|
|
204
169
|
def reload
|
@@ -207,9 +172,7 @@ module Bronto
|
|
207
172
|
# The block below is evaluated in a weird scope so we need to capture self as _self for use inside the block.
|
208
173
|
_self = self
|
209
174
|
|
210
|
-
resp = request(:read)
|
211
|
-
soap.body = { filter: { id: _self.id } }
|
212
|
-
end
|
175
|
+
resp = request(:read, { filter: { id: _self.id } })
|
213
176
|
|
214
177
|
resp[:return].each do |k, v|
|
215
178
|
self.send("#{k}=", v) if self.respond_to? "#{k}="
|
data/lib/bronto/contact.rb
CHANGED
@@ -13,9 +13,7 @@ module Bronto
|
|
13
13
|
body[:fields] = Array.wrap(fields).map { |f| f.is_a?(Bronto::Field) ? f.id : f } if Array(fields).length > 0
|
14
14
|
body[:include_lists] = include_lists
|
15
15
|
|
16
|
-
resp = request(:read,
|
17
|
-
soap.body = body
|
18
|
-
end
|
16
|
+
resp = request(:read, body)
|
19
17
|
|
20
18
|
Array.wrap(resp[:return]).map { |hash| new(hash) }
|
21
19
|
end
|
@@ -24,11 +22,7 @@ module Bronto
|
|
24
22
|
objs = objs.flatten
|
25
23
|
api_key = objs.first.is_a?(String) ? objs.shift : self.api_key
|
26
24
|
|
27
|
-
resp = request(:add_or_update,
|
28
|
-
soap.body = {
|
29
|
-
plural_class_name => objs.map(&:to_hash)
|
30
|
-
}
|
31
|
-
end
|
25
|
+
resp = request(:add_or_update, {plural_class_name => objs.map(&:to_hash)})
|
32
26
|
|
33
27
|
objs.each { |o| o.errors.clear }
|
34
28
|
|
data/lib/bronto/delivery.rb
CHANGED
@@ -13,9 +13,7 @@ module Bronto
|
|
13
13
|
include_content: include_content }
|
14
14
|
api_key = api_key || self.api_key
|
15
15
|
|
16
|
-
resp = request(:read,
|
17
|
-
soap.body = body
|
18
|
-
end
|
16
|
+
resp = request(:read, body)
|
19
17
|
|
20
18
|
Array.wrap(resp[:return]).map { |hash| new(hash) }
|
21
19
|
end
|
@@ -27,7 +25,7 @@ module Bronto
|
|
27
25
|
|
28
26
|
def to_hash
|
29
27
|
hash = {
|
30
|
-
id: id, start: start, message_id: message_id,
|
28
|
+
id: id, start: start, message_id: message_id, from_email: from_email, from_name: from_name,
|
31
29
|
reply_email: reply_email, recipients: recipients, fields: fields, authentication: authentication,
|
32
30
|
reply_tracking: reply_tracking
|
33
31
|
}
|
@@ -44,7 +42,7 @@ module Bronto
|
|
44
42
|
[args.first.class.to_s.split("::").last.downcase, args.first.id]
|
45
43
|
end
|
46
44
|
|
47
|
-
self.recipients << {
|
45
|
+
self.recipients << { id: id, type: type }
|
48
46
|
end
|
49
47
|
end
|
50
48
|
end
|
data/lib/bronto/list.rb
CHANGED
@@ -7,11 +7,7 @@ module Bronto
|
|
7
7
|
lists = lists.flatten
|
8
8
|
api_key = lists.first.is_a?(String) ? lists.shift : self.api_key
|
9
9
|
|
10
|
-
resp = request(:clear,
|
11
|
-
soap.body = {
|
12
|
-
list: lists.map { |l| { id: l.id } }
|
13
|
-
}
|
14
|
-
end
|
10
|
+
resp = request(:clear, {list: lists.map { |l| { id: l.id } }})
|
15
11
|
|
16
12
|
lists.each { |l| l.reload }
|
17
13
|
|
@@ -39,12 +35,7 @@ module Bronto
|
|
39
35
|
# The block below is evaluated in a weird scope so we need to capture self as _self for use inside the block.
|
40
36
|
_self = self
|
41
37
|
|
42
|
-
resp = request("add_to_list")
|
43
|
-
soap.body = {
|
44
|
-
list: { id: _self.id },
|
45
|
-
contacts: contacts.map { |c| { id: c.id } }
|
46
|
-
}
|
47
|
-
end
|
38
|
+
resp = request("add_to_list", {list: { id: _self.id }, contacts: contacts.map { |c| { id: c.id } }})
|
48
39
|
|
49
40
|
errors = Array.wrap(resp[:return][:results]).select { |r| r[:is_error] }
|
50
41
|
errors.each do |error|
|
@@ -59,15 +50,7 @@ module Bronto
|
|
59
50
|
return false if !self.id.present?
|
60
51
|
contacts = contacts.flatten
|
61
52
|
|
62
|
-
|
63
|
-
_self = self
|
64
|
-
|
65
|
-
resp = request("remove_from_list") do
|
66
|
-
soap.body = {
|
67
|
-
list: _self.to_hash,
|
68
|
-
contacts: contacts.map(&:to_hash)
|
69
|
-
}
|
70
|
-
end
|
53
|
+
resp = request("remove_from_list", {list: self.to_hash, contacts: contacts.map(&:to_hash)})
|
71
54
|
|
72
55
|
Array.wrap(resp[:return][:results]).select { |r| r[:is_error] }.count == 0
|
73
56
|
end
|
data/lib/bronto/version.rb
CHANGED
data/test/contact_test.rb
CHANGED
data/test/delivery_test.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
require_relative 'test_helper'
|
2
2
|
|
3
3
|
class DeliveryTest < Test::Unit::TestCase
|
4
4
|
context "" do
|
@@ -11,9 +11,8 @@ class DeliveryTest < Test::Unit::TestCase
|
|
11
11
|
@contact = Bronto::Contact.new(email: "#{Time.now.to_i}-#{rand(1000)}@example.com", status: "active")
|
12
12
|
@contact.save
|
13
13
|
|
14
|
-
@delivery = Bronto::Delivery.new(start: Time.now
|
15
|
-
@delivery_2 = Bronto::Delivery.new(start: Time.now + (60 * 60 * 24 * 5),
|
16
|
-
from_email: "test2@example.com")
|
14
|
+
@delivery = Bronto::Delivery.new(start: Time.now.strftime("%Y-%m-%dT%H:%M:%S.%6N%:z"), from_name: "Hello", from_email: "test@example.com")
|
15
|
+
@delivery_2 = Bronto::Delivery.new(start: (Time.now + (60 * 60 * 24 * 5)).strftime("%Y-%m-%dT%H:%M:%S.%6N%:z"), from_name: "Hello", from_email: "test2@example.com")
|
17
16
|
end
|
18
17
|
|
19
18
|
teardown do
|
data/test/field_test.rb
CHANGED
data/test/list_test.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
require_relative 'test_helper'
|
2
2
|
|
3
3
|
class ListTest < Test::Unit::TestCase
|
4
4
|
context "" do
|
@@ -88,6 +88,9 @@ class ListTest < Test::Unit::TestCase
|
|
88
88
|
assert_equal 0, @list.active_count
|
89
89
|
|
90
90
|
assert @list.add_to_list(contact)
|
91
|
+
|
92
|
+
sleep(5)
|
93
|
+
|
91
94
|
@list.reload
|
92
95
|
assert_equal 1, @list.active_count
|
93
96
|
end
|
@@ -105,10 +108,14 @@ class ListTest < Test::Unit::TestCase
|
|
105
108
|
assert_equal 0, contact2.errors.count
|
106
109
|
|
107
110
|
assert @list.add_to_list(contact, contact2)
|
111
|
+
|
112
|
+
sleep(5)
|
113
|
+
|
108
114
|
@list.reload
|
109
115
|
assert_equal 2, @list.active_count
|
110
116
|
|
111
117
|
assert @list.remove_from_list(contact)
|
118
|
+
|
112
119
|
@list.reload
|
113
120
|
assert_equal 1, @list.active_count
|
114
121
|
end
|
@@ -125,10 +132,16 @@ class ListTest < Test::Unit::TestCase
|
|
125
132
|
assert_equal 0, contact2.errors.count
|
126
133
|
|
127
134
|
assert @list.add_to_list(contact, contact2)
|
135
|
+
|
136
|
+
sleep(5)
|
137
|
+
|
128
138
|
@list.reload
|
129
139
|
assert_equal 2, @list.active_count
|
130
140
|
|
131
141
|
assert Bronto::List.clear_lists(@list)
|
142
|
+
|
143
|
+
sleep(5)
|
144
|
+
|
132
145
|
@list.reload
|
133
146
|
assert_equal 0, @list.active_count
|
134
147
|
end
|
data/test/message_test.rb
CHANGED
data/test/test_helper.rb
CHANGED
@@ -1,20 +1,12 @@
|
|
1
|
-
require 'test/unit'
|
2
1
|
require 'turn'
|
2
|
+
require 'minitest/autorun'
|
3
3
|
require 'shoulda'
|
4
4
|
|
5
|
-
|
5
|
+
require_relative '../lib/bronto'
|
6
6
|
Bronto::Base.api_key = ""
|
7
7
|
|
8
|
-
Savon.configure { |config| config.log = false }
|
9
|
-
|
10
8
|
HTTPI.log = false
|
11
9
|
|
12
|
-
def log_savon(&block)
|
13
|
-
Savon.configure { |config| config.log = true }
|
14
|
-
block.call()
|
15
|
-
Savon.configure { |config| config.log = false }
|
16
|
-
end
|
17
|
-
|
18
10
|
def reset_all
|
19
11
|
types = [Bronto::Contact, Bronto::Field, Bronto::List, Bronto::Message]
|
20
12
|
|
metadata
CHANGED
@@ -1,31 +1,31 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bronto
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Gordon
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: savon
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: byebug
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
@@ -117,7 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
117
|
version: '0'
|
118
118
|
requirements: []
|
119
119
|
rubyforge_project:
|
120
|
-
rubygems_version: 2.2.
|
120
|
+
rubygems_version: 2.2.2
|
121
121
|
signing_key:
|
122
122
|
specification_version: 4
|
123
123
|
summary: A Ruby wrapper for the Bronto SOAP API
|