mailchimp-rest-api 0.2.0 → 0.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cd8e4ac41f0ff072d449fab1c0466bed09d533def2a97db463a0dd76e47aaef2
4
- data.tar.gz: 24fccb0d0eb4175d77055bdd5ce4e935789cbd5b6ed700d17f7d98ab86ada034
3
+ metadata.gz: 2154d72e46deb2e45329e343e31978620eaec64932172d32a2b2e5bf69ae0869
4
+ data.tar.gz: 50479343dea5a9306058079839f9ac82b001a95933608736eefcd4f078cf45db
5
5
  SHA512:
6
- metadata.gz: 8cc1fa18308afd486cb4418317f8809d9e2758df9a1ea4087aaea57ba66db3570ae91fc99faccd8472a4663f22800151519b5c4c350f750f275a484c147544fe
7
- data.tar.gz: 0df6d93f8742d35b73372a5b7ce139723102261f046243e77bcb30f90937fa519a93e0c1fd4e267bc67bb5a38c9932907d5a2373a0f27ac49f81d763bd4465f5
6
+ metadata.gz: a212a5f4d5794f27bc00a47eaa2d24e5f9d1d982621ea3951f32442693c79ebb81dfebb34fda96936d77c1b4421c80752a502eed25ea202f99d619d06d61c478
7
+ data.tar.gz: 683cf77583595b8a7992b92d0d1fa1151a22c484ed16f95c89c49a5832fc4a619b60447fc2c303fae2be7ff7abe279d3673d8c0d7416514f41e7d325cb3fa3ca
data/README.md CHANGED
@@ -1,99 +1,140 @@
1
1
  # Mailchimp REST API (Marketing)
2
2
 
3
+ A Ruby gem for interacting with the Mailchimp Marketing API. This gem provides a clean, intuitive interface for working
4
+ with Mailchimp's REST API, featuring automatic retries, pagination support, and comprehensive error handling.
5
+
6
+ ## Features
7
+
8
+ - Supports Ruby versions: 2.6 through 3.3, head, jruby-9.4, truffleruby-24
9
+ - Zero dependencies
10
+ - Configurable auto-retries
11
+ - Built-in pagination support
12
+ - Comprehensive error handling
13
+ - Clean, intuitive API interface
14
+
3
15
  ## Installation
4
16
 
5
- ```bash
6
- bundle add mailchimp-rest-api
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```console
20
+ gem 'mailchimp-rest-api'
7
21
  ```
8
22
 
9
- ## Features
23
+ And then execute:
24
+
25
+ ```console
26
+ bundle install
27
+ ```
28
+
29
+ ## Quick Start
30
+
31
+ ```ruby
32
+ # Configure the client
33
+ MailchimpAPI.client = MailchimpAPI::Client.new(
34
+ api_key: ENV['MAILCHIMP_API_KEY']
35
+ )
36
+
37
+ # Example: Add a member to an audience
38
+ response = MailchimpAPI.audience_members.create(
39
+ 'audience_id',
40
+ body: {
41
+ email_address: 'user@example.com',
42
+ status: 'subscribed',
43
+ merge_fields: {
44
+ FNAME: 'John',
45
+ LNAME: 'Doe'
46
+ }
47
+ }
48
+ )
49
+
50
+ puts response.body[:status]
51
+ ```
52
+
53
+ ## Configuration
10
54
 
11
- - Supported Ruby Versions - *(2.6 .. 3.3), head, jruby-9.4, truffleruby-24*
12
- - No dependencies;
13
- - Auto-retries (configured);
14
- - Pagination methods
55
+ ### Client Configuration
15
56
 
16
- ## Usage
57
+ The client accepts several configuration options:
17
58
 
18
59
  ```ruby
