mailchimp-rest-api 0.3.0 → 0.4.1

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: 428009c6c680a7c4bc6ec3b80fe5e4c1485df70e358928655fefabbf0b76637c
4
- data.tar.gz: bd8307d56a558d64a4d7a7551a21cacf9dfe1e44036882191421c359c1b2682a
3
+ metadata.gz: 6bb470c7bf3ad957b57ec7bd7c19299e5594de1de05fc4a24b683da82ee3c998
4
+ data.tar.gz: d7951ce1b9194282a03b9cd1743b513c11003c3c50f9edce9e839ee38370d795
5
5
  SHA512:
6
- metadata.gz: c3f2cc4b7561902a8ba1991b8acb68be65714a506854adea22cd3d0f1a7fa449aae6cea82267cb55d4b45a9ea7f11f6c4ba41b5e602d8a75ecb6f6f4df8a3102
7
- data.tar.gz: f4bb0e6175805b8363ed59df417c4f103ee6b70c5bc40dd6003e7f8ac7eaef3ac5ac992a251b055d224981ab6142f9c460e2a0924ce7691bfbf0cc560e922f7f
6
+ metadata.gz: 3a59055a5853bd0d54741202c67029f6bbd4bc6f940a65f0fba3150fa36f747d54658aad02da3cd64b9840f51df88e06177af54f44aaaea7af057f877df0c935
7
+ data.tar.gz: e9b0b7630353f19c94ea8edaaad563288f4adb48c497ff05f2d40779c771d1a341d824eb2bdae1a6ca64dd544a1be0077af5bfbbc5ab9a6c6c658361ce69643c
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:
10
24
 
11
- - Supported Ruby Versions - *(2.6 .. 3.3), head, jruby-9.4, truffleruby-24*
12
- - No dependencies;
13
- - Auto-retries (configured);
14
- - Pagination methods
25
+ ```console
26
+ bundle install
27
+ ```
15
28
 
16
- ## Usage
29
+ ## Quick Start
17
30
 
18
31
  ```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)
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]
36
51
  ```
37
52
 
38
- ### Response
53
+ ## Configuration
39
54
 
40
- `Response` object is returned after each `API` request.
55
+ ### Client Configuration
41
56
 
42
- #### Original HTTP response data
57
+ The client accepts several configuration options:
43
58
 
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
59
+ ```ruby
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
+ )
73
+ ```
74
+
75
+ ### Retry Configuration
76
+
77
+ By default, the client will retry failed requests with the following configuration:
78
+
79
+ - Enabled: true
80
+ - Retry count: 4
81
+ - Delay between retries: 0, 0.25, 0.75, 1.5 seconds
82
+
83
+ Retries are triggered for:
84
+
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
99
+
100
+ You can make HTTP requests directly:
101
+
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
+ ```
64
112
 
65
- All error classes inherit from `MailchimpAPI::Error` and provide:
113
+ ### Working with Responses
66
114
 
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
115
+ The response object provides several useful methods:
75
116
 
76
- MailchimpAPI provides specific error classes for different types of errors:
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,123 +147,130 @@ rescue MailchimpAPI::Errors::NetworkError => e
106
147
  end
107
148
  ```
108
149
 
109
- ## Configuration options
150
+ Available error classes:
110
151
 
111
- MailchimpAPI client accepts this additional options:
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
112
160
 
113
- - `:retries`
114
- - `:http_opts`
161
+ ## Resources
115
162
 
116
- ### Option `:retries`
163
+ Most resources support this APIs:
117
164
 
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`.
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
123
171
 
124
- Retries happen on any network error, on 409, 429, 5xx response status code.
172
+ ### MailchimpAPI.audience_members
125
173
 
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
- ```
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
132
182
 
133
- ### Option `:http_opts`
183
+ ### MailchimpAPI.audience_member_tags
134
184
 
135
- This are the options that are provided to the `Net::HTTP.start` method,
136
- like `:read_timeout`, `:write_timeout`, etc.
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
137
188
 
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).
189
+ ### MailchimpAPI.audience_segments
140
190
 
141
- By default it is an empty hash.
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
142
198
 
143
- ```ruby
144
- client = MailchimpAPI::Client.new(
145
- http_opts: {read_timeout: 30, write_timeout: 30, open_timeout: 30}
146
- # ...
147
- )
148
- ```
199
+ ### MailchimpAPI.audience_segment_members
149
200
 
