Skip to content
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
## [Unreleased]
- Add `Automations#create`, `#update`, `#delete`, `#copy`, `#enable`, `#disable`, `#test`, `#add_subscribers`, `#readd`, `#add_activity_to_group`, `#cancel_activity` and `#skip_activity_delay`
- Add the `AutomationTriggers`, `AutomationSteps`, `AutomationEmails`, `AutomationTemplates` and `AutomationReports` classes
- Add `Subscribers#activity_log` and `Groups#import_subscribers`
- Add the missing documented params to `Subscribers#fetch`, `Subscribers#create`, `Groups#get_subscribers`, `Segments#list`, `Segments#update` and `Automations#get`
- `Segments#get_subscribers` takes `cursor`. The API ignores `after`, so it always returned the first page
- `Groups#get_subscribers` and `Segments#list` send their params in the query string, not in a JSON body on a GET
- `Automations#get_subscriber_activity` takes `filter[:search]`, sent as `filter[search]`. The old `filter[:keyword]` is now sent as `filter[search]` too; the API rejected `filter[keyword]` with a 400
- `AutomationReports#step_subscribers` and `#trigger_subscribers` send an Array `filter[:id]` as one comma-separated value
- `Automations#get_subscriber_activity` takes `with_subscribers: true` to include each run's subscriber. It sends `with_subscribers=1` only when true, because the API turns it on for any value

## [2.0.0] - 2024-12-11
- *BREAKING CHANGE* - Please check the README file, dotenv is now optional
- *BREAKING CHANGE* - Updated the minimal required Ruby version from 2.5 to 3.1
Expand Down
6 changes: 6 additions & 0 deletions lib/mailerlite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

require_relative 'mailerlite/client'
require_relative 'mailerlite/version'
require_relative 'mailerlite/query_params'
require_relative 'mailerlite/subscribers/subscribers'
require_relative 'mailerlite/campaigns/campaigns'
require_relative 'mailerlite/forms/forms'
require_relative 'mailerlite/groups/groups'
require_relative 'mailerlite/segments/segments'
require_relative 'mailerlite/fields/fields'
require_relative 'mailerlite/automations/automations'
require_relative 'mailerlite/automations/automation_triggers'
require_relative 'mailerlite/automations/automation_steps'
require_relative 'mailerlite/automations/automation_emails'
require_relative 'mailerlite/automations/automation_templates'
require_relative 'mailerlite/automations/automation_reports'
require_relative 'mailerlite/webhooks/webhooks'
require_relative 'mailerlite/batch/batch'
require_relative 'mailerlite/timezones/timezones'
57 changes: 57 additions & 0 deletions lib/mailerlite/automations/automation_emails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

module MailerLite
# This is a class for manipulating the Automation emails from MailerLite API.
class AutomationEmails
attr_reader :client

# Inits the `AutomationEmails` class with the specified `client`.
#
# @param client [MailerLite::Client] the `Client` instance to use
def initialize(client: MailerLite::Client.new)
@client = client
end

# Returns the HTML content of the specified email
#
# @param automation_id [String] the ID of the Automation
# @param email_id [String] the ID of the email
# @return [HTTP::Response] the response from the API
def content(automation_id:, email_id:)
client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/emails/#{email_id}/content")
end

# Updates the HTML content of the specified email
#
# @param automation_id [String] the ID of the Automation
# @param email_id [String] the ID of the email
# @param html [String] the new HTML content
# @return [HTTP::Response] the response from the API
def update_content(automation_id:, email_id:, html:)
client.http.put("#{MAILERLITE_API_URL}/automations/#{automation_id}/emails/#{email_id}/content", json: { 'html' => html })
end

# Returns the report stats of the specified email
#
# @param automation_id [String] the ID of the Automation
# @param email_id [String] the ID of the email
# @param start_date [String] the first day, together with end_date
# @param end_date [String] the last day, included
# @param shop_id [String] report on one shop only, e-commerce figures only
# @param excel [Boolean] only used by export endpoints
# @return [HTTP::Response] the response from the API
def report_stats(automation_id:, email_id:, start_date: nil, end_date: nil, shop_id: nil, excel: nil)
params = QueryParams.report(start_date: start_date, end_date: end_date, shop_id: shop_id, excel: excel)
client.http.get(QueryParams.uri("#{MAILERLITE_API_URL}/automations/#{automation_id}/emails/#{email_id}/report-stats", params))
end

# Returns the reasons subscribers gave for unsubscribing from the specified email
#
# @param automation_id [String] the ID of the Automation
# @param email_id [String] the ID of the email
# @return [HTTP::Response] the response from the API
def unsubscribe_reasons(automation_id:, email_id:)
client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/emails/#{email_id}/unsubscribe-reasons")
end
end
end
114 changes: 114 additions & 0 deletions lib/mailerlite/automations/automation_reports.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# frozen_string_literal: true

module MailerLite
# This is a class for reading the Automation reports from MailerLite API.
class AutomationReports
attr_reader :client