19
- # Setup client
20
- MailchimpAPI.client = MailchimpAPI::Client.new(api_key: ENV['MAILCHIMP_API_KEY'])
21
-
22
- # Call any HTTP API
23
- MailchimpAPI.post(path, query: query, body: body, headers: headers)
24
- MailchimpAPI.get(path, query: query, body: body, headers: headers)
25
- MailchimpAPI.patch(path, query: query, body: body, headers: headers)
26
- MailchimpAPI.put(path, query: query, body: body, headers: headers)
27
- MailchimpAPI.delete(path, query: query, body: body, headers: headers)
28
-
29
- # Call any defined REST API (defined in lib/resources)
30
- MailchimpAPI::Audience::Members.show(audience_id, email, query: query)
31
- MailchimpAPI::Audience::Members.create(audience_id, body: body)
32
-
33
- # Or call it via shortcuts (defined in lib/client/api_methods.rb)
34
- MailchimpAPI.audience_members.show(audience_id, email, query: query)
35
- MailchimpAPI.audience_members.create(audience_id, body: body)
60
+ client = MailchimpAPI::Client.new(
61
+ api_key: ENV['MAILCHIMP_API_KEY'],
62
+ retries: {
63
+ enabled: true,
64
+ count: 4,
65
+ sleep: [0, 0.25, 0.75, 1.5] # Retry delays in seconds
66
+ },
67
+ http_opts: {
68
+ read_timeout: 30,
69
+ write_timeout: 30,
70
+ open_timeout: 30
71
+ }
72
+ )
36
73
  ```
37
74
 
38
- ### Response
75
+ ### Retry Configuration
76
+
77
+ By default, the client will retry failed requests with the following configuration:
39
78
 
40
- `Response` object is returned after each `API` request.
79
+ - Enabled: true
80
+ - Retry count: 4
81
+ - Delay between retries: 0, 0.25, 0.75, 1.5 seconds
41
82
 
42
- #### Original HTTP response data
83
+ Retries are triggered for:
43
84
 
44
- - `response.http_status` - response HTTP status as Integer
45
- - `response.http_body` - response body as String
46
- - `response.http_headers` - response headers as Hash with String keys
47
- - `response.http_response` - original Net::HTTP::Response object
48
- - `response.request` - Request object that was used to get this response
85
+ - Network errors
86
+ - HTTP status codes: 409, 429, and 5xx responses
49
87
 
50
- #### Parsed JSON body methods
88
+ ### HTTP Options
51
89
 
52
- - `response.body` - parsed JSON body, keys are Symbols
53
- - `response[:field]` - gets `:field` attribute from parsed body,
54
- returns nil if response have no such key
55
- - `response.fetch(:field)` - gets `:field` attribute from parsed body,
56
- raises KeyError if response has no such key
90
+ You can configure various HTTP options that are passed to `Net::HTTP.start`. Common options include:
57
91
 
58
- #### Error check methods
92
+ - `read_timeout`
93
+ - `write_timeout`
94
+ - `open_timeout`
59
95
 
60
- - `response.success?` - checks HTTP status code is 2xx
61
- - `response.failed?` - checks HTTP status code is not 2xx
96
+ ## API Usage
62
97
 
63
- ## Errors
98
+ ### Making Requests
64
99
 
65
- All error classes inherit from `MailchimpAPI::Error` and provide:
100
+ You can make HTTP requests directly:
66
101
 
67
- - `error_type` - Error type returned by Mailchimp
68
- - `error_title` - Error title or response error class name
69
- - `error_status` - Error status or HTTP status
70
- - `error_detail` - Error description from Mailchimp
71
- - `error_fields` - Field-specific errors
72
- - `error_instance` - Error ID from Mailchimp
73
- - `response` - Response object
74
- - `request` - Request object
102
+ ```ruby
103
+ # GET request
104
+ response = MailchimpAPI.get('/lists/<list_id>/members')
105
+
106
+ # POST request
107
+ response = MailchimpAPI.post(
108
+ '/lists/<list_id>/members',
109
+ body: { email_address: 'user@example.com' }
110
+ )
111
+ ```
75
112
 
76
- MailchimpAPI provides specific error classes for different types of errors:
113
+ ### Working with Responses
114
+
115
+ The response object provides several useful methods:
116
+
117
+ ```ruby
118
+ response = MailchimpAPI.get('/some/path')
77
119
 
78
- ### Network Errors
120
+ # Check response status
121
+ response.success? # true for 2xx status codes
122
+ response.failed? # true for non-2xx status codes
79
123
 
80
- - `MailchimpAPI::Errors::NetworkError` - Raised when a network error occurs
81
- during request execution
124
+ # Access response data
125
+ response.body # Parsed JSON body (symbolized keys)
126
+ response[:field] # Access specific field (returns nil if missing)
127
+ response.fetch(:field) # Access field (raises KeyError if missing)
82
128
 
