Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/convertkit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
require 'convertkit/resources/sequence_response'
require 'convertkit/resources/sequences'
require 'convertkit/resources/subscriber_response'
require 'convertkit/resources/subscriber_bulk_add_tag_response'
require 'convertkit/resources/subscriber_bulk_create_response'
require 'convertkit/resources/subscriber_bulk_create_failure_response'
require 'convertkit/resources/subscriber_bulk_tag_response'
require 'convertkit/resources/subscriber_bulk_remove_tag_response'
require 'convertkit/resources/subscriber_bulk_tag_failure_response'
require 'convertkit/resources/subscribers'
require 'convertkit/resources/subscribers_response'
require 'convertkit/resources/subscription_response'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ConvertKit
module Resources
class SubscriberBulkTagResponse
class SubscriberBulkAddTagResponse
attr_accessor :subscribers, :failures

def initialize(response)
Expand All @@ -9,7 +9,7 @@ def initialize(response)
end

@failures = response['failures'].map do |failure|
ConvertKit::Resources::SubscriberBulkCreateFailureResponse.new(failure)
ConvertKit::Resources::SubscriberBulkTagFailureResponse.new(failure)
end
end
end
Expand Down
13 changes: 13 additions & 0 deletions lib/convertkit/resources/subscriber_bulk_remove_tag_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module ConvertKit
module Resources
class SubscriberBulkRemoveTagResponse
attr_accessor :failures

def initialize(response)
@failures = response['failures'].map do |failure|
ConvertKit::Resources::SubscriberBulkTagFailureResponse.new(failure)
end
end
end
end
end
12 changes: 12 additions & 0 deletions lib/convertkit/resources/subscriber_bulk_tag_failure_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module ConvertKit
module Resources
class SubscriberBulkTagFailureResponse
attr_accessor :subscriber, :errors

def initialize(response)
@subscriber = SubscriberResponse.new(response['subscriber']) if response['subscriber']
@errors = response['errors']
end
end
end
end
18 changes: 15 additions & 3 deletions lib/convertkit/resources/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,28 @@ def subscriptions(tag_id, options = {})
SubscriptionsResponse.new(response)
end

# Bulk tag subscribers
# Bulk add tag to subscribers
# @param [Array<Hash>] taggings
# @option taggings [String] :tag_id
# @option taggings [String] :subscriber_id
def bulk_tag_subscribers(taggings = [])
def bulk_add_to_subscribers(taggings = [])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a huge deal, but I chose this name to reflect Kit documentation:

https://developers.kit.com/v4#bulk-tag-subscribers

Otherwise, I think your name is better. We're in the Tags scope, so it's a little redundant to explicitly mention 'tag' in the method name.

I could be convinced to go either way. If we do want to match documentation, the remove method should be called bulk_remove_tags_from_subscribers, which is honestly not a great method name. Again, yours is better, IMO

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will leave the name as is because I think it is closer to the pattern implemented on this file

raise ArgumentError, 'taggings must be an array' unless taggings.is_a?(Array)

response = @client.post("bulk/#{PATH}/subscribers", { taggings: taggings })

ConvertKit::Resources::SubscriberBulkTagResponse.new(response)
ConvertKit::Resources::SubscriberBulkAddTagResponse.new(response)
end

# Bulk remove tag from subscribers
# @param [Array<Hash>] taggings
# @option taggings [String] :tag_id
# @option taggings [String] :subscriber_id
def bulk_remove_from_subscribers(taggings = [])
raise ArgumentError, 'taggings must be an array' unless taggings.is_a?(Array)

response = @client.delete("bulk/#{PATH}/subscribers", { taggings: taggings })

ConvertKit::Resources::SubscriberBulkRemoveTagResponse.new(response)
end
end
end
Expand Down
76 changes: 74 additions & 2 deletions spec/lib/convertkit/resources/tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
end
end

describe '#bulk_tag_subscribers' do
describe '#bulk_add_to_subscribers' do
let(:tags) { ConvertKit::Resources::Tags.new(client) }
let(:taggings) { [{'tag_id' => 1, 'subscriber_id' => 1}] }
let(:response) do
Expand All @@ -163,9 +163,81 @@
it 'tags listed subscribers' do
expect(client).to receive(:post).with('bulk/tags/subscribers', {taggings: taggings}).and_return(response)

tags_response = tags.bulk_tag_subscribers(taggings)
tags_response = tags.bulk_add_to_subscribers(taggings)
validate_tagged_subscribers(tags_response, response)
end
end

# Failures are not well documented in the API documentation
context 'with failures' do
let(:response) do
{
'subscribers' => [],
'failures' => [{
'subscriber' => {
'id' => 1,
'first_name' => 'foo',
'email_address' => 'foo@bar.com',
'created_at' => '2023-08-09T04:30:00Z',
},
'errors' => ['Test error message']
}]
}
end

it 'return failures with subscriber and error message' do
expect(client).to receive(:post).with('bulk/tags/subscribers', {taggings: taggings}).and_return(response)

tags_response = tags.bulk_add_to_subscribers(taggings)
expect(tags_response.failures.count).to eq(1)
expect(tags_response.failures.first.subscriber.id).to eq(1)
expect(tags_response.failures.first.errors.first).to eq('Test error message')
end
end
end

describe '#bulk_remove_from_subscribers' do
let(:tags) { ConvertKit::Resources::Tags.new(client) }
let(:taggings) { [{'tag_id' => 1, 'subscriber_id' => 1}] }
let(:response) do
{
'failures' => []
}
end

context 'with taggings provided' do
it 'return no failures' do
expect(client).to receive(:delete).with('bulk/tags/subscribers', {taggings: taggings}).and_return(response)

tags_response = tags.bulk_remove_from_subscribers(taggings)
expect(tags_response.failures).to be_empty
end
end

# Failures are not well documented in the API documentation
context 'with failures' do
let(:response) do
{
'failures' => [{
'subscriber' => {
'id' => 1,
'first_name' => 'foo',
'email_address' => 'foo@bar.com',
'created_at' => '2023-08-09T04:30:00Z',
},
'errors' => ['Test error message']
}]
}
end

it 'return failures with subscriber and error message' do
expect(client).to receive(:delete).with('bulk/tags/subscribers', {taggings: taggings}).and_return(response)

tags_response = tags.bulk_remove_from_subscribers(taggings)
expect(tags_response.failures.count).to eq(1)
expect(tags_response.failures.first.subscriber.id).to eq(1)
expect(tags_response.failures.first.errors.first).to eq('Test error message')
end
end
end
end
Loading