# Inits the `AutomationReports` class with the specified `client`.
#
# @param client [MailerLite::Client] the `Client` instance to use
def initialize(client: MailerLite::Client.new)
@client = client
end

# Returns the run counts of every step of the specified Automation
#
# @param automation_id [String] the ID of the Automation
# @return [HTTP::Response] the response from the API
def step_run_counts(automation_id)
client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps/run-counts")
end

# Returns the run counts of the specified step
#
# @param automation_id [String] the ID of the Automation
# @param step_id [String] the ID of the step
# @return [HTTP::Response] the response from the API
def step_run_count(automation_id:, step_id:)
client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps/run-counts/#{step_id}")
end

# Returns the run counts of every trigger of the specified Automation
#
# @param automation_id [String] the ID of the Automation
# @return [HTTP::Response] the response from the API
def trigger_run_counts(automation_id)
client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/triggers/run-counts")
end

# Returns the run counts of the specified trigger
#
# @param automation_id [String] the ID of the Automation
# @param trigger_id [String] the ID of the trigger
# @return [HTTP::Response] the response from the API
def trigger_run_count(automation_id:, trigger_id:)
client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/triggers/run-counts/#{trigger_id}")
end

# Returns the stats of every A/B test step of the specified Automation
#
# @param automation_id [String] the ID of the Automation
# @return [HTTP::Response] the response from the API
def ab_steps_stats(automation_id)
client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/ab-steps-stats")
end

# Returns the stats of the specified A/B test step
#
# @param automation_id [String] the ID of the Automation
# @param step_id [String] the ID of the A/B test step
# @return [HTTP::Response] the response from the API
def ab_step_stats(automation_id:, step_id:)
client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/ab-steps-stats/#{step_id}")
end

# Returns the subscribers who reached the specified step
#
# @param automation_id [String] the ID of the Automation
# @param step_id [String] the ID of the step
# @param status [String] queued or completed
# @param filter[:date_from,:date_to,:search,:id] [Hash] filters for the subscribers
# @param limit [Integer] the maximum number of subscribers to return
# @param page [Integer] the page number of the results to return
# @param sort [String] the field to sort by, prefix with - for descending order
# @return [HTTP::Response] the response from the API
def step_subscribers(automation_id:, step_id:, status:, filter: {}, limit: nil, page: nil, sort: nil)
activity_subscribers("#{automation_id}/activity/steps/#{step_id}/subscribers", status: status, filter: filter, limit: limit, page: page, sort: sort)
end

# Returns the subscribers who passed the specified trigger
#
# @param automation_id [String] the ID of the Automation
# @param trigger_id [String] the ID of the trigger
# @param status [String] queued or completed
# @param filter[:date_from,:date_to,:search,:id] [Hash] filters for the subscribers
# @param limit [Integer] the maximum number of subscribers to return
# @param page [Integer] the page number of the results to return
# @param sort [String] the field to sort by, prefix with - for descending order
# @return [HTTP::Response] the response from the API
def trigger_subscribers(automation_id:, trigger_id:, status:, filter: {}, limit: nil, page: nil, sort: nil)
activity_subscribers("#{automation_id}/activity/triggers/#{trigger_id}/subscribers", status: status, filter: filter, limit: limit, page: page, sort: sort)
end

# Returns the email totals of the specified Automation per month
#
# @param automation_id [String] the ID of the Automation
# @param start_date [String] the first day, together with end_date
# @param end_date [String] the last day, included
# @param shop_id [String] report on one shop only, e-commerce figures only
# @param excel [Boolean] only used by export endpoints
# @return [HTTP::Response] the response from the API
def email_monthly_totals(automation_id:, start_date: nil, end_date: nil, shop_id: nil, excel: nil)
params = QueryParams.report(start_date: start_date, end_date: end_date, shop_id: shop_id, excel: excel)
client.http.get(QueryParams.uri("#{MAILERLITE_API_URL}/automations/#{automation_id}/monthly-totals/email", params))
end

private

def activity_subscribers(path, status:, filter:, **query)
params = { 'status' => status, **query.transform_keys(&:to_s) }.merge(QueryParams.filter(filter, %i[date_from date_to search id]))
client.http.get(QueryParams.uri("#{MAILERLITE_API_URL}/automations/#{path}", params))
end
end
end
105 changes: 105 additions & 0 deletions lib/mailerlite/automations/automation_steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# frozen_string_literal: true

module MailerLite
# This is a class for manipulating the Automation steps from MailerLite API.
class AutomationSteps
attr_reader :client

# Inits the `AutomationSteps` class with the specified `client`.
#
# @param client [MailerLite::Client] the `Client` instance to use
def initialize(client: MailerLite::Client.new)
@client = client
end

# Returns the steps of the specified Automation
#
# @param automation_id [String] the ID of the Automation
# @return [HTTP::Response] the response from the API
def list(automation_id)
client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps/index")
end