83
- ### HTTP Status Code Errors
129
+ # Access HTTP details
130
+ response.http_status
131
+ response.http_body
132
+ response.http_headers
133
+ ```
84
134
 
85
- - `MailchimpAPI::Errors::BadRequest` (400) - Invalid request parameters
86
- - `MailchimpAPI::Errors::Unauthorized` (401) - Invalid API key
87
- - `MailchimpAPI::Errors::Forbidden` (403) - Insufficient permissions
88
- - `MailchimpAPI::Errors::NotFound` (404) - Resource not found
89
- - `MailchimpAPI::Errors::MethodNotAllowed` (405) - HTTP method not allowed
90
- - `MailchimpAPI::Errors::RequestURITooLong` (414) - Request URI too long
91
- - `MailchimpAPI::Errors::UnprocessableEntity` (422) - Request validation failed
92
- - `MailchimpAPI::Errors::UpgradeRequired` (426) - API version upgrade required
93
- - `MailchimpAPI::Errors::TooManyRequests` (429) - Rate limit exceeded
94
- - `MailchimpAPI::Errors::ServerError` (5xx) - Mailchimp server error
135
+ ### Error Handling
95
136
 
96
- Example:
137
+ The gem provides specific error classes for different types of errors:
97
138
 
98
139
  ```ruby
99
140
  begin
@@ -106,129 +147,130 @@ rescue MailchimpAPI::Errors::NetworkError => e
106
147
  end
107
148
  ```
108
149
 
109
- ## Configuration options
110
-
111
- MailchimpAPI client accepts this additional options:
112
-
113
- - `:retries`
114
- - `:http_opts`
115
-
116
- ### Option `:retries`
150
+ Available error classes:
117
151
 
118
- This is a Hash with retries configuration.
119
- By default retries are enabled, 4 retries with 0, 0.25, 0.75, 1.5 seconds delay.
120
- Default config: `{enabled: true, count: 4, sleep: [0, 0.25, 0.75, 1.5]}`.
121
- New options are merged with defaults.
122
- Please keep `sleep` array same size as `count`.
152
+ - `NetworkError` - Network-related issues
153
+ - `BadRequest` (400) - Invalid request parameters
154
+ - `Unauthorized` (401) - Invalid API key
155
+ - `Forbidden` (403) - Insufficient permissions
156
+ - `NotFound` (404) - Resource not found
157
+ - `UnprocessableEntity` (422) - Request validation failed
158
+ - `TooManyRequests` (429) - Rate limit exceeded
159
+ - `ServerError` (5xx) - Mailchimp server error
123
160
 
124
- Retries happen on any network error, on 409, 429, 5xx response status code.
125
-
126
- ```ruby
127
- client = MailchimpAPI::Client.new(
128
- retries: {enabled: !Rails.env.test?, count: 5, sleep: [0, 0.25, 0.75, 1.5, 2]}
129
- # ...
130
- )
131
- ```
161
+ ## Resources
132
162
 
133
- ### Option `:http_opts`
163
+ Most resources support this APIs:
134
164
 
135
- This are the options that are provided to the `Net::HTTP.start` method,
136
- like `:read_timeout`, `:write_timeout`, etc.
165
+ - `list` - List items
166
+ - `create` - Create new item
167
+ - `show` - Get item details
168
+ - `update` - Update item
169
+ - `delete` - Delete item
170
+ - `each` - Iterate through items
137
171
 
138
- You can find full list of available options here <https://docs.ruby-lang.org/en/master/Net/HTTP.html#method-c-start>
139
- (Please choose you version of ruby).
172
+ ### MailchimpAPI.audience_members
140
173
 
141
- By default it is an empty hash.
174
+ - `list(list_id)` - List members
175
+ - `create(list_id, body: { email_address: 'x', status: 'x' })` - Add member
176
+ - `show(list_id, email)` - Get member
177
+ - `update(list_id, email, body: { status: 'x' })` - Update member
178
+ - `archive(list_id, email)` - Archive member
179
+ - `add_or_update(list_id, email, body: { status: 'x' })` - Upsert member
180
+ - `delete_permanent(list_id, email)` - Permanently delete
181
+ - `each(list_id)` - Iterate members
142
182
 
143
- ```ruby
144
- client = MailchimpAPI::Client.new(
145
- http_opts: {read_timeout: 30, write_timeout: 30, open_timeout: 30}
146
- # ...
147
- )
148
- ```
183
+ ### MailchimpAPI.audience_member_tags
149
184
 
150
- ## Pagination
185
+ - `list(list_id, email)` - List tags
186
+ - `create(list_id, email, body: { tags: [{name: 'x'}] })` - Add tags
187
+ - `each(list_id, email)` - Iterate tags
151
188
 
152
- We have two specific methods:
189
+ ### MailchimpAPI.audience_segments
153
190
 
154
- - `MailchimpAPI#each_page(response)` - iterates over current and each next page
155
- response
156
- - `MailchimpAPI#each_page_item(response, items: :members)` - iterates over
157
- each item (each member in this case)
191
+ - `list(list_id)` - List segments
192
+ - `create(list_id, body: { name: 'x' })` - Create segment
193
+ - `show(list_id, segment_id)` - Get segment
194
+ - `update(list_id, segment_id, body: { name: 'x' })` - Update segment
195
+ - `delete(list_id, segment_id)` - Delete segment
196
+ - `batch_add_or_remove_members(list_id, segment_id, body: { members_to_add: [] })` - Bulk modify
197
+ - `each(list_id)` - Iterate segments
158
198
 
