mailerlite-ruby 1.0.6 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. checksums.yaml +4 -4
  2. data/.envrc +2 -0
  3. data/.github/workflows/main.yml +4 -4
  4. data/.github/workflows/publish_gem.yml +5 -5
  5. data/.gitignore +2 -1
  6. data/.rubocop.yml +1 -1
  7. data/CHANGELOG.md +21 -0
  8. data/README.md +583 -1
  9. data/flake.lock +27 -0
  10. data/flake.nix +25 -0
  11. data/lefthook.yml +14 -0
  12. data/lib/mailerlite/api_response.rb +24 -0
  13. data/lib/mailerlite/automations/automation_emails.rb +57 -0
  14. data/lib/mailerlite/automations/automation_reports.rb +114 -0
  15. data/lib/mailerlite/automations/automation_steps.rb +105 -0
  16. data/lib/mailerlite/automations/automation_templates.rb +34 -0
  17. data/lib/mailerlite/automations/automation_triggers.rb +62 -0
  18. data/lib/mailerlite/automations/automations.rb +177 -8
  19. data/lib/mailerlite/brand_styles/brand_styles.rb +24 -0
  20. data/lib/mailerlite/campaigns/campaigns.rb +33 -1
  21. data/lib/mailerlite/client.rb +15 -5
  22. data/lib/mailerlite/ecommerce/ecommerce.rb +316 -0
  23. data/lib/mailerlite/emails/emails.rb +306 -0
  24. data/lib/mailerlite/errors.rb +88 -0
  25. data/lib/mailerlite/files/files.rb +45 -0
  26. data/lib/mailerlite/groups/groups.rb +16 -8
  27. data/lib/mailerlite/params.rb +58 -0
  28. data/lib/mailerlite/query_params.rb +33 -0
  29. data/lib/mailerlite/request.rb +24 -0
  30. data/lib/mailerlite/segments/segments.rb +10 -10
  31. data/lib/mailerlite/subscribers/subscribers.rb +63 -8
  32. data/lib/mailerlite/version.rb +1 -1
  33. data/lib/mailerlite.rb +14 -0
  34. data/mailerlite-ruby.gemspec +8 -8
  35. data/spec/api_response_rspec.rb +140 -0
  36. data/spec/automation_emails_rspec.rb +40 -0
  37. data/spec/automation_reports_rspec.rb +92 -0
  38. data/spec/automation_steps_rspec.rb +76 -0
  39. data/spec/automation_templates_rspec.rb +24 -0
  40. data/spec/automation_triggers_rspec.rb +48 -0
  41. data/spec/automations_rspec.rb +143 -0
  42. data/spec/brand_styles_rspec.rb +49 -0
  43. data/spec/campaigns_rspec.rb +42 -0
  44. data/spec/ecommerce_rspec.rb +141 -0
  45. data/spec/emails_rspec.rb +692 -0
  46. data/spec/files_rspec.rb +90 -0
  47. data/spec/fixtures/brand_styles.json +50 -0
  48. data/spec/fixtures/campaign_simple_editor.json +24 -0
  49. data/spec/fixtures/email.json +81 -0
  50. data/spec/fixtures/email_block.json +12 -0
  51. data/spec/fixtures/email_block_created.json +12 -0
  52. data/spec/fixtures/email_block_created_rejected.json +15 -0
  53. data/spec/fixtures/email_block_moved.json +12 -0
  54. data/spec/fixtures/email_block_updated.json +9 -0
  55. data/spec/fixtures/email_block_updated_rejected.json +13 -0
  56. data/spec/fixtures/email_block_validation_invalid.json +5 -0
  57. data/spec/fixtures/email_etag.json +1 -0
  58. data/spec/fixtures/email_html.json +1 -0
  59. data/spec/fixtures/email_settings.json +111 -0
  60. data/spec/fixtures/email_settings_validation.json +9 -0
  61. data/spec/fixtures/email_template.json +1 -0
  62. data/spec/fixtures/email_version.json +1 -0
  63. data/spec/fixtures/email_versions.json +26 -0
  64. data/spec/fixtures/files.json +44 -0
  65. data/spec/groups_rspec.rb +21 -0
  66. data/spec/params_rspec.rb +126 -0
  67. data/spec/request_rspec.rb +139 -0
  68. data/spec/segments_rspec.rb +39 -0
  69. data/spec/spec_helper.rb +1 -0
  70. data/spec/subscribers_rspec.rb +57 -0
  71. data/spec/support/json_fixture.rb +27 -0
  72. data/spec/support/request_stub.rb +25 -0
  73. metadata +69 -22
  74. /data/{renovate.json → .github/renovate.json} +0 -0
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailerLite
4
+ # Helpers that `Emails`, `BrandStyles` and `Files` add to each `HTTP::Response` that they return.
5
+ #
6
+ # The response is still an `HTTP::Response`. Use `response.code` for the status code,
7
+ # `response.headers` for the headers and `response.parse` for the decoded body.
8
+ module ApiResponse
9
+ # Returns true if the API returned the stored response of an earlier request with the same idempotency key.
10
+ #
11
+ # @return [Boolean] true if the `Idempotent-Replayed` header is "true"
12
+ def idempotent_replay?
13
+ # A proxy can change the case of the value, or send the header two times.
14
+ headers.get('Idempotent-Replayed').any? { |value| value.strip.casecmp?('true') }
15
+ end
16
+
17
+ # Returns the error of the response.
18
+ #
19
+ # @return [MailerLite::ApiError, nil] nil for a 2xx status, otherwise an error that you can raise
20
+ def api_error
21
+ status.success? ? nil : ApiError.from_response(self)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailerLite
4
+ # This is a class for manipulating the Automation emails from MailerLite API.
5
+ class AutomationEmails
6
+ attr_reader :client
7
+
8
+ # Inits the `AutomationEmails` class with the specified `client`.
9
+ #
10
+ # @param client [MailerLite::Client] the `Client` instance to use
11
+ def initialize(client: MailerLite::Client.new)
12
+ @client = client
13
+ end
14
+
15
+ # Returns the HTML content of the specified email
16
+ #
17
+ # @param automation_id [String] the ID of the Automation
18
+ # @param email_id [String] the ID of the email
19
+ # @return [HTTP::Response] the response from the API
20
+ def content(automation_id:, email_id:)
21
+ client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/emails/#{email_id}/content")
22
+ end
23
+
24
+ # Updates the HTML content of the specified email
25
+ #
26
+ # @param automation_id [String] the ID of the Automation
27
+ # @param email_id [String] the ID of the email
28
+ # @param html [String] the new HTML content
29
+ # @return [HTTP::Response] the response from the API
30
+ def update_content(automation_id:, email_id:, html:)
31
+ client.http.put("#{MAILERLITE_API_URL}/automations/#{automation_id}/emails/#{email_id}/content", json: { 'html' => html })
32
+ end
33
+
34
+ # Returns the report stats of the specified email
35
+ #
36
+ # @param automation_id [String] the ID of the Automation
37
+ # @param email_id [String] the ID of the email
38
+ # @param start_date [String] the first day, together with end_date
39
+ # @param end_date [String] the last day, included
40
+ # @param shop_id [String] report on one shop only, e-commerce figures only
41
+ # @param excel [Boolean] only used by export endpoints
42
+ # @return [HTTP::Response] the response from the API
43
+ def report_stats(automation_id:, email_id:, start_date: nil, end_date: nil, shop_id: nil, excel: nil)
44
+ params = QueryParams.report(start_date: start_date, end_date: end_date, shop_id: shop_id, excel: excel)
45
+ client.http.get(QueryParams.uri("#{MAILERLITE_API_URL}/automations/#{automation_id}/emails/#{email_id}/report-stats", params))
46
+ end
47
+
48
+ # Returns the reasons subscribers gave for unsubscribing from the specified email
49
+ #
50
+ # @param automation_id [String] the ID of the Automation
51
+ # @param email_id [String] the ID of the email
52
+ # @return [HTTP::Response] the response from the API
53
+ def unsubscribe_reasons(automation_id:, email_id:)
54
+ client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/emails/#{email_id}/unsubscribe-reasons")
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailerLite
4
+ # This is a class for reading the Automation reports from MailerLite API.
5
+ class AutomationReports
6
+ attr_reader :client
7
+
8
+ # Inits the `AutomationReports` class with the specified `client`.
9
+ #
10
+ # @param client [MailerLite::Client] the `Client` instance to use
11
+ def initialize(client: MailerLite::Client.new)
12
+ @client = client
13
+ end
14
+
15
+ # Returns the run counts of every step of the specified Automation
16
+ #
17
+ # @param automation_id [String] the ID of the Automation
18
+ # @return [HTTP::Response] the response from the API
19
+ def step_run_counts(automation_id)
20
+ client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps/run-counts")
21
+ end
22
+
23
+ # Returns the run counts of the specified step
24
+ #
25
+ # @param automation_id [String] the ID of the Automation
26
+ # @param step_id [String] the ID of the step
27
+ # @return [HTTP::Response] the response from the API
28
+ def step_run_count(automation_id:, step_id:)
29
+ client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps/run-counts/#{step_id}")
30
+ end
31
+
32
+ # Returns the run counts of every trigger of the specified Automation
33
+ #
34
+ # @param automation_id [String] the ID of the Automation
35
+ # @return [HTTP::Response] the response from the API
36
+ def trigger_run_counts(automation_id)
37
+ client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/triggers/run-counts")
38
+ end
39
+
40
+ # Returns the run counts of the specified trigger
41
+ #
42
+ # @param automation_id [String] the ID of the Automation
43
+ # @param trigger_id [String] the ID of the trigger
44
+ # @return [HTTP::Response] the response from the API
45
+ def trigger_run_count(automation_id:, trigger_id:)
46
+ client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/triggers/run-counts/#{trigger_id}")
47
+ end
48
+
49
+ # Returns the stats of every A/B test step of the specified Automation
50
+ #
51
+ # @param automation_id [String] the ID of the Automation
52
+ # @return [HTTP::Response] the response from the API
53
+ def ab_steps_stats(automation_id)
54
+ client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/ab-steps-stats")
55
+ end
56
+
57
+ # Returns the stats of the specified A/B test step
58
+ #
59
+ # @param automation_id [String] the ID of the Automation
60
+ # @param step_id [String] the ID of the A/B test step
61
+ # @return [HTTP::Response] the response from the API
62
+ def ab_step_stats(automation_id:, step_id:)
63
+ client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/ab-steps-stats/#{step_id}")
64
+ end
65
+
66
+ # Returns the subscribers who reached the specified step
67
+ #
68
+ # @param automation_id [String] the ID of the Automation
69
+ # @param step_id [String] the ID of the step
70
+ # @param status [String] queued or completed
71
+ # @param filter[:date_from,:date_to,:search,:id] [Hash] filters for the subscribers
72
+ # @param limit [Integer] the maximum number of subscribers to return
73
+ # @param page [Integer] the page number of the results to return
74
+ # @param sort [String] the field to sort by, prefix with - for descending order
75
+ # @return [HTTP::Response] the response from the API
76
+ def step_subscribers(automation_id:, step_id:, status:, filter: {}, limit: nil, page: nil, sort: nil)
77
+ activity_subscribers("#{automation_id}/activity/steps/#{step_id}/subscribers", status: status, filter: filter, limit: limit, page: page, sort: sort)
78
+ end
79
+
80
+ # Returns the subscribers who passed the specified trigger
81
+ #
82
+ # @param automation_id [String] the ID of the Automation
83
+ # @param trigger_id [String] the ID of the trigger
84
+ # @param status [String] queued or completed
85
+ # @param filter[:date_from,:date_to,:search,:id] [Hash] filters for the subscribers
86
+ # @param limit [Integer] the maximum number of subscribers to return
87
+ # @param page [Integer] the page number of the results to return
88
+ # @param sort [String] the field to sort by, prefix with - for descending order
89
+ # @return [HTTP::Response] the response from the API
90
+ def trigger_subscribers(automation_id:, trigger_id:, status:, filter: {}, limit: nil, page: nil, sort: nil)
91
+ activity_subscribers("#{automation_id}/activity/triggers/#{trigger_id}/subscribers", status: status, filter: filter, limit: limit, page: page, sort: sort)
92
+ end
93
+
94
+ # Returns the email totals of the specified Automation per month
95
+ #
96
+ # @param automation_id [String] the ID of the Automation
97
+ # @param start_date [String] the first day, together with end_date
98
+ # @param end_date [String] the last day, included
99
+ # @param shop_id [String] report on one shop only, e-commerce figures only
100
+ # @param excel [Boolean] only used by export endpoints
101
+ # @return [HTTP::Response] the response from the API
102
+ def email_monthly_totals(automation_id:, start_date: nil, end_date: nil, shop_id: nil, excel: nil)
103
+ params = QueryParams.report(start_date: start_date, end_date: end_date, shop_id: shop_id, excel: excel)
104
+ client.http.get(QueryParams.uri("#{MAILERLITE_API_URL}/automations/#{automation_id}/monthly-totals/email", params))
105
+ end
106
+
107
+ private
108
+
109
+ def activity_subscribers(path, status:, filter:, **query)
110
+ params = { 'status' => status, **query.transform_keys(&:to_s) }.merge(QueryParams.filter(filter, %i[date_from date_to search id]))
111
+ client.http.get(QueryParams.uri("#{MAILERLITE_API_URL}/automations/#{path}", params))
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailerLite
4
+ # This is a class for manipulating the Automation steps from MailerLite API.
5
+ class AutomationSteps
6
+ attr_reader :client
7
+
8
+ # Inits the `AutomationSteps` class with the specified `client`.
9
+ #
10
+ # @param client [MailerLite::Client] the `Client` instance to use
11
+ def initialize(client: MailerLite::Client.new)
12
+ @client = client
13
+ end
14
+
15
+ # Returns the steps of the specified Automation
16
+ #
17
+ # @param automation_id [String] the ID of the Automation
18
+ # @return [HTTP::Response] the response from the API
19
+ def list(automation_id)
20
+ client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps/index")
21
+ end
22
+
23
+ # Adds a step to the specified Automation
24
+ #
25
+ # @param automation_id [String] the ID of the Automation
26
+ # @param type [String] email, condition, delay, action, abtest or webhook
27
+ # @param parent_id [Integer] the ID of the step to add the new step after
28
+ # @param branch [String] the branch of the parent step: yes, no, a_path, b_path or c_path
29
+ # @param move_to_branch [String] the branch of the new step to move the following steps to: yes or no
30
+ # @param data [Hash] the settings of the step
31
+ # @return [HTTP::Response] the response from the API
32
+ def create(automation_id:, type:, parent_id: nil, branch: nil, move_to_branch: nil, data: nil)
33
+ params = { 'type' => type, 'parent_id' => parent_id, 'branch' => branch, 'move_to_branch' => move_to_branch, 'data' => data }
34
+ client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps", json: params.compact)
35
+ end
36
+
37
+ # Returns the specified step
38
+ #
39
+ # @param automation_id [String] the ID of the Automation
40
+ # @param step_id [String] the ID of the step
41
+ # @return [HTTP::Response] the response from the API
42
+ def fetch(automation_id:, step_id:)
43
+ client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps/#{step_id}")
44
+ end
45
+
46
+ # Updates the specified step
47
+ #
48
+ # @param automation_id [String] the ID of the Automation
49
+ # @param step_id [String] the ID of the step
50
+ # @param parent_id [Integer] the ID of the parent step
51
+ # @param data [Hash] the settings of the step
52
+ # @param reschedule_waiting_subscribers [Boolean] reschedule the subscribers waiting on a delay step
53
+ # @return [HTTP::Response] the response from the API
54
+ def update(automation_id:, step_id:, parent_id: nil, data: nil, reschedule_waiting_subscribers: nil)
55
+ params = { 'parent_id' => parent_id, 'data' => data, 'reschedule_waiting_subscribers' => reschedule_waiting_subscribers }
56
+ client.http.put("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps/#{step_id}", json: params.compact)
57
+ end
58
+
59
+ # Deletes the specified step
60
+ #
61
+ # @param automation_id [String] the ID of the Automation
62
+ # @param step_id [String] the ID of the step
63
+ # @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
64
+ # @return [HTTP::Response] the response from the API
65
+ def delete(automation_id:, step_id:, branch_to_delete: nil)
66
+ client.http.delete(QueryParams.uri("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps/#{step_id}", { 'branch_to_delete' => branch_to_delete }))
67
+ end
68
+
69
+ # Copies the specified step
70
+ #
71
+ # @param automation_id [String] the ID of the Automation
72
+ # @param step_id [String] the ID of the step to copy
73
+ # @param parent_id [Integer] the ID of the step to add the copy after
74
+ # @param branch [String] the branch of the parent step: yes, no, a_path, b_path or c_path
75
+ # @param move_to_branch [String] the branch of the copy to move the following steps to: yes or no
76
+ # @return [HTTP::Response] the response from the API
77
+ def copy(automation_id:, step_id:, parent_id: nil, branch: nil, move_to_branch: nil)
78
+ params = { 'parent_id' => parent_id, 'branch' => branch, 'move_to_branch' => move_to_branch }
79
+ client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps/#{step_id}/copy", json: params.compact)
80
+ end
81
+
82
+ # Moves a step to another place in the specified Automation
83
+ #
84
+ # @param automation_id [String] the ID of the Automation
85
+ # @param dragged_step_id [Integer] the ID of the step to move
86
+ # @param new_parent_step_id [Integer] the ID of the step to move it after
87
+ # @param branch [String] the branch of the new parent step: yes, no, a_path, b_path or c_path
88
+ # @param branch_for_child [String] the branch of the moved step to attach the following steps to: yes or no
89
+ # @return [HTTP::Response] the response from the API
90
+ def reorder(automation_id:, dragged_step_id:, new_parent_step_id: nil, branch: nil, branch_for_child: nil)
91
+ params = { 'dragged_step_id' => dragged_step_id, 'new_parent_step_id' => new_parent_step_id, 'branch' => branch, 'branch_for_child' => branch_for_child }
92
+ client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps/reorder", json: params.compact)
93
+ end
94
+
95
+ # Selects the winning path of the specified A/B test step
96
+ #
97
+ # @param automation_id [String] the ID of the Automation
98
+ # @param step_id [String] the ID of the A/B test step
99
+ # @param ab_test_winner [String] a_path, b_path or c_path
100
+ # @return [HTTP::Response] the response from the API
101
+ def select_ab_test_winner(automation_id:, step_id:, ab_test_winner:)
102
+ client.http.put("#{MAILERLITE_API_URL}/automations/#{automation_id}/steps/#{step_id}/select-ab-test-winner", json: { 'ab_test_winner' => ab_test_winner })
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailerLite
4
+ # This is a class for manipulating the Automation templates from MailerLite API.
5
+ class AutomationTemplates
6
+ attr_reader :client
7
+
8
+ # Inits the `AutomationTemplates` class with the specified `client`.
9
+ #
10
+ # @param client [MailerLite::Client] the `Client` instance to use
11
+ def initialize(client: MailerLite::Client.new)
12
+ @client = client
13
+ end
14
+
15
+ # Returns a list of Automation templates that match the specified filter criteria.
16
+ #
17
+ # @param filter[:type,:category] [Hash] filters for the templates
18
+ # @param limit [Integer] the maximum number of templates to return
19
+ # @param page [Integer] the page number of the results to return
20
+ # @return [HTTP::Response] the response from the API
21
+ def list(filter: {}, limit: nil, page: nil)
22
+ params = QueryParams.filter(filter, %i[type category]).merge('limit' => limit, 'page' => page)
23
+ client.http.get(QueryParams.uri("#{MAILERLITE_API_URL}/automation-templates", params))
24
+ end
25
+
26
+ # Creates an Automation from the specified template
27
+ #
28
+ # @param template_id [String] the ID of the Automation template
29
+ # @return [HTTP::Response] the response from the API
30
+ def select_template(template_id)
31
+ client.http.post("#{MAILERLITE_API_URL}/automations/select-template/#{template_id}")
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailerLite
4
+ # This is a class for manipulating the Automation triggers from MailerLite API.
5
+ class AutomationTriggers
6
+ attr_reader :client
7
+
8
+ # Inits the `AutomationTriggers` class with the specified `client`.
9
+ #
10
+ # @param client [MailerLite::Client] the `Client` instance to use
11
+ def initialize(client: MailerLite::Client.new)
12
+ @client = client
13
+ end
14
+
15
+ # Returns the triggers of the specified Automation
16
+ #
17
+ # @param automation_id [String] the ID of the Automation
18
+ # @return [HTTP::Response] the response from the API
19
+ def list(automation_id)
20
+ client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/triggers")
21
+ end
22
+
23
+ # Adds a trigger to the specified Automation
24
+ #
25
+ # @param automation_id [String] the ID of the Automation
26
+ # @param type [String] the trigger type, for example subscriber_joins_group
27
+ # @param data [Hash] the settings of the trigger
28
+ # @return [HTTP::Response] the response from the API
29
+ def create(automation_id:, type:, data: nil)
30
+ params = { 'type' => type, 'data' => data }
31
+ client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/triggers", json: params.compact)
32
+ end
33
+
34
+ # Returns the specified trigger
35
+ #
36
+ # @param automation_id [String] the ID of the Automation
37
+ # @param trigger_id [String] the ID of the trigger
38
+ # @return [HTTP::Response] the response from the API
39
+ def fetch(automation_id:, trigger_id:)
40
+ client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}/triggers/#{trigger_id}")
41
+ end
42
+
43
+ # Updates the specified trigger
44
+ #
45
+ # @param automation_id [String] the ID of the Automation
46
+ # @param trigger_id [String] the ID of the trigger
47
+ # @param data [Hash] the settings of the trigger
48
+ # @return [HTTP::Response] the response from the API
49
+ def update(automation_id:, trigger_id:, data:)
50
+ client.http.put("#{MAILERLITE_API_URL}/automations/#{automation_id}/triggers/#{trigger_id}", json: { 'data' => data })
51
+ end
52
+
53
+ # Deletes the specified trigger
54
+ #
55
+ # @param automation_id [String] the ID of the Automation
56
+ # @param trigger_id [String] the ID of the trigger
57
+ # @return [HTTP::Response] the response from the API
58
+ def delete(automation_id:, trigger_id:)
59
+ client.http.delete("#{MAILERLITE_API_URL}/automations/#{automation_id}/triggers/#{trigger_id}")
60
+ end
61
+ end
62
+ end
@@ -14,17 +14,21 @@ module MailerLite
14
14
 