# Adds a step to the specified Automation
#
# @param automation_id [String] the ID of the Automation
# @param type [String] email, condition, delay, action, abtest or webhook
# @param parent_id [Integer] the ID of the step to add the new step after
# @param branch [String] the branch of the parent step: yes, no, a_path, b_path or c_path
# @param move_to_branch [String] the branch of the new step to move the following steps to: yes or no
# @param data [Hash] the settings of the step
# @return [HTTP::Response] the response from the API
def create(automation_id:, type:, parent_id: nil, branch: nil, move_to_branch: nil, data: nil)
params = { 'type' => type, 'parent_id' => parent_id, 'branch' => branch, 'move_to_branch' => move_to_branch, 'data' => data }
client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps", json: params.compact)
end

# Returns the specified step
#
# @param automation_id [String] the ID of the Automation
# @param step_id [String] the ID of the step
# @return [HTTP::Response] the response from the API
def fetch(automation_id:, step_id:)
client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps/#{step_id}")
end

# Updates the specified step
#
# @param automation_id [String] the ID of the Automation
# @param step_id [String] the ID of the step
# @param parent_id [Integer] the ID of the parent step
# @param data [Hash] the settings of the step
# @param reschedule_waiting_subscribers [Boolean] reschedule the subscribers waiting on a delay step
# @return [HTTP::Response] the response from the API
def update(automation_id:, step_id:, parent_id: nil, data: nil, reschedule_waiting_subscribers: nil)
params = { 'parent_id' => parent_id, 'data' => data, 'reschedule_waiting_subscribers' => reschedule_waiting_subscribers }
client.http.put("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps/#{step_id}", json: params.compact)
end

# Deletes the specified step
#
# @param automation_id [String] the ID of the Automation
# @param step_id [String] the ID of the step
# @param branch_to_delete [String] all, yes or no for a condition step; all, a_path, b_path or c_path for an A/B test step
# @return [HTTP::Response] the response from the API
def delete(automation_id:, step_id:, branch_to_delete: nil)
client.http.delete(QueryParams.uri("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps/#{step_id}", { 'branch_to_delete' => branch_to_delete }))
end

# Copies the specified step
#
# @param automation_id [String] the ID of the Automation
# @param step_id [String] the ID of the step to copy
# @param parent_id [Integer] the ID of the step to add the copy after
# @param branch [String] the branch of the parent step: yes, no, a_path, b_path or c_path
# @param move_to_branch [String] the branch of the copy to move the following steps to: yes or no
# @return [HTTP::Response] the response from the API
def copy(automation_id:, step_id:, parent_id: nil, branch: nil, move_to_branch: nil)
params = { 'parent_id' => parent_id, 'branch' => branch, 'move_to_branch' => move_to_branch }
client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps/#{step_id}/copy", json: params.compact)
end

# Moves a step to another place in the specified Automation
#
# @param automation_id [String] the ID of the Automation
# @param dragged_step_id [Integer] the ID of the step to move
# @param new_parent_step_id [Integer] the ID of the step to move it after
# @param branch [String] the branch of the new parent step: yes, no, a_path, b_path or c_path
# @param branch_for_child [String] the branch of the moved step to attach the following steps to: yes or no
# @return [HTTP::Response] the response from the API
def reorder(automation_id:, dragged_step_id:, new_parent_step_id: nil, branch: nil, branch_for_child: nil)
params = { 'dragged_step_id' => dragged_step_id, 'new_parent_step_id' => new_parent_step_id, 'branch' => branch, 'branch_for_child' => branch_for_child }
client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps/reorder", json: params.compact)
end

# Selects the winning path of the specified A/B test step
#
# @param automation_id [String] the ID of the Automation
# @param step_id [String] the ID of the A/B test step
# @param ab_test_winner [String] a_path, b_path or c_path
# @return [HTTP::Response] the response from the API
def select_ab_test_winner(automation_id:, step_id:, ab_test_winner:)
client.http.put("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps/#{step_id}/select-ab-test-winner", json: { 'ab_test_winner' => ab_test_winner })
end
end
end
34 changes: 34 additions & 0 deletions lib/mailerlite/automations/automation_templates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

module MailerLite
# This is a class for manipulating the Automation templates from MailerLite API.
class AutomationTemplates
attr_reader :client

# Inits the `AutomationTemplates` class with the specified `client`.
#
# @param client [MailerLite::Client] the `Client` instance to use
def initialize(client: MailerLite::Client.new)
@client = client
end

# Returns a list of Automation templates that match the specified filter criteria.
#
# @param filter[:type,:category] [Hash] filters for the templates
# @param limit [Integer] the maximum number of templates to return
# @param page [Integer] the page number of the results to return
# @return [HTTP::Response] the response from the API
def list(filter: {}, limit: nil, page: nil)
params = QueryParams.filter(filter, %i[type category]).merge('limit' => limit, 'page' => page)
client.http.get(QueryParams.uri("#{MAILERLITE_API_URL}/automation-templates", params))
end

# Creates an Automation from the specified template
#
# @param template_id [String] the ID of the Automation template
# @return [HTTP::Response] the response from the API
def select_template(template_id)
client.http.post("#{MAILERLITE_API_URL}/automations/select-template/#{template_id}")
end
end
end
Loading
Loading