mailerlite-ruby 2.0.0 → 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 (71) hide show
  1. checksums.yaml +4 -4
  2. data/.envrc +2 -0
  3. data/.github/workflows/main.yml +1 -1
  4. data/.github/workflows/publish_gem.yml +1 -1
  5. data/.gitignore +2 -1
  6. data/CHANGELOG.md +17 -0
  7. data/README.md +574 -0
  8. data/flake.lock +27 -0
  9. data/flake.nix +25 -0
  10. data/lefthook.yml +14 -0
  11. data/lib/mailerlite/api_response.rb +24 -0
  12. data/lib/mailerlite/automations/automation_emails.rb +57 -0
  13. data/lib/mailerlite/automations/automation_reports.rb +114 -0
  14. data/lib/mailerlite/automations/automation_steps.rb +105 -0
  15. data/lib/mailerlite/automations/automation_templates.rb +34 -0
  16. data/lib/mailerlite/automations/automation_triggers.rb +62 -0
  17. data/lib/mailerlite/automations/automations.rb +177 -8
  18. data/lib/mailerlite/brand_styles/brand_styles.rb +24 -0
  19. data/lib/mailerlite/campaigns/campaigns.rb +33 -1
  20. data/lib/mailerlite/ecommerce/ecommerce.rb +316 -0
  21. data/lib/mailerlite/emails/emails.rb +306 -0
  22. data/lib/mailerlite/errors.rb +88 -0
  23. data/lib/mailerlite/files/files.rb +45 -0
  24. data/lib/mailerlite/groups/groups.rb +16 -8
  25. data/lib/mailerlite/params.rb +58 -0
  26. data/lib/mailerlite/query_params.rb +33 -0
  27. data/lib/mailerlite/request.rb +24 -0
  28. data/lib/mailerlite/segments/segments.rb +10 -10
  29. data/lib/mailerlite/subscribers/subscribers.rb +63 -8
  30. data/lib/mailerlite/version.rb +1 -1
  31. data/lib/mailerlite.rb +14 -0
  32. data/spec/api_response_rspec.rb +140 -0
  33. data/spec/automation_emails_rspec.rb +40 -0
  34. data/spec/automation_reports_rspec.rb +92 -0
  35. data/spec/automation_steps_rspec.rb +76 -0
  36. data/spec/automation_templates_rspec.rb +24 -0
  37. data/spec/automation_triggers_rspec.rb +48 -0
  38. data/spec/automations_rspec.rb +143 -0
  39. data/spec/brand_styles_rspec.rb +49 -0
  40. data/spec/campaigns_rspec.rb +42 -0
  41. data/spec/ecommerce_rspec.rb +141 -0
  42. data/spec/emails_rspec.rb +692 -0
  43. data/spec/files_rspec.rb +90 -0
  44. data/spec/fixtures/brand_styles.json +50 -0
  45. data/spec/fixtures/campaign_simple_editor.json +24 -0
  46. data/spec/fixtures/email.json +81 -0
  47. data/spec/fixtures/email_block.json +12 -0
  48. data/spec/fixtures/email_block_created.json +12 -0
  49. data/spec/fixtures/email_block_created_rejected.json +15 -0
  50. data/spec/fixtures/email_block_moved.json +12 -0
  51. data/spec/fixtures/email_block_updated.json +9 -0
  52. data/spec/fixtures/email_block_updated_rejected.json +13 -0
  53. data/spec/fixtures/email_block_validation_invalid.json +5 -0
  54. data/spec/fixtures/email_etag.json +1 -0
  55. data/spec/fixtures/email_html.json +1 -0
  56. data/spec/fixtures/email_settings.json +111 -0
  57. data/spec/fixtures/email_settings_validation.json +9 -0
  58. data/spec/fixtures/email_template.json +1 -0
  59. data/spec/fixtures/email_version.json +1 -0
  60. data/spec/fixtures/email_versions.json +26 -0
  61. data/spec/fixtures/files.json +44 -0
  62. data/spec/groups_rspec.rb +21 -0
  63. data/spec/params_rspec.rb +126 -0
  64. data/spec/request_rspec.rb +139 -0
  65. data/spec/segments_rspec.rb +39 -0
  66. data/spec/spec_helper.rb +1 -0
  67. data/spec/subscribers_rspec.rb +57 -0
  68. data/spec/support/json_fixture.rb +27 -0
  69. data/spec/support/request_stub.rb +25 -0
  70. metadata +54 -7
  71. /data/{renovate.json → .github/renovate.json} +0 -0