15
15
  # Returns a list of Automations that match the specified filter criteria.
16
16
  #
17
- # @param filter[:status,:name,:group] [Array] filters for automation
17
+ # @param filter[:status,:name,:group,:folder,:enabled,:automation_state,:drafts_state] [Array] filters for automation
18
18
  # @param limit [Integer] the maximum number of Automations to return
19
19
  # @param page [Integer] the page number of the results to return
20
+ # @param sort [String] the field to sort by, prefix with - for descending order
21
+ # @param with_steps [Boolean] include the steps of each Automation, defaults to true
20
22
  # @return [HTTP::Response] the response from the API
21
- def get(limit: nil, page: nil, filter: {})
23
+ def get(limit: nil, page: nil, filter: {}, sort: nil, with_steps: nil)
22
24
  params = {}
23
- params['filter[status]'] = filter[:status] if filter.key?(:status)
24
- params['filter[name]'] = filter[:name] if filter.key?(:name)
25
- params['filter[group]'] = filter[:group] if filter.key?(:group)
25
+ %i[status name group folder enabled automation_state drafts_state].each do |key|
26
+ params["filter[#{key}]"] = filter[key] if filter.key?(key)
27
+ end
26
28
  params['limit'] = limit if limit
27
29
  params['page'] = page if page
