diff --git a/CHANGELOG.md b/CHANGELOG.md index 29039a8..1d86775 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/mailerlite.rb b/lib/mailerlite.rb index 22a1997..53677a5 100644 --- a/lib/mailerlite.rb +++ b/lib/mailerlite.rb @@ -2,6 +2,7 @@ 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' @@ -9,6 +10,11 @@ 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' diff --git a/lib/mailerlite/automations/automation_emails.rb b/lib/mailerlite/automations/automation_emails.rb new file mode 100644 index 0000000..7c55e81 --- /dev/null +++ b/lib/mailerlite/automations/automation_emails.rb @@ -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 diff --git a/lib/mailerlite/automations/automation_reports.rb b/lib/mailerlite/automations/automation_reports.rb new file mode 100644 index 0000000..c35972c --- /dev/null +++ b/lib/mailerlite/automations/automation_reports.rb @@ -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 diff --git a/lib/mailerlite/automations/automation_steps.rb b/lib/mailerlite/automations/automation_steps.rb new file mode 100644 index 0000000..d95a8cd --- /dev/null +++ b/lib/mailerlite/automations/automation_steps.rb @@ -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 diff --git a/lib/mailerlite/automations/automation_templates.rb b/lib/mailerlite/automations/automation_templates.rb new file mode 100644 index 0000000..4fd0e0c --- /dev/null +++ b/lib/mailerlite/automations/automation_templates.rb @@ -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 diff --git a/lib/mailerlite/automations/automation_triggers.rb b/lib/mailerlite/automations/automation_triggers.rb new file mode 100644 index 0000000..151cfa5 --- /dev/null +++ b/lib/mailerlite/automations/automation_triggers.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +module MailerLite + # This is a class for manipulating the Automation triggers from MailerLite API. + class AutomationTriggers + attr_reader :client + + # Inits the `AutomationTriggers` 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 triggers 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}/triggers") + end + + # Adds a trigger to the specified Automation + # + # @param automation_id [String] the ID of the Automation + # @param type [String] the trigger type, for example subscriber_joins_group + # @param data [Hash] the settings of the trigger + # @return [HTTP::Response] the response from the API + def create(automation_id:, type:, data: nil) + params = { 'type' => type, 'data' => data } + client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/triggers", json: params.compact) + end + + # Returns 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 fetch(automation_id:, trigger_id:) + client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/triggers/#{trigger_id}") + end + + # Updates the specified trigger + # + # @param automation_id [String] the ID of the Automation + # @param trigger_id [String] the ID of the trigger + # @param data [Hash] the settings of the trigger + # @return [HTTP::Response] the response from the API + def update(automation_id:, trigger_id:, data:) + client.http.put("#{MAILERLITE_API_URL}/automations/#{automation_id}/triggers/#{trigger_id}", json: { 'data' => data }) + end + + # Deletes 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 delete(automation_id:, trigger_id:) + client.http.delete("#{MAILERLITE_API_URL}/automations/#{automation_id}/triggers/#{trigger_id}") + end + end +end diff --git a/lib/mailerlite/automations/automations.rb b/lib/mailerlite/automations/automations.rb index 00baa3e..c26c61e 100644 --- a/lib/mailerlite/automations/automations.rb +++ b/lib/mailerlite/automations/automations.rb @@ -14,17 +14,21 @@ def initialize(client: MailerLite::Client.new) # Returns a list of Automations that match the specified filter criteria. # - # @param filter[:status,:name,:group] [Array] filters for automation + # @param filter[:status,:name,:group,:folder,:enabled,:automation_state,:drafts_state] [Array] filters for automation # @param limit [Integer] the maximum number of Automations 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 + # @param with_steps [Boolean] include the steps of each Automation, defaults to true # @return [HTTP::Response] the response from the API - def get(limit: nil, page: nil, filter: {}) + def get(limit: nil, page: nil, filter: {}, sort: nil, with_steps: nil) params = {} - params['filter[status]'] = filter[:status] if filter.key?(:status) - params['filter[name]'] = filter[:name] if filter.key?(:name) - params['filter[group]'] = filter[:group] if filter.key?(:group) + %i[status name group folder enabled automation_state drafts_state].each do |key| + params["filter[#{key}]"] = filter[key] if filter.key?(key) + end params['limit'] = limit if limit params['page'] = page if page + params['sort'] = sort if sort + params['with_steps'] = QueryParams.boolean(with_steps) uri = URI("#{MAILERLITE_API_URL}/automations") uri.query = URI.encode_www_form(params.compact) client.http.get(uri) @@ -38,22 +42,141 @@ def fetch(automation_id) client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}") end + # Creates an Automation + # + # @param name [String] the name of the Automation + # @return [HTTP::Response] the response from the API + def create(name:) + client.http.post("#{MAILERLITE_API_URL}/automations", json: { 'name' => name }) + end + + # Updates the specified Automation + # + # @param automation_id [String] the ID of the Automation to update + # @param name [String] the name of the Automation + # @param trigger_data [Hash] the settings of the first trigger + # @param triggers [Array] the triggers of the Automation + # @return [HTTP::Response] the response from the API + def update(automation_id:, name:, trigger_data: nil, triggers: nil) + params = { 'name' => name, 'trigger_data' => trigger_data, 'triggers' => triggers } + client.http.put("#{MAILERLITE_API_URL}/automations/#{automation_id}", json: params.compact) + end + + # Deletes the specified Automation + # + # @param automation_id [String] the ID of the Automation to delete + # @return [HTTP::Response] the response from the API + def delete(automation_id) + client.http.delete("#{MAILERLITE_API_URL}/automations/#{automation_id}") + end + + # Copies the specified Automation + # + # @param automation_id [String] the ID of the Automation to copy + # @return [HTTP::Response] the response from the API + def copy(automation_id) + client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/copy") + end + + # Enables the specified Automation + # + # @param automation_id [String] the ID of the Automation to enable + # @return [HTTP::Response] the response from the API + def enable(automation_id) + client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/enable") + end + + # Disables the specified Automation + # + # @param automation_id [String] the ID of the Automation to disable + # @return [HTTP::Response] the response from the API + def disable(automation_id) + client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/disable") + end + + # Sends the emails of the specified Automation to a test address + # + # @param automation_id [String] the ID of the Automation to test + # @param email [String] the address to send the test emails to + # @return [HTTP::Response] the response from the API + def test(automation_id:, email:) + client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/test", json: { 'email' => email }) + end + + # Adds subscribers to the specified Automation + # + # @param automation_id [String] the ID of the Automation + # @param step_id [Integer] the step to start the subscribers at + # @param emails [Array] the email addresses of the subscribers to add + # @param groups [Array] the IDs of the groups whose subscribers to add + # @return [HTTP::Response] the response from the API + def add_subscribers(automation_id:, step_id: nil, emails: nil, groups: nil) + params = { 'step_id' => step_id, 'emails' => emails, 'groups' => groups } + client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/add-subscribers", json: params.compact) + end + + # Adds subscribers back to the specified Automation + # + # @param automation_id [String] the ID of the Automation + # @param readd_subscribers_to [String] last_completed_step, selected_step or last_failed_step + # @param filter [Hash] the activity to re-add, with status and optional search, date_from, date_to, id and step + # @param selected_step_id [String] the step to re-add the subscribers to, when readd_subscribers_to is selected_step + # @return [HTTP::Response] the response from the API + def readd(automation_id:, readd_subscribers_to:, filter:, selected_step_id: nil) + params = { 'readd_subscribers_to' => readd_subscribers_to, 'filter' => filter, 'selected_step_id' => selected_step_id } + client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/readd", json: params.compact) + end + + # Adds the subscribers in the activity of the specified Automation to groups + # + # @param automation_id [String] the ID of the Automation + # @param destination_groups [Array] the IDs of the groups to add the subscribers to + # @param filter [Hash] the activity to act on, with status and optional id, search and date filters + # @return [HTTP::Response] the response from the API + def add_activity_to_group(automation_id:, destination_groups:, filter:) + params = { 'destination_groups' => destination_groups, 'filter' => filter } + client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/activity/add-to-group", json: params) + end + + # Cancels the active runs in the activity of the specified Automation + # + # @param automation_id [String] the ID of the Automation + # @param filter [Hash] the activity to act on, with status active and optional id, search and date filters + # @return [HTTP::Response] the response from the API + def cancel_activity(automation_id:, filter:) + client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/activity/cancel-automation", json: { 'filter' => filter }) + end + + # Skips the current delay of the active runs in the activity of the specified Automation + # + # @param automation_id [String] the ID of the Automation + # @param filter [Hash] the activity to act on, with status active and optional id, search and date filters + # @return [HTTP::Response] the response from the API + def skip_activity_delay(automation_id:, filter:) + client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/activity/skip-delay", json: { 'filter' => filter }) + end + # get_subscriber_activity the subscriber activity for specified Automation # # @param automation_id [Integer] the ID of the Automation to get_subscriber_activity - # @param filter[:status,:date_from,:date_to,:scheduled_from,:scheduled_to,:keyword] [Array] Must be one of the following: completed, active, canceled, failed + # @param filter[:status,:date_from,:date_to,:scheduled_from,:scheduled_to,:search] [Array] Must be one of the following: completed, active, canceled, failed + # @param filter[:keyword] [String] deprecated, sent as filter[search], use filter[:search] # @param limit [Integer] the maximum number of Automations 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 + # @param with_subscribers [Boolean] include the id, email and name of each subscriber # @return [HTTP::Response] the response from the API - def get_subscriber_activity(automation_id:, filter: {}, page: nil, limit: nil) + def get_subscriber_activity(automation_id:, filter: {}, page: nil, limit: nil, sort: nil, with_subscribers: nil) params = { 'filter[status]' => filter[:status] } params['filter[date_from]'] = filter[:date_from] if filter.key?(:date_from) params['filter[date_to]'] = filter[:date_to] if filter.key?(:date_to) params['filter[scheduled_from]'] = filter[:scheduled_from] if filter.key?(:scheduled_from) params['filter[scheduled_to]'] = filter[:scheduled_to] if filter.key?(:scheduled_to) - params['filter[keyword]'] = filter[:keyword] if filter.key?(:keyword) + params['filter[search]'] = filter.fetch(:search, filter[:keyword]) params['page'] = page if page params['limit'] = limit if limit + params['sort'] = sort if sort + params['with_subscribers'] = 1 if with_subscribers uri = URI("#{MAILERLITE_API_URL}/automations/#{automation_id}/activity") uri.query = URI.encode_www_form(params.compact) client.http.get(uri) diff --git a/lib/mailerlite/groups/groups.rb b/lib/mailerlite/groups/groups.rb index 65ba3d0..781542c 100644 --- a/lib/mailerlite/groups/groups.rb +++ b/lib/mailerlite/groups/groups.rb @@ -52,15 +52,23 @@ def update(group_id:, name:) # @param group_id [Integer] The id of existing group belonging to the account # @param filter[:status] [String] Must be one of the possible statuses: active, unsubscribed, unconfirmed, bounced or junk. Defaults to active. # @param limit [Integer] the maximum number of subscribers to return - # @param page [Integer] the page number of the results to return + # @param cursor [String] the cursor for the page of results to return + # @param include [String] the relations to embed, only groups is supported # @return [HTTP::Response] the response from the API - def get_subscribers(group_id:, filter: {}, limit: nil, cursor: nil, sort: nil) - params = {} - params['filter[status]'] = filter[:status] if filter.key?(:status) - params['limit'] = limit if limit - params['sort'] = sort if sort - params['cursor'] = cursor if cursor - client.http.get("#{MAILERLITE_API_URL}/groups/#{group_id}/subscribers", json: params.compact) + def get_subscribers(group_id:, filter: {}, limit: nil, cursor: nil, sort: nil, include: nil) + params = QueryParams.filter(filter, %i[status]).merge('limit' => limit, 'sort' => sort, 'cursor' => cursor, 'include' => include) + client.http.get(QueryParams.uri("#{MAILERLITE_API_URL}/groups/#{group_id}/subscribers", params)) + end + + # Imports subscribers into the specified group. + # @param group_id [Integer] The id of existing group belonging to the account + # @param subscribers [Array] the subscribers to import, each a hash with email, and optional name and fields + # @param resubscribe [Boolean] reactivate unsubscribed subscribers, bounced and junk subscribers are not reactivated + # @param trigger_automations [Boolean] start the automations the import qualifies for + # @return [HTTP::Response] the response from the API + def import_subscribers(group_id:, subscribers:, resubscribe: nil, trigger_automations: nil) + params = { 'subscribers' => subscribers, 'resubscribe' => resubscribe, 'trigger_automations' => trigger_automations } + client.http.post("#{MAILERLITE_API_URL}/groups/#{group_id}/import-subscribers", json: params.compact) end # Assign Subscriber to the specified group. diff --git a/lib/mailerlite/query_params.rb b/lib/mailerlite/query_params.rb new file mode 100644 index 0000000..875843d --- /dev/null +++ b/lib/mailerlite/query_params.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require 'uri' + +module MailerLite + # Encodes values for the query string of the MailerLite API. + module QueryParams + module_function + + def boolean(value) + return if value.nil? + + value ? 1 : 0 + end + + def filter(filter, keys) + filter.each_with_object({}) do |(key, value), params| + params["filter[#{key}]"] = value.is_a?(Array) ? value.join(',') : value if keys.include?(key) + end + end + + def report(start_date:, end_date:, shop_id:, excel:) + { 'start_date' => start_date, 'end_date' => end_date, 'shop_id' => shop_id, 'excel' => boolean(excel) } + end + + def uri(url, params) + uri = URI(url) + query = URI.encode_www_form(params.compact) + uri.query = query unless query.empty? + uri + end + end +end diff --git a/lib/mailerlite/segments/segments.rb b/lib/mailerlite/segments/segments.rb index 052c427..8fe59c4 100644 --- a/lib/mailerlite/segments/segments.rb +++ b/lib/mailerlite/segments/segments.rb @@ -16,22 +16,20 @@ def initialize(client: MailerLite::Client.new) # # @param limit [Integer] the maximum number of Segments 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 list(limit: nil, page: nil) - params = {} - params['limit'] = limit if limit - params['page'] = page if page - - client.http.get("#{MAILERLITE_API_URL}/segments", json: params.compact) + def list(limit: nil, page: nil, sort: nil) + client.http.get(QueryParams.uri("#{MAILERLITE_API_URL}/segments", { 'limit' => limit, 'page' => page, 'sort' => sort })) end # Update the specified Segment # # @param segment_id [String] the ID of the Segments to update # @param name [String] the name to update + # @param filter [Array] the segment rules # @return [HTTP::Response] the response from the API - def update(segment_id:, name:) - params = { 'name' => name } + def update(segment_id:, name:, filter: nil) + params = { 'name' => name, 'filter' => filter } client.http.put("#{MAILERLITE_API_URL}/segments/#{segment_id}", json: params.compact) end @@ -39,12 +37,14 @@ def update(segment_id:, name:) # @param segment_id [Integer] The id of existing Segment belonging to the account # @param filter[status] [String] Must be one of the possible statuses: active, unsubscribed, unconfirmed, bounced or junk. Defaults to active. # @param limit [Integer] the maximum number of subscribers to return - # @param after [Integer] The last subscriber id, available in meta.last + # @param cursor [String] the cursor for the page of results to return, available in meta.next_cursor + # @param after [Integer] deprecated, the API ignores it, use cursor # @return [HTTP::Response] the response from the API - def get_subscribers(segment_id:, filter: {}, limit: nil, after: nil) + def get_subscribers(segment_id:, filter: {}, limit: nil, after: nil, cursor: nil) params = {} params['filter[status]'] = filter[:status] if filter.key?(:status) params['limit'] = limit if limit + params['cursor'] = cursor if cursor params['after'] = after if after uri = URI("#{MAILERLITE_API_URL}/segments/#{segment_id}/subscribers") uri.query = URI.encode_www_form(params.compact) diff --git a/lib/mailerlite/subscribers/subscribers.rb b/lib/mailerlite/subscribers/subscribers.rb index 535e747..58d2409 100644 --- a/lib/mailerlite/subscribers/subscribers.rb +++ b/lib/mailerlite/subscribers/subscribers.rb @@ -14,18 +14,33 @@ def initialize(client: MailerLite::Client.new) # Returns a list of subscribers that match the specified filter criteria. # - # @param filter[:status] [Array] the status of the subscribers to include in the results + # @param filter[:status] [String] the status of the subscribers to include in the results + # @param filter[:group] [String] the ID of a group, or several IDs separated with commas + # @param filter[:automation] [String] the ID of an automation, or several IDs separated with commas + # @param filter[:form] [String] the ID of a form, or several IDs separated with commas + # @param filter[:site] [String] the ID of a site, or several IDs separated with commas + # @param filter[:page] [String] the ID of a landing page, or several IDs separated with commas + # @param filter[:import] [String] the ID of an import, or several IDs separated with commas + # @param filter[:post] [String] the ID of a blog post, or several IDs separated with commas # @param limit [Integer] the maximum number of subscribers to return - # @param page [Integer] the page number of the results to return + # @param cursor [String] the cursor for the page of results to return + # @param sort [String] the field to sort by, prefix with - for descending order + # @param include [String] the relations to embed, only groups is supported + # @param start_date [String] only subscribers who signed up on or after this date + # @param end_date [String] only subscribers who signed up on or before this date + # @param with_fields [Boolean] return each custom field with its name, value and type # @return [HTTP::Response] the response from the API - def fetch(filter:, limit: nil, cursor: nil) - params = { 'filter[status]' => filter[:status] } + def fetch(filter:, limit: nil, cursor: nil, sort: nil, include: nil, start_date: nil, end_date: nil, with_fields: nil) + params = QueryParams.filter(filter, %i[status group automation form site page import post]) params['limit'] = limit if limit params['cursor'] = cursor if cursor - uri = URI("#{MAILERLITE_API_URL}/subscribers") - uri.query = URI.encode_www_form(params.compact) - client.http.get(uri) + params['sort'] = sort if sort + params['include'] = include if include + params['start_date'] = start_date if start_date + params['end_date'] = end_date if end_date + params['with_fields'] = QueryParams.boolean(with_fields) + client.http.get(QueryParams.uri("#{MAILERLITE_API_URL}/subscribers", params)) end # Creates a new subscriber with the specified details. @@ -39,8 +54,11 @@ def fetch(filter:, limit: nil, cursor: nil) # @param opted_in_at [DateTime] the date and time when the subscriber confirmed their subscription # @param optin_ip [String] the IP address of the subscriber when they confirmed their subscription # @param unsubscribed_at [DateTime] the date and time when the subscriber was unsubscribed + # @param resubscribe [Boolean] reactivate the subscriber if they are unsubscribed, bounced or junk + # @param double_optin [Boolean] send a confirmation email to the new subscriber + # @param notes [Array] notes to add to the subscriber # @return [HTTP::Response] the response from the API - def create(email:, fields: nil, groups: nil, status: nil, subscribed_at: nil, ip_address: nil, opted_in_at: nil, optin_ip: nil, unsubscribed_at: nil) + def create(email:, fields: nil, groups: nil, status: nil, subscribed_at: nil, ip_address: nil, opted_in_at: nil, optin_ip: nil, unsubscribed_at: nil, resubscribe: nil, double_optin: nil, notes: nil) params = { 'email' => email } params['fields'] = fields if fields @@ -51,6 +69,9 @@ def create(email:, fields: nil, groups: nil, status: nil, subscribed_at: nil, ip params['opted_in_at'] = opted_in_at if opted_in_at params['optin_ip'] = optin_ip if optin_ip params['unsubscribed_at'] = unsubscribed_at if unsubscribed_at + params['resubscribe'] = resubscribe + params['double_optin'] = double_optin + params['notes'] = notes if notes client.http.post("#{MAILERLITE_API_URL}/subscribers", json: params.compact) end @@ -92,6 +113,18 @@ def get(subscriber_id) client.http.get("#{MAILERLITE_API_URL}/subscribers/#{subscriber_id}") end + # Returns the activity log of the specified subscriber + # + # @param subscriber_id [String] the ID of the subscriber + # @param filter[:log_name] [String] the type of activity to include in the results + # @param limit [Integer] the maximum number of entries to return + # @param page [Integer] the page number of the results to return + # @return [HTTP::Response] the response from the API + def activity_log(subscriber_id:, filter: {}, limit: nil, page: nil) + params = QueryParams.filter(filter, %i[log_name]).merge('limit' => limit, 'page' => page) + client.http.get(QueryParams.uri("#{MAILERLITE_API_URL}/subscribers/#{subscriber_id}/activity-log", params)) + end + # Returns the details of the specified subscribers # # @param import_id [String] the ID of the import to fetch report diff --git a/spec/automation_emails_rspec.rb b/spec/automation_emails_rspec.rb new file mode 100644 index 0000000..4e2c8e7 --- /dev/null +++ b/spec/automation_emails_rspec.rb @@ -0,0 +1,40 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe MailerLite::AutomationEmails do + let(:client) { MailerLite::Client.new } + let(:emails) { described_class.new(client: client) } + + describe '#content' do + it 'sends GET /automations/{automation}/emails/{email}/content' do + stub = stub_api(:get, '/automations/1/emails/2/content') + emails.content(automation_id: 1, email_id: 2) + expect(stub).to have_been_requested + end + end + + describe '#update_content' do + it 'sends PUT /automations/{automation}/emails/{email}/content' do + stub = stub_api(:put, '/automations/1/emails/2/content', body: { 'html' => '

