Skip to content

Commit 6587425

Browse files
committed
Merge pull request #13 from DripEmail/add_batch_operations
Added subscriber and event batch methods
2 parents 76a51ac + 567eba2 commit 6587425

5 files changed

Lines changed: 93 additions & 1 deletion

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A Ruby toolkit for the [Drip](https://www.getdrip.com/) API.
66

77
Add this line to your application's Gemfile:
88

9-
gem 'drip-ruby', :require => 'drip'
9+
gem 'drip-ruby', :require => 'drip'
1010

1111
And then execute:
1212

@@ -46,12 +46,14 @@ as methods on the client object. The following methods are currently available:
4646
| Action | Method |
4747
| :------------------------- | :--------------------------------------------------- |
4848
| Create/update a subscriber | `#create_or_update_subscriber(email, options = {})` |
49+
| Create/update a batch of subscribers | `#create_or_update_subscribers(subscribers)` |
4950
| Fetch a subscriber | `#subscriber(id_or_email)` |
5051
| Subscribe to a campaign | `#subscribe(email, campaign_id, options = {})` |
5152
| Unsubscribe | `#unsubscribe(id_or_email, options = {})` |
5253
| Apply a tag | `#apply_tag(email, tag)` |
5354
| Remove a tag | `#remove_tag(email, tag)` |
5455
| Track an event | `#track_event(email, action, properties = {})` |
56+
| Track a batch of events | `#track_events(events)` |
5557

5658

5759
**Note:** We do not have complete API coverage yet. If we are missing an API method

lib/drip/client/events.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ 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 of between 1 and 1000 Hashes of event data.
22+
# - email - Required. The String email address of the subscriber.
23+
# - action - Required. The String event action.
24+
# - properties - Optional. A Hash of event properties.
25+
#
26+
# Returns a Drip::Response.
27+
# See https://www.getdrip.com/docs/rest-api#event_batches
28+
def track_events(events)
29+
url = "#{account_id}/events/batches"
30+
post url, generate_resource("batches", { "events" => events })
31+
end
1832
end
1933
end
2034
end

lib/drip/client/subscribers.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ 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 of between 1 and 1000 Hashes of subscriber data.
39+
# - email - Required. The String subscriber email address.
40+
# - new_email - Optional. A new email address for the subscriber.
41+
# If provided and a subscriber with the email above
42+
# does not exist, this address will be used to
43+
# create a new subscriber.
44+
# - time_zone - Optional. The subscriber's time zone (in Olsen
45+
# format). Defaults to Etc/UTC.
46+
# - custom_fields - Optional. A Hash of custom field data.
47+
# - tags - Optional. An Array of tags.
48+
#
49+
# Returns a Drip::Response
50+
# See https://www.getdrip.com/docs/rest-api#subscriber_batches
51+
def create_or_update_subscribers(subscribers)
52+
url = "#{account_id}/subscribers/batches"
53+
post url, generate_resource("batches", { "subscribers" => subscribers })
54+
end
55+
3656
# Public: Unsubscribe a subscriber globally or from a specific campaign.
3757
#
3858
# 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)