30
+ params['sort'] = sort if sort
31
+ params['with_steps'] = QueryParams.boolean(with_steps)
28
32
  uri = URI("#{MAILERLITE_API_URL}/automations")
29
33
  uri.query = URI.encode_www_form(params.compact)
30
34
  client.http.get(uri)
@@ -38,25 +42,190 @@ module MailerLite
38
42
  client.http.get("#{MAILERLITE_API_URL}/automations/#{automation_id}")
39
43
  end
40
44
 
45
+ # Creates an Automation
46
+ #
47
+ # @param name [String] the name of the Automation
48
+ # @return [HTTP::Response] the response from the API
49
+ def create(name:)
50
+ client.http.post("#{MAILERLITE_API_URL}/automations", json: { 'name' => name })
51
+ end
52
+
53
+ # Updates the specified Automation
54
+ #
55
+ # @param automation_id [String] the ID of the Automation to update
56
+ # @param name [String] the name of the Automation
57
+ # @param trigger_data [Hash] the settings of the first trigger
58
+ # @param triggers [Array] the triggers of the Automation
59
+ # @return [HTTP::Response] the response from the API
60
+ def update(automation_id:, name:, trigger_data: nil, triggers: nil)
61
+ params = { 'name' => name, 'trigger_data' => trigger_data, 'triggers' => triggers }
62
+ client.http.put("#{MAILERLITE_API_URL}/automations/#{automation_id}", json: params.compact)
63
+ end
64
+
65
+ # Deletes the specified Automation
66
+ #
67
+ # @param automation_id [String] the ID of the Automation to delete
68
+ # @return [HTTP::Response] the response from the API
69
+ def delete(automation_id)
70
+ client.http.delete("#{MAILERLITE_API_URL}/automations/#{automation_id}")
71
+ end
72
+
73
+ # Copies the specified Automation
74
+ #
75
+ # @param automation_id [String] the ID of the Automation to copy
76
+ # @return [HTTP::Response] the response from the API
77
+ def copy(automation_id)
78
+ client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/copy")
79
+ end
80
+
81
+ # Enables the specified Automation
82
+ #
83
+ # @param automation_id [String] the ID of the Automation to enable
84
+ # @return [HTTP::Response] the response from the API
85
+ def enable(automation_id)
86
+ client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/enable")
87
+ end
88
+
89
+ # Disables the specified Automation
90
+ #
91
+ # @param automation_id [String] the ID of the Automation to disable
92
+ # @return [HTTP::Response] the response from the API
93
+ def disable(automation_id)
94
+ client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/disable")
95
+ end
96
+
97
+ # Sends the emails of the specified Automation to a test address
98
+ #
99
+ # @param automation_id [String] the ID of the Automation to test
100
+ # @param email [String] the address to send the test emails to
101
+ # @return [HTTP::Response] the response from the API
102
+ def test(automation_id:, email:)
103
+ client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/test", json: { 'email' => email })
104
+ end
105
+
106
+ # Adds subscribers to the specified Automation
107
+ #
108
+ # @param automation_id [String] the ID of the Automation
109
+ # @param step_id [Integer] the step to start the subscribers at
110
+ # @param emails [Array] the email addresses of the subscribers to add
111
+ # @param groups [Array] the IDs of the groups whose subscribers to add
112
+ # @return [HTTP::Response] the response from the API
113
+ def add_subscribers(automation_id:, step_id: nil, emails: nil, groups: nil)
114
+ params = { 'step_id' => step_id, 'emails' => emails, 'groups' => groups }
115
+ client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/add-subscribers", json: params.compact)
116
+ end
117
+
118
+ # Adds subscribers back to the specified Automation
119
+ #
120
+ # @param automation_id [String] the ID of the Automation
121
+ # @param readd_subscribers_to [String] last_completed_step, selected_step or last_failed_step
122
+ # @param filter [Hash] the activity to re-add, with status and optional search, date_from, date_to, id and step
123
+ # @param selected_step_id [String] the step to re-add the subscribers to, when readd_subscribers_to is selected_step
124
+ # @return [HTTP::Response] the response from the API
125
+ def readd(automation_id:, readd_subscribers_to:, filter:, selected_step_id: nil)
126
+ params = { 'readd_subscribers_to' => readd_subscribers_to, 'filter' => filter, 'selected_step_id' => selected_step_id }
127
+ client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/readd", json: params.compact)
128
+ end
129
+
130
+ # Adds the subscribers in the activity of the specified Automation to groups
131
+ #
132
+ # @param automation_id [String] the ID of the Automation
133
+ # @param destination_groups [Array] the IDs of the groups to add the subscribers to
134
+ # @param filter [Hash] the activity to act on, with status and optional id, search and date filters
135
+ # @return [HTTP::Response] the response from the API
136
+ def add_activity_to_group(automation_id:, destination_groups:, filter:)
137
+ params = { 'destination_groups' => destination_groups, 'filter' => filter }
138
+ client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/activity/add-to-group", json: params)
139
+ end
140
+
141
+ # Cancels the active runs in the activity of the specified Automation
142
+ #
143
+ # @param automation_id [String] the ID of the Automation
144
+ # @param filter [Hash] the activity to act on, with status active and optional id, search and date filters
145
+ # @return [HTTP::Response] the response from the API
146
+ def cancel_activity(automation_id:, filter:)
147
+ client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/activity/cancel-automation", json: { 'filter' => filter })
148
+ end
149
+
150
+ # Skips the current delay of the active runs in the activity of the specified Automation
151
+ #
152
+ # @param automation_id [String] the ID of the Automation
153
+ # @param filter [Hash] the activity to act on, with status active and optional id, search and date filters
154
+ # @return [HTTP::Response] the response from the API
155
+ def skip_activity_delay(automation_id:, filter:)
156
+ client.http.post("#{MAILERLITE_API_URL}/automations/#{automation_id}/activity/skip-delay", json: { 'filter' => filter })
157
+ end
158
+
41
159
  # get_subscriber_activity the subscriber activity for specified Automation