150
- ## Pagination
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
151
205
 
152
- All REST resources have `#each` method. Example:
206
+ ### MailchimpAPI.audience_interest_categories
153
207
 
154
- ```ruby
155
- MailchimpAPI.audience_members.each(list_id) { |member| ... }
156
- MailchimpAPI.audience_member_tags.each(list_id, member_id) { |tag| ... }
157
- ```
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
158
214
 
159
- `Each` method accepts `query`, `body` and `headers` as any other API.
160
- By default it requests 1000 items on each page, but it is configurable through
161
- `query` parameter. Example:
215
+ ### MailchimpAPI.audience_interests
162
216
 
163
- ```ruby
164
- MailchimpAPI.audience_members.each(list_id, query: { count: 100 }) { |member| ... }
165
- ```
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
166
223
 
167
- ## Request
224
+ ### MailchimpAPI.audience_webhooks
168
225
 
169
- ...
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
170
232
 
171
- ## APIs
233
+ ### Pagination
172
234
 
173
- ### Audience Members
235
+ Most resources support pagination through the `each` method:
174
236
 
175
- - List `MailchimpAPI.audience_members.get(audience_id)`
176
- - Create `MailchimpAPI.audience_members.create(audience_id, body: body)`
177
- - Show `MailchimpAPI.audience_members.show(audience_id, email)`
178
- - Update `MailchimpAPI.audience_members.update(audience_id, email, body: body)`
179
- - Add or Update `MailchimpAPI.audience_members.add_or_update(audience_id, email, body: body)`
180
- - Archive `MailchimpAPI.audience_members.delete(audience_id, email)`
181
- - Delete Permanently `MailchimpAPI.audience_members.delete(audience_id, email)`
182
-
183
- ### Audience Member Tags
184
-
185
- - List `MailchimpAPI.audience_member_tags.get(audience_id, email)`
186
- - 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
187
242
 
188
- ### 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
+ ```
189
248
 
190
- - List `MailchimpAPI.audience_webhooks.get(audience_id)`
191
- - Create `MailchimpAPI.audience_webhooks.create(audience_id, body: body)`
192
- - Show `MailchimpAPI.audience_webhooks.show(audience_id, webhook_id)`
193
- - Update `MailchimpAPI.audience_webhooks.update(audience_id, webhook_id, body: body)`
194
- - Delete `MailchimpAPI.audience_webhooks.delete(audience_id, webhook_id)`
249
+ ## Development
195
250
 
196
- ### Audience Interest Categories
251
+ After checking out the repo, run:
197
252
 
198
- - List `MailchimpAPI.audience_interest_categories.get(audience_id)`
199
- - Create `MailchimpAPI.audience_interest_categories.create(audience_id, body: body)`
200
- - Show `MailchimpAPI.audience_interest_categories.show(audience_id, webhook_id)`
201
- - Update `MailchimpAPI.audience_interest_categories.update(audience_id, webhook_id, body: body)`
202
- - Delete `MailchimpAPI.audience_interest_categories.delete(audience_id, webhook_id)`
253
+ ```bash
254
+ bundle install
255
+ ```
203
256
 
204
- ### Audience Interests
257
+ To run tests:
205
258
 
206
- - List `MailchimpAPI.audience_interests.get(audience_id)`
207
- - Create `MailchimpAPI.audience_interests.create(audience_id, body: body)`
208
- - Show `MailchimpAPI.audience_interests.show(audience_id, webhook_id)`
209
- - Update `MailchimpAPI.audience_interests.update(audience_id, webhook_id, body: body)`
210
- - Delete `MailchimpAPI.audience_interests.delete(audience_id, webhook_id)`
259
+ ```bash
260
+ bundle exec rspec
261
+ ```
211
262
 
212
- ## Development
263
+ To run linters:
213
264
 
214
265
  ```bash
215
- rubocop
216
- rspec
217
- mdl README.md CHANGELOG.md RELEASE.md
266
+ bundle exec rubocop
267
+ bundle exec mdl README.md CHANGELOG.md RELEASE.md
218
268
  ```
219
269
 
220
270
  ## Contributing
221
271
 
222
- 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).
223
273
 
224
274
  ## License
225
275
 
226
276
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
227
-
228
- [pagination]: #pagination
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.1
@@ -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