@@ -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
@@ -3,6 +3,9 @@
3
3
  module MailerLite
4
4
  # This is a class for manipulating the Campaigns from MailerLite API.
5
5
  class Campaigns
6
+ # The value of `editor` in the emails of `create` for the simple editor.
7
+ EDITOR_SIMPLE = 'simple'
8
+
6
9
  attr_reader :client
7
10
 
8
11
  # Inits the `Campaigns` class with the specified `client`.
@@ -34,7 +37,13 @@ module MailerLite
34
37
  # @param name [String] Maximum string length of 255 characters
35
38
  # @param language_id [integer] Used to define the language in the unsubscribe template. Must be a valid language id. Defaults to english
36
39
  # @param type [String] Must be one of the following: regular, ab, resend. Type resend is only available for accounts on growing or advanced plans
37
- # @param emails [Array] Must contain 1 email object item
40
+ # @param emails [Array] Must contain 1 email object item. The SDK sends each email as it is. An email can also have these keys:
41
+ # - `editor`: set it to `simple` (`EDITOR_SIMPLE`) to create the email in the simple editor. Do not send `content` with it.
42
+ # It must be the same on every email of the campaign. It is not available on every plan: for an account on a plan without it, the API returns an error.
43
+ # - `template_id`: the simple editor template that the email starts from. It is a template from the shared gallery, or a template that you saved from your own email
44
+ # with `Emails#create_template` (the `id` in its response). It is optional, and allowed only when `editor` is `simple`. Without it, the email starts from the
45
+ # default blank template. The API copies the template into the new email and does not change the template. It must be the same on every email of the campaign.
46
+ # Then edit the email with `MailerLite::Emails`. Its ID is in `data.emails[0].id` of the response.
38
47
  # @param groups [Array] Must contain valid group ids belonging to the account
39
48
  # @param segments [Array] Must contain valid segment ids belonging to the account. If both groups and segments are provided, only segments are used
40
49
  # @param ab_settings [Array<Array>] only if type is ab - All items of the array are required
@@ -82,6 +91,7 @@ module MailerLite
82
91
  end
83
92
 
84
93
  # Update a new campaign with the specified details.
94
+ # Only `create` accepts `editor` and `template_id`.
85
95
  #
86
96
  # @param campaign_id [Integer] the ID of the campaign to update
87
97
  # @param name [String] Maximum string length of 255 characters
@@ -212,5 +222,27 @@ module MailerLite
212
222
  def languages
213
223
  client.http.get("#{MAILERLITE_API_URL}/campaigns/languages")
214
224
  end
225
+
226
+ # Returns the orders and revenue a campaign earned in one shop
227
+ #
228
+ # @param campaign_id [String] the ID of the campaign
229
+ # @param shop_id [String] the ID of the shop
230
+ # @return [HTTP::Response] the response from the API
231
+ def ecommerce_stats(campaign_id:, shop_id:)
232
+ uri = URI("#{MAILERLITE_API_URL}/campaigns/#{campaign_id}/reports/ecommerce")
233
+ uri.query = URI.encode_www_form('shop_id' => shop_id)
234
+ client.http.get(uri)
235
+ end
236
+
237
+ # Returns the orders attributed to a campaign in one shop
238
+ #
239
+ # @param campaign_id [String] the ID of the campaign
240
+ # @param shop_id [String] the ID of the shop
241
+ # @return [HTTP::Response] the response from the API
242
+ def ecommerce_orders(campaign_id:, shop_id:, page: nil, limit: nil)
243
+ uri = URI("#{MAILERLITE_API_URL}/campaigns/#{campaign_id}/reports/ecommerce/orders")
244
+ uri.query = URI.encode_www_form({ 'shop_id' => shop_id, 'page' => page, 'limit' => limit }.compact)
245
+ client.http.get(uri)
246
+ end
215
247
  end
216
248
  end