159
- Example:
199
+ ### MailchimpAPI.audience_segment_members
160
200
 
161
- ```ruby
162
- first_page = MailchimpAPI.audience_members.list(list_id, query: { count: 100 }
201
+ - `list(list_id, segment_id)` - List segment members
202
+ - `create(list_id, segment_id, body: { email_address: 'x' })` - Add segment member
203
+ - `delete(list_id, segment_id, email)` - Remove segment member
204
+ - `each(list_id, segment_id)` - Iterate segment members
163
205
 
164
- MailchimpAPI.each_page(first_page) do |response|
165
- puts response.body
166
- end
206
+ ### MailchimpAPI.audience_interest_categories
167
207
 
168
- MailchimpAPI.each_page_item(first_page, items: :members) do |item|
169
- puts item[:email_address]
170
- end
171
- ```
208
+ - `list(list_id)` - List categories
209
+ - `create(list_id, body: { title: 'x', type: 'x' })` - Create category
210
+ - `show(list_id, category_id)` - Get category
211
+ - `update(list_id, category_id, body: { title: 'x' })` - Update category
212
+ - `delete(list_id, category_id)` - Delete category
213
+ - `each(list_id)` - Iterate categories
172
214
 
173
- ## Request
215
+ ### MailchimpAPI.audience_interests
174
216
 
175
- ...
217
+ - `list(list_id, category_id)` - List interests
218
+ - `create(list_id, category_id, body: { name: 'x' })` - Create interest
219
+ - `show(list_id, category_id, interest_id)` - Get interest
220
+ - `update(list_id, category_id, interest_id, body: { name: 'x' })` - Update interest
221
+ - `delete(list_id, category_id, interest_id)` - Delete interest
222
+ - `each(list_id, category_id)` - Iterate interests
176
223
 
177
- ## APIs
224
+ ### MailchimpAPI.audience_webhooks
178
225
 
179
- ### Audience Members
226
+ - `list(list_id)` - List webhooks
227
+ - `create(list_id, body: { url: 'x', events: {} })` - Create webhook
228
+ - `show(list_id, webhook_id)` - Get webhook
229
+ - `update(list_id, webhook_id, body: { events: {} })` - Update webhook
230
+ - `delete(list_id, webhook_id)` - Delete webhook
231
+ - `each(list_id)` - Iterate webhooks
180
232
 
181
- - List `MailchimpAPI.audience_members.get(audience_id)`
182
- - Create `MailchimpAPI.audience_members.create(audience_id, body: body)`
183
- - Show `MailchimpAPI.audience_members.show(audience_id, email)`
184
- - Update `MailchimpAPI.audience_members.update(audience_id, email, body: body)`
185
- - Add or Update `MailchimpAPI.audience_members.add_or_update(audience_id, email, body: body)`
186
- - Archive `MailchimpAPI.audience_members.delete(audience_id, email)`
187
- - Delete Permanently `MailchimpAPI.audience_members.delete(audience_id, email)`
233
+ ### Pagination
188
234
 
189
- ### Audience Member Tags
235
+ Most resources support pagination through the `each` method:
190
236
 
