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 +4 -4
- data/README.md +198 -150
- data/VERSION +1 -1
- data/lib/mailchimp-api/batch_request.rb +20 -3
- data/lib/mailchimp-api/client/api_methods.rb +37 -3
- data/lib/mailchimp-api/client/batch_methods.rb +63 -4
- data/lib/mailchimp-api/client.rb +60 -2
- data/lib/mailchimp-api/config.rb +18 -12
- data/lib/mailchimp-api/error.rb +35 -22
- data/lib/mailchimp-api/failed_request_error_builder.rb +11 -11
- data/lib/mailchimp-api/network_error_builder.rb +8 -11
- data/lib/mailchimp-api/pagination/list_each_item_helper.rb +12 -0
- data/lib/mailchimp-api/pagination/prepare_query_params.rb +16 -3
- data/lib/mailchimp-api/request.rb +40 -2
- data/lib/mailchimp-api/request_executor.rb +18 -4
- data/lib/mailchimp-api/resource.rb +2 -0
- data/lib/mailchimp-api/resources/audience/interest_categories.rb +63 -0
- data/lib/mailchimp-api/resources/audience/interests.rb +66 -0
- data/lib/mailchimp-api/resources/audience/member_tags.rb +39 -2
- data/lib/mailchimp-api/resources/audience/members.rb +92 -2
- data/lib/mailchimp-api/resources/audience/segment_members.rb +78 -0
- data/lib/mailchimp-api/resources/audience/segments.rb +125 -0
- data/lib/mailchimp-api/resources/audience/utils/subscriber_hash.rb +9 -0
- data/lib/mailchimp-api/resources/audience/webhooks.rb +63 -0
- data/lib/mailchimp-api/response.rb +30 -13
- data/lib/mailchimp-api/uri_builder.rb +13 -0
- data/lib/mailchimp-api.rb +139 -30
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6bb470c7bf3ad957b57ec7bd7c19299e5594de1de05fc4a24b683da82ee3c998
|
4
|
+
data.tar.gz: d7951ce1b9194282a03b9cd1743b513c11003c3c50f9edce9e839ee38370d795
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
6
|
-
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```console
|
20
|
+
gem 'mailchimp-rest-api'
|
7
21
|
```
|
8
22
|
|
9
|
-
|
23
|
+
And then execute:
|
10
24
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
- Pagination methods
|
25
|
+
```console
|
26
|
+
bundle install
|
27
|
+
```
|
15
28
|
|
16
|
-
##
|
29
|
+
## Quick Start
|
17
30
|
|
18
31
|
```ruby
|
19
|
-
#
|
20
|
-
MailchimpAPI.client = MailchimpAPI::Client.new(
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
MailchimpAPI.
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
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
|
-
|
53
|
+
## Configuration
|
39
54
|
|
40
|
-
|
55
|
+
### Client Configuration
|
41
56
|
|
42
|
-
|
57
|
+
The client accepts several configuration options:
|
43
58
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
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
|
-
|
88
|
+
### HTTP Options
|
51
89
|
|
52
|
-
|
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
|
-
|
92
|
+
- `read_timeout`
|
93
|
+
- `write_timeout`
|
94
|
+
- `open_timeout`
|
59
95
|
|
60
|
-
|
61
|
-
- `response.failed?` - checks HTTP status code is not 2xx
|
96
|
+
## API Usage
|
62
97
|
|
63
|
-
|
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
|
-
|
113
|
+
### Working with Responses
|
66
114
|
|
67
|
-
|
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
|
-
|
117
|
+
```ruby
|
118
|
+
response = MailchimpAPI.get('/some/path')
|
77
119
|
|
78
|
-
|
120
|
+
# Check response status
|
121
|
+
response.success? # true for 2xx status codes
|
122
|
+
response.failed? # true for non-2xx status codes
|
79
123
|
|
80
|
-
|
81
|
-
|
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
|
-
|
129
|
+
# Access HTTP details
|
130
|
+
response.http_status
|
131
|
+
response.http_body
|
132
|
+
response.http_headers
|
133
|
+
```
|
84
134
|
|
85
|
-
|
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
|
-
|
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
|
-
|
150
|
+
Available error classes:
|
110
151
|
|
111
|
-
|
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
|
-
|
114
|
-
- `:http_opts`
|
161
|
+
## Resources
|
115
162
|
|
116
|
-
|
163
|
+
Most resources support this APIs:
|
117
164
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
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
|
-
|
172
|
+
### MailchimpAPI.audience_members
|
125
173
|
|
126
|
-
|
127
|
-
|
128
|
-
|
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
|
-
###
|
183
|
+
### MailchimpAPI.audience_member_tags
|
134
184
|
|
135
|
-
|
136
|
-
|
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
|
-
|
139
|
-
(Please choose you version of ruby).
|
189
|
+
### MailchimpAPI.audience_segments
|
140
190
|
|
141
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
206
|
+
### MailchimpAPI.audience_interest_categories
|
153
207
|
|
154
|
-
|
155
|
-
|
156
|
-
|
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
|
-
|
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
|
-
|
164
|
-
|
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
|
-
|
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
|
-
|
233
|
+
### Pagination
|
172
234
|
|
173
|
-
|
235
|
+
Most resources support pagination through the `each` method:
|
174
236
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
251
|
+
After checking out the repo, run:
|
197
252
|
|
198
|
-
|
199
|
-
|
200
|
-
|
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
|
-
|
257
|
+
To run tests:
|
205
258
|
|
206
|
-
|
207
|
-
|
208
|
-
|
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
|
-
|
263
|
+
To run linters:
|
213
264
|
|
214
265
|
```bash
|
215
|
-
|
216
|
-
|
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
|
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.
|
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
|
-
|
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
|
-
#
|
15
|
-
#
|
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
|
-
#
|
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
|