@@ -0,0 +1,316 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MailerLite
4
+ # Classes for manipulating e-commerce data from MailerLite API.
5
+ module Ecommerce
6
+ # Shared setup for the e-commerce classes.
7
+ class Resource
8
+ attr_reader :client
9
+
10
+ # Inits the class with the specified `client`.
11
+ #
12
+ # @param client [MailerLite::Client] the `Client` instance to use
13
+ def initialize(client: MailerLite::Client.new)
14
+ @client = client
15
+ end
16
+
17
+ private
18
+
19
+ def shop_url(shop_id, path = '')
20
+ "#{MAILERLITE_API_URL}/ecommerce/shops/#{shop_id}#{path}"
21
+ end
22
+
23
+ def get_with_query(url, filter: {}, **params)
24
+ query = filter.to_h { |key, value| ["filter[#{key}]", value] }.merge(params.transform_keys(&:to_s)).compact
25
+ uri = URI(url)
26
+ uri.query = URI.encode_www_form(query) unless query.empty?
27
+ client.http.get(uri)
28
+ end
29
+ end
30
+
31
+ # Shops from MailerLite API.
32
+ class Shops < Resource
33
+ # @return [HTTP::Response] the response from the API
34
+ def list(page: nil, limit: nil)
35
+ get_with_query("#{MAILERLITE_API_URL}/ecommerce/shops", page: page, limit: limit)
36
+ end
37
+
38
+ # @param shop_id [String] the ID of the shop
39
+ # @return [HTTP::Response] the response from the API
40
+ def get(shop_id)
41
+ client.http.get(shop_url(shop_id))
42
+ end
43
+
44
+ # @param params [Hash] name, url and currency are required
45
+ # @return [HTTP::Response] the response from the API
46
+ def create(params)
47
+ client.http.post("#{MAILERLITE_API_URL}/ecommerce/shops", json: params)
48
+ end
49
+
50
+ # @return [HTTP::Response] the response from the API
51
+ def update(shop_id:, params:)
52
+ client.http.put(shop_url(shop_id), json: params)
53
+ end
54
+
55
+ # @return [HTTP::Response] the response from the API
56
+ def delete(shop_id)
57
+ client.http.delete(shop_url(shop_id))
58
+ end
59
+
60
+ # @param start_date [String] the first day, e.g. 2026-01-01
61
+ # @param end_date [String] the last day, included
62
+ # @return [HTTP::Response] the response from the API
63
+ def stats(shop_id:, start_date:, end_date:)
64
+ get_with_query(shop_url(shop_id, '/reports/stats'), start_date: start_date, end_date: end_date)
65
+ end
66
+ end
67
+
68
+ # Products from MailerLite API.
69
+ class Products < Resource
70
+ # @param filter [Hash] name, type, enabled, category or exclude_from_automations
71
+ # @return [HTTP::Response] the response from the API
72
+ def list(shop_id:, filter: {}, sort: nil, page: nil, limit: nil)
73
+ get_with_query(shop_url(shop_id, '/products'), filter: filter, sort: sort, page: page, limit: limit)
74
+ end
75
+
76
+ # @return [HTTP::Response] the response from the API
77
+ def get(shop_id:, product_id:)
78
+ client.http.get(shop_url(shop_id, "/products/#{product_id}"))
79
+ end
80
+
81
+ # @return [HTTP::Response] the response from the API
82
+ def create(shop_id:, params:)
83
+ client.http.post(shop_url(shop_id, '/products'), json: params)
84
+ end
85
+
86
+ # @return [HTTP::Response] the response from the API
87
+ def update(shop_id:, product_id:, params:)
88
+ client.http.put(shop_url(shop_id, "/products/#{product_id}"), json: params)
89
+ end
90
+
91
+ # @return [HTTP::Response] the response from the API
92
+ def delete(shop_id:, product_id:)
93
+ client.http.delete(shop_url(shop_id, "/products/#{product_id}"))
94
+ end
95
+
96
+ # @param enabled [Boolean] whether the product is enabled
97
+ # @return [HTTP::Response] the response from the API
98
+ def change_status(shop_id:, product_id:, enabled:)
99
+ client.http.put(shop_url(shop_id, "/products/#{product_id}/change-status"), json: { status: enabled })
100
+ end
101
+
102
+ # @param replace [Boolean] also remove the categories that are not listed
103
+ # @return [HTTP::Response] the response from the API
104
+ def assign_categories(shop_id:, product_id:, category_ids:, replace: false)
105
+ client.http.put(shop_url(shop_id, "/products/#{product_id}/categories/multiple"), json: { categories: category_ids, replace: replace })
106
+ end
107
+ end
108
+
109
+ # Product variants from MailerLite API.
110
+ class ProductVariants < Resource
111
+ # @return [HTTP::Response] the response from the API
112
+ def list(shop_id:, product_id:)
113
+ client.http.get(shop_url(shop_id, "/products/#{product_id}/variants"))
114
+ end
115
+
116
+ # @param variants [Array<Hash>] name and attributes are required
117
+ # @return [HTTP::Response] the response from the API
118
+ def sync(shop_id:, product_id:, variants:)
119
+ client.http.post(shop_url(shop_id, "/products/#{product_id}/variants"), json: variants)
120
+ end
121
+
122
+ # @return [HTTP::Response] the response from the API
123
+ def update(shop_id:, product_id:, variant_id:, params:)
124
+ client.http.put(shop_url(shop_id, "/products/#{product_id}/variants/#{variant_id}"), json: params)
125
+ end
126
+
127
+ # @return [HTTP::Response] the response from the API
128
+ def delete(shop_id:, product_id:, variant_id:)
129
+ client.http.delete(shop_url(shop_id, "/products/#{product_id}/variants/#{variant_id}"))
130
+ end
131
+ end
132
+
133
+ # Categories from MailerLite API.
134
+ class Categories < Resource
135
+ # @return [HTTP::Response] the response from the API
136
+ def list(shop_id:, filter: {}, page: nil, limit: nil)
137
+ get_with_query(shop_url(shop_id, '/categories'), filter: filter, page: page, limit: limit)
138
+ end
139
+
140
+ # @return [HTTP::Response] the response from the API
141
+ def get(shop_id:, category_id:)
142
+ client.http.get(shop_url(shop_id, "/categories/#{category_id}"))
143
+ end
144
+
145
+ # @return [HTTP::Response] the response from the API
146
+ def create(shop_id:, params:)
147
+ client.http.post(shop_url(shop_id, '/categories'), json: params)
148
+ end
149
+
150
+ # @return [HTTP::Response] the response from the API
151
+ def update(shop_id:, category_id:, params:)
152
+ client.http.put(shop_url(shop_id, "/categories/#{category_id}"), json: params)
153
+ end
154
+
155
+ # @return [HTTP::Response] the response from the API
156
+ def delete(shop_id:, category_id:)
157
+ client.http.delete(shop_url(shop_id, "/categories/#{category_id}"))
158
+ end
159
+
160
+ # @return [HTTP::Response] the response from the API
161
+ def products(shop_id:, category_id:, page: nil, limit: nil)
162
+ get_with_query(shop_url(shop_id, "/categories/#{category_id}/products"), page: page, limit: limit)
163
+ end
164
+
165
+ # @return [HTTP::Response] the response from the API
166
+ def assign_product(shop_id:, category_id:, product_id:)
167
+ client.http.put(shop_url(shop_id, "/categories/#{category_id}/products/#{product_id}"))
168
+ end
169
+
170
+ # @return [HTTP::Response] the response from the API
171
+ def unassign_product(shop_id:, category_id:, product_id:)
172
+ client.http.delete(shop_url(shop_id, "/categories/#{category_id}/products/#{product_id}"))
173
+ end
174
+ end
175
+
176
+ # Customers from MailerLite API.
177
+ class Customers < Resource
178
+ # @param filter [Hash] email
179
+ # @return [HTTP::Response] the response from the API
180
+ def list(shop_id:, filter: {}, sort: nil, page: nil, limit: nil)
181
+ get_with_query(shop_url(shop_id, '/customers'), filter: filter, sort: sort, page: page, limit: limit)
182
+ end
183
+
184
+ # @return [HTTP::Response] the response from the API
185
+ def get(shop_id:, customer_id:)
186
+ client.http.get(shop_url(shop_id, "/customers/#{customer_id}"))
187
+ end
188
+
189
+ # @return [HTTP::Response] the response from the API
190
+ def create(shop_id:, params:)
191
+ client.http.post(shop_url(shop_id, '/customers'), json: params)
192
+ end
193
+
194
+ # @return [HTTP::Response] the response from the API
195
+ def update(shop_id:, customer_id:, params:)
196
+ client.http.put(shop_url(shop_id, "/customers/#{customer_id}"), json: params)
197
+ end
198
+
199
+ # @return [HTTP::Response] the response from the API
200
+ def delete(shop_id:, customer_id:)
201
+ client.http.delete(shop_url(shop_id, "/customers/#{customer_id}"))
202
+ end
203
+ end
204
+
205
+ # Orders from MailerLite API.
206
+ class Orders < Resource
207
+ # @param filter [Hash] status, customer, attributed or email_id
208
+ # @return [HTTP::Response] the response from the API
209
+ def list(shop_id:, filter: {}, sort: nil, page: nil, limit: nil)
210
+ get_with_query(shop_url(shop_id, '/orders'), filter: filter, sort: sort, page: page, limit: limit)
211
+ end
212
+
213
+ # @return [HTTP::Response] the response from the API
214
+ def get(shop_id:, order_id:)
215
+ client.http.get(shop_url(shop_id, "/orders/#{order_id}"))
216
+ end
217
+
218
+ # @param params [Hash] customer and cart are required
219
+ # @return [HTTP::Response] the response from the API
220
+ def create(shop_id:, params:)
221
+ client.http.post(shop_url(shop_id, '/orders'), json: params)
222
+ end
223
+
224
+ # @return [HTTP::Response] the response from the API
225
+ def update(shop_id:, order_id:, params:)
226
+ client.http.put(shop_url(shop_id, "/orders/#{order_id}"), json: params)
227
+ end
228
+
229
+ # @return [HTTP::Response] the response from the API
230
+ def delete(shop_id:, order_id:)
231
+ client.http.delete(shop_url(shop_id, "/orders/#{order_id}"))
232
+ end
233
+ end
234
+
235
+ # Carts from MailerLite API.
236
+ class Carts < Resource
237
+ # @return [HTTP::Response] the response from the API
238
+ def list(shop_id:, page: nil, limit: nil)
239
+ get_with_query(shop_url(shop_id, '/carts'), page: page, limit: limit)
240
+ end
241
+
242
+ # @return [HTTP::Response] the response from the API
243
+ def get(shop_id:, cart_id:)
244
+ client.http.get(shop_url(shop_id, "/carts/#{cart_id}"))
245
+ end
246
+
247
+ # @return [HTTP::Response] the response from the API
248
+ def update(shop_id:, cart_id:, params:)
249
+ client.http.put(shop_url(shop_id, "/carts/#{cart_id}"), json: params)
250
+ end
251
+ end
252
+
253
+ # Cart items from MailerLite API.
254
+ class CartItems < Resource
255
+ # @return [HTTP::Response] the response from the API
256
+ def list(shop_id:, cart_id:, page: nil, limit: nil)
257
+ get_with_query(shop_url(shop_id, "/carts/#{cart_id}/items"), page: page, limit: limit)
258
+ end
259
+
260
+ # @return [HTTP::Response] the response from the API
261
+ def get(shop_id:, cart_id:, item_id:)
262
+ client.http.get(shop_url(shop_id, "/carts/#{cart_id}/items/#{item_id}"))
263
+ end
264
+
265
+ # @return [HTTP::Response] the response from the API
266
+ def create(shop_id:, cart_id:, params:)
267
+ client.http.post(shop_url(shop_id, "/carts/#{cart_id}/items"), json: params)
268
+ end
269
+
270
+ # @return [HTTP::Response] the response from the API
271
+ def update(shop_id:, cart_id:, item_id:, params:)
272
+ client.http.put(shop_url(shop_id, "/carts/#{cart_id}/items/#{item_id}"), json: params)
273
+ end
274
+
275
+ # @return [HTTP::Response] the response from the API
276
+ def delete(shop_id:, cart_id:, item_id:)
277
+ client.http.delete(shop_url(shop_id, "/carts/#{cart_id}/items/#{item_id}"))
278
+ end
279
+ end
280
+
281
+ # Bulk imports from MailerLite API.
282
+ class Imports < Resource
283
+ # @param products [Array<Hash>] the products to import
284
+ # @return [HTTP::Response] the response from the API
285
+ def products(shop_id:, products:)
286
+ client.http.post(shop_url(shop_id, '/products/import'), json: products)
287
+ end
288
+
289
+ # @param categories [Array<Hash>] the categories to import
290
+ # @return [HTTP::Response] the response from the API
291
+ def categories(shop_id:, categories:)
292
+ client.http.post(shop_url(shop_id, '/categories/import'), json: categories)
293
+ end
294
+
295
+ # @param customers [Array<Hash>] the customers to import
296
+ # @return [HTTP::Response] the response from the API
297
+ def customers(shop_id:, customers:)
298
+ client.http.post(shop_url(shop_id, '/customers/import'), json: customers)
299
+ end
300
+
301
+ # @param orders [Array<Hash>] the orders to import
302
+ # @return [HTTP::Response] the response from the API
303
+ def orders(shop_id:, orders:)
304
+ client.http.post(shop_url(shop_id, '/orders/import'), json: { orders: orders })
305
+ end
306
+ end
307
+
308
+ # E-commerce tracking settings from MailerLite API.
309
+ class Settings < Resource
310
+ # @return [HTTP::Response] the response from the API
311
+ def get
312
+ client.http.get("#{MAILERLITE_API_URL}/ecommerce/settings")
313
+ end
314
+ end
315
+ end
316
+ end