191
- - List `MailchimpAPI.audience_member_tags.get(audience_id, email)`
192
- - Create `MailchimpAPI.audience_member_tags.create(audience_id, email, body: body)`
237
+ ```ruby
238
+ # Iterate through all members
239
+ MailchimpAPI.audience_members.each(audience_id) do |member|
240
+ puts member['email_address']
241
+ end
193
242
 
194
- ### Audience Webhooks
243
+ # Configure page size
244
+ MailchimpAPI.audience_members.each(audience_id, query: { count: 100 }) do |member|
245
+ puts member['email_address']
246
+ end
247
+ ```
195
248
 
196
- - List `MailchimpAPI.audience_webhooks.get(audience_id)`
197
- - Create `MailchimpAPI.audience_webhooks.create(audience_id, body: body)`
198
- - Show `MailchimpAPI.audience_webhooks.show(audience_id, webhook_id)`
199
- - Update `MailchimpAPI.audience_webhooks.update(audience_id, webhook_id, body: body)`
200
- - Delete `MailchimpAPI.audience_webhooks.delete(audience_id, webhook_id)`
249
+ ## Development
201
250
 
202
- ### Audience Interest Categories
251
+ After checking out the repo, run:
203
252
 
204
- - List `MailchimpAPI.audience_interest_categories.get(audience_id)`
205
- - Create `MailchimpAPI.audience_interest_categories.create(audience_id, body: body)`
206
- - Show `MailchimpAPI.audience_interest_categories.show(audience_id, webhook_id)`
207
- - Update `MailchimpAPI.audience_interest_categories.update(audience_id, webhook_id, body: body)`
208
- - Delete `MailchimpAPI.audience_interest_categories.delete(audience_id, webhook_id)`
253
+ ```bash
254
+ bundle install
255
+ ```
209
256
 
210
- ### Audience Interests
257
+ To run tests:
211
258
 
212
- - List `MailchimpAPI.audience_interests.get(audience_id)`
213
- - Create `MailchimpAPI.audience_interests.create(audience_id, body: body)`
214
- - Show `MailchimpAPI.audience_interests.show(audience_id, webhook_id)`
215
- - Update `MailchimpAPI.audience_interests.update(audience_id, webhook_id, body: body)`
216
- - Delete `MailchimpAPI.audience_interests.delete(audience_id, webhook_id)`
259
+ ```bash
260
+ bundle exec rspec
261
+ ```
217
262
 
218
- ## Development
263
+ To run linters:
219
264
 
220
265
  ```bash
221
- rubocop
222
- rspec
223
- mdl README.md CHANGELOG.md RELEASE.md
266
+ bundle exec rubocop
267
+ bundle exec mdl README.md CHANGELOG.md RELEASE.md
224
268
  ```
225
269
 
226
270
  ## Contributing
227
271
 