42
160
  #
43
161
  # @param automation_id [Integer] the ID of the Automation to get_subscriber_activity
44
- # @param filter[:status,:date_from,:date_to,:scheduled_from,:scheduled_to,:keyword] [Array] Must be one of the following: completed, active, canceled, failed
162
+ # @param filter[:status,:date_from,:date_to,:scheduled_from,:scheduled_to,:search] [Array] Must be one of the following: completed, active, canceled, failed
163
+ # @param filter[:keyword] [String] deprecated, sent as filter[search], use filter[:search]
45
164
  # @param limit [Integer] the maximum number of Automations to return
46
165
  # @param page [Integer] the page number of the results to return
166
+ # @param sort [String] the field to sort by, prefix with - for descending order
167
+ # @param with_subscribers [Boolean] include the id, email and name of each subscriber
47
168
  # @return [HTTP::Response] the response from the API
48
- def get_subscriber_activity(automation_id:, filter: {}, page: nil, limit: nil)
169
+ def get_subscriber_activity(automation_id:, filter: {}, page: nil, limit: nil, sort: nil, with_subscribers: nil)
49
170
  params = { 'filter[status]' => filter[:status] }
50
171
  params['filter[date_from]'] = filter[:date_from] if filter.key?(:date_from)
51
172
  params['filter[date_to]'] = filter[:date_to] if filter.key?(:date_to)