Hi

' }) + emails.update_content(automation_id: 1, email_id: 2, html: '

Hi

') + expect(stub).to have_been_requested + end + end + + describe '#report_stats' do + it 'sends GET /automations/{automation}/emails/{email}/report-stats with the query' do + stub = stub_api(:get, '/automations/1/emails/2/report-stats', query: { 'start_date' => '2026-01-01', 'end_date' => '2026-01-31', 'shop_id' => '3', 'excel' => '0' }) + emails.report_stats(automation_id: 1, email_id: 2, start_date: '2026-01-01', end_date: '2026-01-31', shop_id: '3', excel: false) + expect(stub).to have_been_requested + end + end + + describe '#unsubscribe_reasons' do + it 'sends GET /automations/{automation}/emails/{email}/unsubscribe-reasons' do + stub = stub_api(:get, '/automations/1/emails/2/unsubscribe-reasons') + emails.unsubscribe_reasons(automation_id: 1, email_id: 2) + expect(stub).to have_been_requested + end + end +end diff --git a/spec/automation_reports_rspec.rb b/spec/automation_reports_rspec.rb new file mode 100644 index 0000000..d48cfce --- /dev/null +++ b/spec/automation_reports_rspec.rb @@ -0,0 +1,92 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe MailerLite::AutomationReports do + let(:client) { MailerLite::Client.new } + let(:reports) { described_class.new(client: client) } + + describe '#step_run_counts' do + it 'sends GET /automations/{automation}/steps/run-counts' do + stub = stub_api(:get, '/automations/1/steps/run-counts') + reports.step_run_counts(1) + expect(stub).to have_been_requested + end + end + + describe '#step_run_count' do + it 'sends GET /automations/{automation}/steps/run-counts/{step}' do + stub = stub_api(:get, '/automations/1/steps/run-counts/2') + reports.step_run_count(automation_id: 1, step_id: 2) + expect(stub).to have_been_requested + end + end + + describe '#trigger_run_counts' do + it 'sends GET /automations/{automation}/triggers/run-counts' do + stub = stub_api(:get, '/automations/1/triggers/run-counts') + reports.trigger_run_counts(1) + expect(stub).to have_been_requested + end + end + + describe '#trigger_run_count' do + it 'sends GET /automations/{automation}/triggers/run-counts/{trigger}' do + stub = stub_api(:get, '/automations/1/triggers/run-counts/2') + reports.trigger_run_count(automation_id: 1, trigger_id: 2) + expect(stub).to have_been_requested + end + end + + describe '#ab_steps_stats' do + it 'sends GET /automations/{automation}/ab-steps-stats' do + stub = stub_api(:get, '/automations/1/ab-steps-stats') + reports.ab_steps_stats(1) + expect(stub).to have_been_requested + end + end + + describe '#ab_step_stats' do + it 'sends GET /automations/{automation}/ab-steps-stats/{step}' do + stub = stub_api(:get, '/automations/1/ab-steps-stats/2') + reports.ab_step_stats(automation_id: 1, step_id: 2) + expect(stub).to have_been_requested + end + end + + describe '#step_subscribers' do + it 'sends GET /automations/{automation}/activity/steps/{step}/subscribers with the query' do + stub = stub_api(:get, '/automations/1/activity/steps/2/subscribers', query: { + 'status' => 'queued', 'limit' => '10', 'page' => '2', 'sort' => '-id', + 'filter[date_from]' => '2026-01-01', 'filter[date_to]' => '2026-01-31', 'filter[search]' => 'a@b.com', 'filter[id]' => '5' + }) + reports.step_subscribers(automation_id: 1, step_id: 2, status: 'queued', limit: 10, page: 2, sort: '-id', + filter: { date_from: '2026-01-01', date_to: '2026-01-31', search: 'a@b.com', id: '5' }) + expect(stub).to have_been_requested + end + end + + describe '#step_subscribers with an Array filter[:id]' do + it 'sends the ids as one comma-separated value' do + stub = stub_api(:get, '/automations/1/activity/steps/2/subscribers', query: { 'status' => 'queued', 'filter[id]' => '5,6' }) + reports.step_subscribers(automation_id: 1, step_id: 2, status: 'queued', filter: { id: %w[5 6] }) + expect(stub).to have_been_requested + end + end + + describe '#trigger_subscribers' do + it 'sends GET /automations/{automation}/activity/triggers/{trigger}/subscribers with the query' do + stub = stub_api(:get, '/automations/1/activity/triggers/2/subscribers', query: { 'status' => 'completed', 'filter[search]' => 'a@b.com' }) + reports.trigger_subscribers(automation_id: 1, trigger_id: 2, status: 'completed', filter: { search: 'a@b.com' }) + expect(stub).to have_been_requested + end + end + + describe '#email_monthly_totals' do + it 'sends GET /automations/{automation}/monthly-totals/email with the query' do + stub = stub_api(:get, '/automations/1/monthly-totals/email', query: { 'start_date' => '2026-01-01', 'end_date' => '2026-06-30', 'shop_id' => '3', 'excel' => '1' }) + reports.email_monthly_totals(automation_id: 1, start_date: '2026-01-01', end_date: '2026-06-30', shop_id: '3', excel: true) + expect(stub).to have_been_requested + end + end +end diff --git a/spec/automation_steps_rspec.rb b/spec/automation_steps_rspec.rb new file mode 100644 index 0000000..864be4a --- /dev/null +++ b/spec/automation_steps_rspec.rb @@ -0,0 +1,76 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe MailerLite::AutomationSteps do + let(:client) { MailerLite::Client.new } + let(:steps) { described_class.new(client: client) } + + describe '#list' do + it 'sends GET /automations/{automation}/steps/index' do + stub = stub_api(:get, '/automations/1/steps/index') + steps.list(1) + expect(stub).to have_been_requested + end + end + + describe '#create' do + it 'sends POST /automations/{automation}/steps' do + stub = stub_api(:post, '/automations/1/steps', body: { + 'type' => 'action', 'parent_id' => 2, 'branch' => 'yes', 'move_to_branch' => 'no', + 'data' => { 'action_type' => 'copy_to_group', 'group_id' => '3' } + }) + steps.create(automation_id: 1, type: 'action', parent_id: 2, branch: 'yes', move_to_branch: 'no', + data: { 'action_type' => 'copy_to_group', 'group_id' => '3' }) + expect(stub).to have_been_requested + end + end + + describe '#fetch' do + it 'sends GET /automations/{automation}/steps/{step}' do + stub = stub_api(:get, '/automations/1/steps/2') + steps.fetch(automation_id: 1, step_id: 2) + expect(stub).to have_been_requested + end + end + + describe '#update' do + it 'sends PUT /automations/{automation}/steps/{step}' do + stub = stub_api(:put, '/automations/1/steps/2', body: { 'parent_id' => 3, 'data' => { 'value' => 2 }, 'reschedule_waiting_subscribers' => true }) + steps.update(automation_id: 1, step_id: 2, parent_id: 3, data: { 'value' => 2 }, reschedule_waiting_subscribers: true) + expect(stub).to have_been_requested + end + end + + describe '#delete' do + it 'sends DELETE /automations/{automation}/steps/{step} with branch_to_delete' do + stub = stub_api(:delete, '/automations/1/steps/2', query: { 'branch_to_delete' => 'yes' }) + steps.delete(automation_id: 1, step_id: 2, branch_to_delete: 'yes') + expect(stub).to have_been_requested + end + end + + describe '#copy' do + it 'sends POST /automations/{automation}/steps/{step}/copy' do + stub = stub_api(:post, '/automations/1/steps/2/copy', body: { 'parent_id' => 3, 'branch' => 'a_path', 'move_to_branch' => 'yes' }) + steps.copy(automation_id: 1, step_id: 2, parent_id: 3, branch: 'a_path', move_to_branch: 'yes') + expect(stub).to have_been_requested + end + end + + describe '#reorder' do + it 'sends POST /automations/{automation}/steps/reorder' do + stub = stub_api(:post, '/automations/1/steps/reorder', body: { 'dragged_step_id' => 2, 'new_parent_step_id' => 3, 'branch' => 'no', 'branch_for_child' => 'yes' }) + steps.reorder(automation_id: 1, dragged_step_id: 2, new_parent_step_id: 3, branch: 'no', branch_for_child: 'yes') + expect(stub).to have_been_requested + end + end + + describe '#select_ab_test_winner' do + it 'sends PUT /automations/{automation}/steps/{step}/select-ab-test-winner' do + stub = stub_api(:put, '/automations/1/steps/2/select-ab-test-winner', body: { 'ab_test_winner' => 'b_path' }) + steps.select_ab_test_winner(automation_id: 1, step_id: 2, ab_test_winner: 'b_path') + expect(stub).to have_been_requested + end + end +end diff --git a/spec/automation_templates_rspec.rb b/spec/automation_templates_rspec.rb new file mode 100644 index 0000000..bc457df --- /dev/null +++ b/spec/automation_templates_rspec.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe MailerLite::AutomationTemplates do + let(:client) { MailerLite::Client.new } + let(:templates) { described_class.new(client: client) } + + describe '#list' do + it 'sends GET /automation-templates with the query' do + stub = stub_api(:get, '/automation-templates', query: { 'filter[type]' => 'ecommerce', 'filter[category]' => '2', 'limit' => '10', 'page' => '3' }) + templates.list(filter: { type: 'ecommerce', category: 2 }, limit: 10, page: 3) + expect(stub).to have_been_requested + end + end + + describe '#select_template' do + it 'sends POST /automations/select-template/{automationTemplate}' do + stub = stub_api(:post, '/automations/select-template/welcome') + templates.select_template('welcome') + expect(stub).to have_been_requested + end + end +end diff --git a/spec/automation_triggers_rspec.rb b/spec/automation_triggers_rspec.rb new file mode 100644 index 0000000..e959e57 --- /dev/null +++ b/spec/automation_triggers_rspec.rb @@ -0,0 +1,48 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe MailerLite::AutomationTriggers do + let(:client) { MailerLite::Client.new } + let(:triggers) { described_class.new(client: client) } + + describe '#list' do + it 'sends GET /automations/{automation}/triggers' do + stub = stub_api(:get, '/automations/1/triggers') + triggers.list(1) + expect(stub).to have_been_requested + end + end + + describe '#create' do + it 'sends POST /automations/{automation}/triggers' do + stub = stub_api(:post, '/automations/1/triggers', body: { 'type' => 'subscriber_joins_group', 'data' => { 'group_ids' => ['2'] } }) + triggers.create(automation_id: 1, type: 'subscriber_joins_group', data: { 'group_ids' => ['2'] }) + expect(stub).to have_been_requested + end + end + + describe '#fetch' do + it 'sends GET /automations/{automation}/triggers/{trigger}' do + stub = stub_api(:get, '/automations/1/triggers/2') + triggers.fetch(automation_id: 1, trigger_id: 2) + expect(stub).to have_been_requested + end + end + + describe '#update' do + it 'sends PUT /automations/{automation}/triggers/{trigger}' do + stub = stub_api(:put, '/automations/1/triggers/2', body: { 'data' => { 'group_ids' => ['3'] } }) + triggers.update(automation_id: 1, trigger_id: 2, data: { 'group_ids' => ['3'] }) + expect(stub).to have_been_requested + end + end + + describe '#delete' do + it 'sends DELETE /automations/{automation}/triggers/{trigger}' do + stub = stub_api(:delete, '/automations/1/triggers/2') + triggers.delete(automation_id: 1, trigger_id: 2) + expect(stub).to have_been_requested + end + end +end diff --git a/spec/automations_rspec.rb b/spec/automations_rspec.rb index efc3b03..ea62894 100644 --- a/spec/automations_rspec.rb +++ b/spec/automations_rspec.rb @@ -60,4 +60,147 @@ end end end + + describe '#get with every filter' do + it 'sends the documented query params' do + stub = stub_api(:get, '/automations', query: { + 'filter[status]' => 'true', 'filter[name]' => 'Welcome', 'filter[group]' => '1', 'filter[folder]' => '2', + 'filter[enabled]' => '1', 'filter[automation_state]' => 'workflows', 'filter[drafts_state]' => 'complete_workflow', + 'limit' => '10', 'page' => '2', 'sort' => '-created_at', 'with_steps' => '0' + }) + + automations.get( + filter: { status: true, name: 'Welcome', group: 1, folder: 2, enabled: 1, automation_state: 'workflows', drafts_state: 'complete_workflow' }, + limit: 10, page: 2, sort: '-created_at', with_steps: false + ) + + expect(stub).to have_been_requested + end + end + + describe '#create' do + it 'sends POST /automations' do + stub = stub_api(:post, '/automations', body: { 'name' => 'Welcome' }) + automations.create(name: 'Welcome') + expect(stub).to have_been_requested + end + end + + describe '#update' do + it 'sends PUT /automations/{automation}' do + stub = stub_api(:put, '/automations/1', body: { 'name' => 'Welcome', 'trigger_data' => { 'group_ids' => ['2'] }, 'triggers' => [{ 'type' => 'manual' }] }) + automations.update(automation_id: 1, name: 'Welcome', trigger_data: { 'group_ids' => ['2'] }, triggers: [{ 'type' => 'manual' }]) + expect(stub).to have_been_requested + end + end + + describe '#delete' do + it 'sends DELETE /automations/{automation}' do + stub = stub_api(:delete, '/automations/1') + automations.delete(1) + expect(stub).to have_been_requested + end + end + + describe '#copy' do + it 'sends POST /automations/{automation}/copy' do + stub = stub_api(:post, '/automations/1/copy') + automations.copy(1) + expect(stub).to have_been_requested + end + end + + describe '#enable' do + it 'sends POST /automations/{automation}/enable' do + stub = stub_api(:post, '/automations/1/enable') + automations.enable(1) + expect(stub).to have_been_requested + end + end + + describe '#disable' do + it 'sends POST /automations/{automation}/disable' do + stub = stub_api(:post, '/automations/1/disable') + automations.disable(1) + expect(stub).to have_been_requested + end + end + + describe '#test' do + it 'sends POST /automations/{automation}/test' do + stub = stub_api(:post, '/automations/1/test', body: { 'email' => 'a@b.com' }) + automations.test(automation_id: 1, email: 'a@b.com') + expect(stub).to have_been_requested + end + end + + describe '#get_subscriber_activity search' do + it 'sends filter[search] and sort' do + stub = stub_api(:get, '/automations/1/activity', query: { 'filter[status]' => 'completed', 'filter[search]' => 'a@b.com', 'sort' => '-id' }) + automations.get_subscriber_activity(automation_id: 1, filter: { status: 'completed', search: 'a@b.com' }, sort: '-id') + expect(stub).to have_been_requested + end + + it 'sends the old filter[:keyword] as filter[search]' do + stub = stub_api(:get, '/automations/1/activity', query: { 'filter[status]' => 'completed', 'filter[search]' => 'a@b.com' }) + automations.get_subscriber_activity(automation_id: 1, filter: { status: 'completed', keyword: 'a@b.com' }) + expect(stub).to have_been_requested + end + end + + describe '#get_subscriber_activity with_subscribers' do + it 'sends with_subscribers=1 when true' do + stub = stub_api(:get, '/automations/1/activity', query: { 'filter[status]' => 'active', 'with_subscribers' => '1' }) + automations.get_subscriber_activity(automation_id: 1, filter: { status: 'active' }, with_subscribers: true) + expect(stub).to have_been_requested + end + + it 'omits with_subscribers when false' do + stub = stub_api(:get, '/automations/1/activity', query: { 'filter[status]' => 'active' }) + automations.get_subscriber_activity(automation_id: 1, filter: { status: 'active' }, with_subscribers: false) + expect(stub).to have_been_requested + end + end + + describe '#add_subscribers' do + it 'sends POST /automations/{automation}/add-subscribers' do + stub = stub_api(:post, '/automations/1/add-subscribers', body: { 'step_id' => 2, 'emails' => ['a@b.com'], 'groups' => ['3'] }) + automations.add_subscribers(automation_id: 1, step_id: 2, emails: ['a@b.com'], groups: ['3']) + expect(stub).to have_been_requested + end + end + + describe '#readd' do + it 'sends POST /automations/{automation}/readd' do + stub = stub_api(:post, '/automations/1/readd', body: { + 'readd_subscribers_to' => 'selected_step', 'selected_step_id' => '2', 'filter' => { 'status' => 'completed', 'id' => ['5'] } + }) + automations.readd(automation_id: 1, readd_subscribers_to: 'selected_step', selected_step_id: '2', filter: { status: 'completed', id: ['5'] }) + expect(stub).to have_been_requested + end + end + + describe '#add_activity_to_group' do + it 'sends POST /automations/{automation}/activity/add-to-group' do + stub = stub_api(:post, '/automations/1/activity/add-to-group', body: { 'destination_groups' => ['3'], 'filter' => { 'status' => 'active' } }) + automations.add_activity_to_group(automation_id: 1, destination_groups: ['3'], filter: { status: 'active' }) + expect(stub).to have_been_requested + end + end + + describe '#cancel_activity' do + it 'sends POST /automations/{automation}/activity/cancel-automation' do + stub = stub_api(:post, '/automations/1/activity/cancel-automation', body: { 'filter' => { 'status' => 'active', 'next_step' => 4 } }) + automations.cancel_activity(automation_id: 1, filter: { status: 'active', next_step: 4 }) + expect(stub).to have_been_requested + end + end + + describe '#skip_activity_delay' do + it 'sends POST /automations/{automation}/activity/skip-delay' do + stub = stub_api(:post, '/automations/1/activity/skip-delay', body: { 'filter' => { 'status' => 'active', 'search' => 'a@b.com' } }) + automations.skip_activity_delay(automation_id: 1, filter: { status: 'active', search: 'a@b.com' }) + expect(stub).to have_been_requested + end + end end diff --git a/spec/groups_rspec.rb b/spec/groups_rspec.rb index f5e9df7..1ff0a34 100644 --- a/spec/groups_rspec.rb +++ b/spec/groups_rspec.rb @@ -94,4 +94,25 @@ end end end + + describe '#get_subscribers query string' do + it 'sends the params in the query string with no body' do + stub = stub_api(:get, '/groups/1/subscribers', query: { 'filter[status]' => 'active', 'limit' => '10', 'cursor' => 'abc', 'include' => 'groups' }) + + groups.get_subscribers(group_id: 1, filter: { status: 'active' }, limit: 10, cursor: 'abc', include: 'groups') + + expect(stub).to have_been_requested + end + end + + describe '#import_subscribers' do + it 'sends POST /groups/{group}/import-subscribers with the body' do + list = [{ 'email' => 'a@b.com', 'name' => 'Ann', 'fields' => { 'city' => 'Vilnius' } }] + stub = stub_api(:post, '/groups/1/import-subscribers', body: { 'subscribers' => list, 'resubscribe' => true, 'trigger_automations' => false }) + + groups.import_subscribers(group_id: 1, subscribers: list, resubscribe: true, trigger_automations: false) + + expect(stub).to have_been_requested + end + end end diff --git a/spec/segments_rspec.rb b/spec/segments_rspec.rb index 225aae0..a7103e5 100644 --- a/spec/segments_rspec.rb +++ b/spec/segments_rspec.rb @@ -67,4 +67,43 @@ end end end + + describe '#list query string' do + it 'sends the params in the query string with no body' do + stub = stub_api(:get, '/segments', query: { 'limit' => '10', 'page' => '2', 'sort' => '-created_at' }) + + segments.list(limit: 10, page: 2, sort: '-created_at') + + expect(stub).to have_been_requested + end + end + + describe '#update with filter' do + it 'sends name and filter' do + rules = [[{ 'operator' => 'in_any', 'args' => %w[groups 1] }]] + stub = stub_api(:put, '/segments/1', body: { 'name' => 'VIP', 'filter' => rules }) + + segments.update(segment_id: 1, name: 'VIP', filter: rules) + + expect(stub).to have_been_requested + end + end + + describe '#get_subscribers cursor' do + it 'sends cursor' do + stub = stub_api(:get, '/segments/1/subscribers', query: { 'filter[status]' => 'active', 'limit' => '10', 'cursor' => 'abc' }) + + segments.get_subscribers(segment_id: 1, filter: { status: 'active' }, limit: 10, cursor: 'abc') + + expect(stub).to have_been_requested + end + + it 'still sends after' do + stub = stub_api(:get, '/segments/1/subscribers', query: { 'after' => '123' }) + + segments.get_subscribers(segment_id: 1, after: 123) + + expect(stub).to have_been_requested + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b5c9b3c..d2fa1d6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,6 +3,7 @@ require 'simplecov' SimpleCov.start require 'mailerlite' +require_relative 'support/request_stub' # This file was generated by the `rspec --init` command. Conventionally, all # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. # The generated `.rspec` file contains `--require spec_helper` which will cause diff --git a/spec/subscribers_rspec.rb b/spec/subscribers_rspec.rb index a79ee56..acbd85f 100644 --- a/spec/subscribers_rspec.rb +++ b/spec/subscribers_rspec.rb @@ -105,4 +105,61 @@ end end end + + describe '#fetch with every filter' do + it 'sends the documented query params' do + stub = stub_api(:get, '/subscribers', query: { + 'filter[status]' => 'active', 'filter[group]' => '1,2', 'filter[automation]' => '3', 'filter[form]' => '4', + 'filter[site]' => '5', 'filter[page]' => '6', 'filter[import]' => '7', 'filter[post]' => '8', + 'limit' => '10', 'cursor' => 'abc', 'sort' => '-id', 'include' => 'groups', + 'start_date' => '2026-01-01', 'end_date' => '2026-01-31', 'with_fields' => '1' + }) + + subscribers.fetch( + filter: { status: 'active', group: '1,2', automation: '3', form: '4', site: '5', page: '6', import: '7', post: '8' }, + limit: 10, cursor: 'abc', sort: '-id', include: 'groups', start_date: '2026-01-01', end_date: '2026-01-31', with_fields: true + ) + + expect(stub).to have_been_requested + end + + it 'sends Array filter values comma-separated' do + stub = stub_api(:get, '/subscribers', query: { 'filter[group]' => '1,2' }) + + subscribers.fetch(filter: { group: %w[1 2] }) + + expect(stub).to have_been_requested + end + end + + describe '#fetch filter order' do + it 'sends the filter keys in the order the caller gave them' do + uris = capture_raw_uris + stub_api(:get, '/subscribers', query: { 'filter[form]' => '4', 'filter[status]' => 'active', 'filter[group]' => '1', 'sort' => 'signup_date' }) + + subscribers.fetch(filter: { form: '4', status: 'active', group: '1' }, sort: 'signup_date') + + expect(uris).to eq ["#{MAILERLITE_API_URL}/subscribers?filter%5Bform%5D=4&filter%5Bstatus%5D=active&filter%5Bgroup%5D=1&sort=signup_date"] + end + end + + describe '#create with the new body fields' do + it 'sends resubscribe, double_optin and notes' do + stub = stub_api(:post, '/subscribers', body: { 'email' => 'a@b.com', 'resubscribe' => false, 'double_optin' => true, 'notes' => ['VIP'] }) + + subscribers.create(email: 'a@b.com', resubscribe: false, double_optin: true, notes: ['VIP']) + + expect(stub).to have_been_requested + end + end + + describe '#activity_log' do + it 'sends GET /subscribers/{subscriber}/activity-log with the query' do + stub = stub_api(:get, '/subscribers/1/activity-log', query: { 'filter[log_name]' => 'email_opened', 'limit' => '10', 'page' => '2' }) + + subscribers.activity_log(subscriber_id: 1, filter: { log_name: 'email_opened' }, limit: 10, page: 2) + + expect(stub).to have_been_requested + end + end end diff --git a/spec/support/request_stub.rb b/spec/support/request_stub.rb new file mode 100644 index 0000000..5408438 --- /dev/null +++ b/spec/support/request_stub.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require 'webmock/rspec' + +module RequestStub + def stub_api(method, path, query: nil, body: nil) + pattern = {} + pattern[:query] = query if query + pattern[:body] = body if body + stub_request(method, "#{MAILERLITE_API_URL}#{path}") + .with(**pattern) { |request| body || request.body.to_s.empty? } + .to_return(status: 200, body: '{}', headers: { 'Content-Type' => 'application/json' }) + end + + def capture_raw_uris + uris = [] + allow_any_instance_of(HTTP::Client).to receive(:perform).and_wrap_original do |method, request, options| + uris << request.uri.to_s + method.call(request, options) + end + uris + end +end + +RSpec.configure { |config| config.include RequestStub }