228
- Bug reports and pull requests are welcome on GitHub at <https://github.com/aglushkov/mailchimp-rest-api>.
272
+ Bug reports and pull requests are welcome on GitHub at [https://github.com/andreyruby/mailchimp-rest-api](https://github.com/andreyruby/mailchimp-rest-api).
229
273
 
230
274
  ## License
231
275
 
232
276
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
233
-
234
- [pagination]: #pagination
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.4.0
@@ -1,18 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MailchimpAPI
4
+ # Internal class for constructing batch operation requests
5
+ # @api private
4
6
  class BatchRequest
7
+ # API version prefix to be removed from paths
5
8
  API_VERSION_PREFIX = "/#{MailchimpAPI::API_VERSION}"
6
9
 
7
- attr_reader :request, :operation_id
10
+ # @return [MailchimpAPI::Request] The original request object
11
+ attr_reader :request
8
12
 
13
+ # @return [String, nil] Optional operation ID for tracking batch operations
14
+ attr_reader :operation_id
15
+
16
+ # Creates a new batch request
17
+ # @param request [MailchimpAPI::Request] The original request to be batched
18
+ # @param operation_id [String, nil] Optional operation ID for tracking
9
19
  def initialize(request, operation_id: nil)
10
20
  @request = request
11
21
  @operation_id = operation_id
12
22
  end
13
23
 
14
- # Make a batch operation request
15
- # https://mailchimp.com/developer/marketing/guides/run-async-requests-batch-endpoint/#make-a-batch-operations-request
24
+ # Converts the request into a batch operation format
25
+ # @return [Hash] The operation in format suitable for `/batch` request
26
+ # @example
27
+ # {
28
+ # method: "GET",
29
+ # path: "/lists",
30
+ # params: { count: 10 },
31
+ # operation_id: "get_lists"
32
+ # }
16
33
  def operation
17
34
  operation = {
18
35
  method: request.method, # "GET", "POST", "PUT", "PATCH", "DELETE"
@@ -2,26 +2,60 @@
2
2
 
3
3
  module MailchimpAPI
4
4
  class Client
5
- #
6
- # Methods to access API resources
7
- #
5
+ # Methods to access Mailchimp API resources
8
6
  module APIMethods
7
+ # Returns a new instance of Audience::InterestCategories
8
+ # @return [Audience::InterestCategories] Interest categories resource
9
+ # @example
10
+ # client.audience_interest_categories # => #<MailchimpAPI::Audience::InterestCategories>
9
11
  def audience_interest_categories
10
12
  Audience::InterestCategories.new(self)
11
13
  end
12
14
 
15
+ # Returns a new instance of Audience::Interests
16
+ # @return [Audience::Interests] Interests resource
17
+ # @example
18
+ # client.audience_interests # => #<MailchimpAPI::Audience::Interests>
13
19
  def audience_interests
14
20
  Audience::Interests.new(self)
15
21
  end
16
22
 
23
+ # Returns a new instance of Audience::MemberTags
24
+ # @return [Audience::MemberTags] Member tags resource
25
+ # @example
26
+ # client.audience_member_tags # => #<MailchimpAPI::Audience::MemberTags>
17
27
  def audience_member_tags
18
28
  Audience::MemberTags.new(self)
19
29
  end
20
30
 
31
+ # Returns a new instance of Audience::Members
32
+ # @return [Audience::Members] Members resource
33
+ # @example
34
+ # client.audience_members # => #<MailchimpAPI::Audience::Members>
21
35
  def audience_members
22
36
  Audience::Members.new(self)
23
37
  end
24
38
 
39
+ # Returns a new instance of Audience::Segments
40
+ # @return [Audience::Segments] Segments resource
41
+ # @example
42
+ # client.audience_segments # => #<MailchimpAPI::Audience::Segments>
43
+ def audience_segments
44
+ Audience::Segments.new(self)
45
+ end
46
+
47
+ # Returns a new instance of Audience::SegmentMembers
48
+ # @return [Audience::SegmentMembers] Segment members resource
49
+ # @example
50
+ # client.audience_segment_members # => #<MailchimpAPI::Audience::SegmentMembers>
51
+ def audience_segment_members
52
+ Audience::SegmentMembers.new(self)
53
+ end
54
+
55
+ # Returns a new instance of Audience::Webhooks
56
+ # @return [Audience::Webhooks] Webhooks resource
57
+ # @example
58
+ # client.audience_webhooks # => #<MailchimpAPI::Audience::Webhooks>
25
59
  def audience_webhooks
26
60
  Audience::Webhooks.new(self)
27
61
  end
@@ -2,30 +2,83 @@
2
2
 
3
3
  module MailchimpAPI
4
4
  class Client
5
- #
6
5
  # Methods to create and run batch requests
7
- #
8
6
  module BatchMethods
7
+ # Creates a new batch GET request
8
+ # @param path [String] API endpoint path
9
+ # @param query [Hash] Optional query parameters
10
+ # @param body [Hash] Optional request body
11
+ # @param headers [Hash] Optional request headers
12
+ # @param operation_id [String] Optional operation ID
13
+ # @return [BatchRequest] New batch request
14
+ # @example
15
+ # request = client.batch_get_request("/lists", operation_id: "get_lists")
9
16
  def batch_get_request(path, query: nil, body: nil, headers: nil, operation_id: nil)
10
17
  new_batch_request(Net::HTTP::Get, path, query: query, body: body, headers: headers, operation_id: operation_id)
11
18
  end
12
19
 
20
+ # Creates a new batch POST request
21
+ # @param path [String] API endpoint path
22
+ # @param query [Hash] Optional query parameters
23
+ # @param body [Hash] Optional request body
24
+ # @param headers [Hash] Optional request headers
25
+ # @param operation_id [String] Optional operation ID
26
+ # @return [BatchRequest] New batch request
27
+ # @example
28
+ # request = client.batch_post_request("/lists", body: { name: "New List" }, operation_id: "create_list")
13
29
  def batch_post_request(path, query: nil, body: nil, headers: nil, operation_id: nil)
14
30
  new_batch_request(Net::HTTP::Post, path, query: query, body: body, headers: headers, operation_id: operation_id)
15
31
  end
16
32
 
33
+ # Creates a new batch PUT request
34
+ # @param path [String] API endpoint path
35
+ # @param query [Hash] Optional query parameters
36
+ # @param body [Hash] Optional request body
37
+ # @param headers [Hash] Optional request headers
38
+ # @param operation_id [String] Optional operation ID
39
+ # @return [BatchRequest] New batch request
40
+ # @example
41
+ # request = client.batch_put_request("/lists/123", body: { name: "Updated List" }, operation_id: "update_list")
17
42
  def batch_put_request(path, query: nil, body: nil, headers: nil, operation_id: nil)
18
43
  new_batch_request(Net::HTTP::Put, path, query: query, body: body, headers: headers, operation_id: operation_id)
19
44
  end
20
45
 
46
+ # Creates a new batch PATCH request
47
+ # @param path [String] API endpoint path
48
+ # @param query [Hash] Optional query parameters
49
+ # @param body [Hash] Optional request body
50
+ # @param headers [Hash] Optional request headers
51
+ # @param operation_id [String] Optional operation ID
52
+ # @return [BatchRequest] New batch request
53
+ # @example
54
+ # request = client.batch_patch_request("/lists/123", body: { name: "Updated List" }, operation_id: "update_list")
21
55
  def batch_patch_request(path, query: nil, body: nil, headers: nil, operation_id: nil)
22
56
  new_batch_request(Net::HTTP::Patch, path, query: query, body: body, headers: headers, operation_id: operation_id)
23
57
  end
24
58
 
59
+ # Creates a new batch DELETE request
60
+ # @param path [String] API endpoint path
61
+ # @param query [Hash] Optional query parameters
62
+ # @param body [Hash] Optional request body
63
+ # @param headers [Hash] Optional request headers
64
+ # @param operation_id [String] Optional operation ID
65
+ # @return [BatchRequest] New batch request
66
+ # @example
67
+ # request = client.batch_delete_request("/lists/123", operation_id: "delete_list")
25
68
  def batch_delete_request(path, query: nil, body: nil, headers: nil, operation_id: nil)
26
69
  new_batch_request(Net::HTTP::Delete, path, query: query, body: body, headers: headers, operation_id: operation_id)
27
70
  end
28
71
 
72
+ # Executes a batch of requests
73
+ # @param batch_requests [Array<BatchRequest>] Array of batch requests to execute
74
+ # @param query [Hash] Optional query parameters
75
+ # @return [Response] API response
76
+ # @example
77
+ # requests = [
78
+ # client.batch_get_request("/lists", operation_id: "get_lists"),
79
+ # client.batch_post_request("/lists", body: { name: "New List" }, operation_id: "create_list")
80
+ # ]
81
+ # response = client.batch(requests)
29
82
  def batch(batch_requests, query: nil)
30
83
  operations = batch_requests.map(&:operation)
31
84
  post("batches", query: query, body: {operations: operations})
@@ -33,12 +86,18 @@ module MailchimpAPI
33
86
 
34
87
  private
35
88
 
36
- # rubocop:disable Metrics/ParameterLists
89
+ # Creates a new batch request
90
+ # @param http_method [Net::HTTP::Request] HTTP method class
91
+ # @param path [String] API endpoint path
92
+ # @param query [Hash] Optional query parameters
93
+ # @param body [Hash] Optional request body
94
+ # @param headers [Hash] Optional request headers
95
+ # @param operation_id [String] Optional operation ID
96
+ # @return [BatchRequest] New batch request
37
97
  def new_batch_request(http_method, path, query: nil, body: nil, headers: nil, operation_id: nil)
38
98
  request = new_request(http_method, path: path, query: query, body: body, headers: headers)
39
99
  BatchRequest.new(request, operation_id: operation_id)
40
100
  end
41
- # rubocop:enable Metrics/ParameterLists
42
101
  end
43
102
  end
44
103
  end