Skip to content

Commit 0000fa6

Browse files
author
Darin Haener
committed
Added subscriber and event batch methods
1 parent 76a51ac commit 0000fa6

4 files changed

Lines changed: 92 additions & 0 deletions

File tree

lib/drip/client/events.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ def track_event(email, action, properties = {})
1515
data = { "email" => email, "action" => action, "properties" => properties }
1616
post "#{account_id}/events", generate_resource("events", data)
1717
end
18+
19+
# Public: Track a collection of events all at once.
20+
#
21+
# events - Required. An Array with between 1 and 1000 objects containing event data.
22+
# - A hash containing event data.
23+
# - email - Required. The String email address of the subscriber.
24+
# - action - Required. The String event action.
25+
# - properties - Optional. A Hash of event properties.
26+
#
27+
# Returns a Drip::Response.
28+
# See https://www.getdrip.com/docs/rest-api#event_batches
29+
def track_events(events)
30+
url = "#{account_id}/events/batches"
31+
post url, generate_resource("batches", { "events" => events })
32+
end
1833
end
1934
end
2035
end

lib/drip/client/subscribers.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,27 @@ def create_or_update_subscriber(email, options = {})
3333
post "#{account_id}/subscribers", generate_resource("subscribers", data)
3434
end
3535

36+
# Public: Create or update a collection of subscribers.
37+
#
38+
# subscribers - Required. An Array with between 1 and 1000 objects containing subscriber data
39+
# - A hash containing subscribers data.
40+
# - email - Required. The String subscriber email address.
41+
# - new_email - Optional. A new email address for the subscriber.
42+
# If provided and a subscriber with the email above
43+
# does not exist, this address will be used to
44+
# create a new subscriber.
45+
# - time_zone - Optional. The subscriber's time zone (in Olsen
46+
# format). Defaults to Etc/UTC.
47+
# - custom_fields - Optional. A Hash of custom field data.
48+
# - tags - Optional. An Array of tags.
49+
#
50+
# Returns a Drip::Response
51+
# See https://www.getdrip.com/docs/rest-api#subscriber_batches
52+
def create_or_update_subscribers(subscribers)
53+
url = "#{account_id}/subscribers/batches"
54+
post url, generate_resource("batches", { "subscribers" => subscribers })
55+
end
56+
3657
# Public: Unsubscribe a subscriber globally or from a specific campaign.
3758
#
3859
# id_or_email - Required. The String id or email address of the subscriber.

test/drip/client/events_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,32 @@ def setup
3939
assert_equal expected, @client.track_event(@email, @action, @properties)
4040
end
4141
end
42+
43+
context "#track_events" do
44+
setup do
45+
@events = [
46+
{
47+
:email => "derrick@getdrip.com",
48+
:action => "subscribed"
49+
},
50+
{
51+
:email => "darin@getdrip.com",
52+
:action => "unsubscribed"
53+
}
54+
]
55+
56+
@payload = { "batches" => [ { "events" => @events } ] }.to_json
57+
@response_status = 201
58+
@response_body = stub
59+
60+
@stubs.post "12345/events/batches", @payload do
61+
[@response_status, {}, @response_body]
62+
end
63+
end
64+
65+
should "send the right request" do
66+
expected = Drip::Response.new(@response_status, @response_body)
67+
assert_equal expected, @client.track_events(@events)
68+
end
69+
end
4270
end

test/drip/client/subscribers_test.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,34 @@ def setup
5050
end
5151
end
5252

53+
context "#create_or_update_subscribers" do
54+
setup do
55+
@subscribers = [
56+
{
57+
:email => "derrick@getdrip.com",
58+
:time_zone => "America/Los_Angeles"
59+
},
60+
{
61+
:email => "darin@getdrip.com",
62+
:time_zone => "America/Los_Angeles"
63+
}
64+
]
65+
66+
@payload = { "batches" => [ { "subscribers" => @subscribers } ] }.to_json
67+
@response_status = 201
68+
@response_body = stub
69+
70+
@stubs.post "12345/subscribers/batches", @payload do
71+
[@response_status, {}, @response_body]
72+
end
73+
end
74+
75+
should "send the right request" do
76+
expected = Drip::Response.new(@response_status, @response_body)
77+
assert_equal expected, @client.create_or_update_subscribers(@subscribers)
78+
end
79+
end
80+
5381
context "#subscribe" do
5482
setup do
5583
@email = "derrick@getdrip.com"

0 commit comments

Comments
 (0)