52
173
  params['filter[scheduled_from]'] = filter[:scheduled_from] if filter.key?(:scheduled_from)
53
174
  params['filter[scheduled_to]'] = filter[:scheduled_to] if filter.key?(:scheduled_to)
54
- params['filter[keyword]'] = filter[:keyword] if filter.key?(:keyword)
175
+ params['filter[search]'] = filter.fetch(:search, filter[:keyword])
55
176
  params['page'] = page if page
56
177
  params['limit'] = limit if limit
178
+ params['sort'] = sort if sort
179
+ params['with_subscribers'] = 1 if with_subscribers
57
180
  uri = URI("#{MAILERLITE_API_URL}/automations/#{automation_id}/activity")
58
181
  uri.query = URI.encode_www_form(params.compact)
59
182
  client.http.get(uri)
60
183
  end
184
+
185
+ # Returns the orders and revenue an automation earned
186
+ #
187
+ # @param automation_id [String] the ID of the Automation
188
+ # @param shop_id [String] report on one shop only
189
+ # @param start_date [String] the first day, together with end_date
190
+ # @param end_date [String] the last day, included
191
+ # @return [HTTP::Response] the response from the API
192
+ def ecommerce_stats(automation_id:, shop_id: nil, start_date: nil, end_date: nil)
193
+ uri = URI("#{MAILERLITE_API_URL}/automations/#{automation_id}/monthly-totals/ecommerce")
194
+ query = { 'shop_id' => shop_id, 'start_date' => start_date, 'end_date' => end_date }.compact
195
+ uri.query = URI.encode_www_form(query) unless query.empty?
196
+ client.http.get(uri)
197
+ end
198
+
199
+ # Returns the orders and revenue one automation email earned
200
+ #
201
+ # @param automation_id [String] the ID of the Automation
202
+ # @param email_id [String] the ID of the email in the Automation
203
+ # @return [HTTP::Response] the response from the API
204
+ def email_ecommerce_stats(automation_id:, email_id:, shop_id: nil, start_date: nil, end_date: nil)
205
+ uri = URI("#{MAILERLITE_API_URL}/automations/#{automation_id}/emails/#{email_id}/ecommerce-overview-stats")
206
+ query = { 'shop_id' => shop_id, 'start_date' => start_date, 'end_date' => end_date }.compact
207
+ uri.query = URI.encode_www_form(query) unless query.empty?
208
+ client.http.get(uri)
209
+ end
210
+
211
+ # Returns the orders attributed to an automation
212
+ #
213
+ # @param automation_id [String] the ID of the Automation
214
+ # @param time_frame [String] week, month, two_months, three_months, six_months, year, last_year, all_time or custom
215
+ # @param time_frame_start [String] the first day, when time_frame is custom
216
+ # @param time_frame_end [String] the last day, when time_frame is custom
217
+ # @return [HTTP::Response] the response from the API
218
+ def ecommerce_orders(automation_id:, time_frame:, time_frame_start: nil, time_frame_end: nil, shop_id: nil, page: nil, limit: nil)
219
+ uri = URI("#{MAILERLITE_API_URL}/automations/#{automation_id}/order-activity")
220
+ uri.query = URI.encode_www_form({
221
+ 'time_frame' => time_frame,
222
+ 'time_frame_start' => time_frame_start,
223
+ 'time_frame_end' => time_frame_end,
224
+ 'ecommerce_shop_id' => shop_id,
225
+ 'page' => page,
226
+ 'limit' => limit
227
+ }.compact)
228
+ client.http.get(uri)
229
+ end
61
230
  end
62
231
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailerLite
4
+ # This is a class for the brand styles from MailerLite API.
5
+ class BrandStyles
6
+ attr_reader :client
7
+
8
+ # Inits the `BrandStyles` class with the specified `client`.
9
+ #
10
+ # @param client [MailerLite::Client] the `Client` instance to use
11
+ def initialize(client: MailerLite::Client.new)
12
+ @client = client
13
+ end
14
+
15
+ # Returns the palettes of the account: `brand` (the brand styles of the account, or nil if it has none),
16
+ # `default` and the `presets`. Match a palette on its `identifier`, not on its `name`.
17
+ # To apply a preset to an email, send its `identifier` in `brand.style` with `Emails#update_settings`.
18
+ #
19
+ # @return [HTTP::Response] the response from the API, with the `MailerLite::ApiResponse` methods
20
+ def get
21
+ Request.call(client, :get, '/brand-styles')
22
+ end
23
+ end
24
+ end