mailchimp3 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ed47a5c9aa596d1bd8ab0960067fecd28276112
4
- data.tar.gz: c4106451d50a559f56e15b552d76e26cfbabfb93
3
+ metadata.gz: 86cbb578a02bfc1b1f33bf8a3394f5a4d324adb8
4
+ data.tar.gz: 41a06107a92fce74c0a282972fc9994acd6d7ec5
5
5
  SHA512:
6
- metadata.gz: c58c68f2c6b1fcff8db1f90bf260ecf565ed74384053635353470f9512a6eb1591d186a35b186e72cb968ae98c2b6ff535a1e7de37aa29ba6a88d5e9d7974f81
7
- data.tar.gz: 1a4a7c917560dba01b9e71a39f5570dc432af9a0589c0641c4ada87dc726d4fc23e233b802f24d663617df721ac12902856b3ff144653907652ea2f9bd651e39
6
+ metadata.gz: 2f88cecce1c849cfdcb88a77dd4a8b73a4effbd1b0539e7f979d1d2db4d06871c4914de90e058d261860972246efadde5e667d5ee09f3f05cb51b40eb4577cb4
7
+ data.tar.gz: 40cbb2dcf74b50a58a6faf091904fa24d37c0eeb25a42a1997bff0738d90f592638bfbbd7fb805723b6f62290290c881f8cb4222ab2799ab0595cf20acbd19f8
data/README.md CHANGED
@@ -3,12 +3,17 @@
3
3
  [![Circle CI](https://circleci.com/gh/seven1m/mailchimp3/tree/master.svg?style=svg)](https://circleci.com/gh/seven1m/mailchimp3/tree/master)
4
4
 
5
5
  `mailchimp3` is a Rubygem that provides a very thin, simple wrapper around the MailChimp RESTful JSON API version 3.0
6
- documented at [kb.mailchimp.com/api](http://kb.mailchimp.com/api/).
6
+ documented at [kb.mailchimp.com/api](http://kb.mailchimp.com/api/). (The wrapper also works with the 2.0 API if you
7
+ just use the `post` method. See 2.0 section later in this document.)
8
+
9
+ This wrapper is very low-level -- you'll still be dealing with individual GET, POST, PATCH, and DELETE requests, but the gem
10
+ handles the OAuth2 flow (getting a token), passing the auth token in every request, and raises descriptive errors you can easily rescue
11
+ in your own code.
7
12
 
8
13
  ## Installation
9
14
 
10
15
  ```
11
- gem install mailchimp3 (eventually)
16
+ gem install mailchimp3
12
17
  ```
13
18
 
14
19
  ## Usage with HTTP Basic Auth
@@ -225,17 +230,17 @@ api.lists['abc123'].members['cde345'].delete
225
230
 
226
231
  The following errors may be raised by the library, depending on the API response status code.
227
232
 
228
- | HTTP Status Codes | Error Class |
229
- | ------------------- | ----------------------------------------------------------------------------- |
230
- | 400 | `MailChimp3::Errors::BadRequest` < `MailChimp3::Errors::ClientError` |
231
- | 401 | `MailChimp3::Errors::Unauthorized` < `MailChimp3::Errors::ClientError` |
232
- | 403 | `MailChimp3::Errors::Forbidden` < `MailChimp3::Errors::ClientError` |
233
- | 404 | `MailChimp3::Errors::NotFound` < `MailChimp3::Errors::ClientError` |
234
- | 405 | `MailChimp3::Errors::MethodNotAllowed` < `MailChimp3::Errors::ClientError` |
235
- | 422 | `MailChimp3::Errors::UnprocessableEntity` < `MailChimp3::Errors::ClientError` |
236
- | other 4xx errors | `MailChimp3::Errors::ClientError` |
237
- | 500 | `MailChimp3::Errors::InternalServerError` < `MailChimp3::Errors::ServerError` |
238
- | other 5xx errors | `MailChimp3::Errors::ServerError` |
233
+ | HTTP Status Codes | Specific Error Class | Generic Error Class |
234
+ | ------------------- | ----------------------------------------- | --------------------------------- |
235
+ | 400 | `MailChimp3::Errors::BadRequest` | `MailChimp3::Errors::ClientError` |
236
+ | 401 | `MailChimp3::Errors::Unauthorized` | `MailChimp3::Errors::ClientError` |
237
+ | 403 | `MailChimp3::Errors::Forbidden` | `MailChimp3::Errors::ClientError` |
238
+ | 404 | `MailChimp3::Errors::NotFound` | `MailChimp3::Errors::ClientError` |
239
+ | 405 | `MailChimp3::Errors::MethodNotAllowed` | `MailChimp3::Errors::ClientError` |
240
+ | 422 | `MailChimp3::Errors::UnprocessableEntity` | `MailChimp3::Errors::ClientError` |
241
+ | other 4xx errors | `MailChimp3::Errors::ClientError` | |
242
+ | 500 | `MailChimp3::Errors::InternalServerError` | `MailChimp3::Errors::ServerError` |
243
+ | other 5xx errors | `MailChimp3::Errors::ServerError` | |
239
244
 
240
245
  The exception object has the following methods:
241
246
 
@@ -268,6 +273,21 @@ The `message` should be a simple string given by the API, e.g. "Resource Not Fou
268
273
  Alternatively, you may rescue `MailChimp3::Errors::BaseError` and branch your code based on
269
274
  the status code returned by calling `error.status`.
270
275
 
276
+ ## MailChimp 2.0 API
277
+
278
+ This wrapper works with MailChimp's soon-to-be-deprecated 2.0 API. You need only use the `v2` path and the `post` method
279
+ for all your calls, like so:
280
+
281
+ ```ruby
282
+ api.v2.lists['member-activity'].post(id: 'abc123', emails: [{email: 'tim@timmorgan.org'}])
283
+ ```
284
+
285
+ Some notes:
286
+
287
+ 1. Call `v2` to switch to the 2.0 API, then add other path elements after that.
288
+ 2. Any path element with a hyphen will need to use the array access syntax, e.g. `lists['member-activity']` not `lists.member-activity`.
289
+ 3. You must use the `post` method for every call.
290
+
271
291
  ## Copyright & License
272
292
 
273
293
  Copyright 2015, Tim Morgan. Licensed MIT.
@@ -5,12 +5,13 @@ module MailChimp3
5
5
  class Endpoint
6
6
  attr_reader :url, :last_result
7
7
 
8
- def initialize(oauth_access_token: nil, basic_auth_key: nil, dc: nil, url: nil)
8
+ def initialize(oauth_access_token: nil, basic_auth_key: nil, dc: nil, url: nil, version: 3)
9
9
  @oauth_access_token = oauth_access_token
10
10
  @basic_auth_key = basic_auth_key
11
11
  @dc = dc
12
12
  @dc ||= @basic_auth_key.split('-').last if @basic_auth_key
13
13
  @url = url || _build_url
14
+ @version = version
14
15
  fail Errors::DataCenterRequiredError, 'You must pass dc.' unless @dc || @url
15
16
  @cache = {}
16
17
  end
@@ -30,6 +31,7 @@ module MailChimp3
30
31
 
31
32
  def post(body = {})
32
33
  @last_result = _connection.post(@url) do |req|
34
+ body[:apikey] = @oauth_access_token || @basic_auth_key if @version == 2
33
35
  req.body = body.to_json
34
36
  end
35
37
  _build_response(@last_result)
@@ -51,6 +53,15 @@ module MailChimp3
51
53
  end
52
54
  end
53
55
 
56
+ def v2
57
+ self.class.new(
58
+ url: _build_v2_url,
59
+ basic_auth_key: @basic_auth_key,
60
+ oauth_access_token: @oauth_access_token,
61
+ version: 2
62
+ )
63
+ end
64
+
54
65
  private
55
66
 
56
67
  def _build_response(result)
@@ -86,15 +97,18 @@ module MailChimp3
86
97
  self.class.new(
87
98
  url: File.join(url, path.to_s),
88
99
  basic_auth_key: @basic_auth_key,
89
- oauth_access_token: @oauth_access_token
100
+ oauth_access_token: @oauth_access_token,
101
+ version: @version
90
102
  )
91
103
  end
92
104
  end
93
105
 
94
106
  def _build_url
95
- url = "https://#{@dc}.api.mailchimp.com/3.0"
96
- url << '/' if url =~ /3\.0\z/ # workaround for the api requiring a slash on the root urls
97
- url
107
+ "https://#{@dc}.api.mailchimp.com/3.0/"
108
+ end
109
+
110
+ def _build_v2_url
111
+ "https://#{@dc}.api.mailchimp.com/2.0/"
98
112
  end
99
113
 
100
114
  def _connection
@@ -8,7 +8,9 @@ module MailChimp3
8
8
 
9
9
  def initialize(response)
10
10
  @status = response.status
11
- @message = if response.body.is_a?(Hash)
11
+ @message = if response.body.is_a?(Hash) && response.body['error']
12
+ "#{response.body['name']}: #{response.body['error']}"
13
+ elsif response.body.is_a?(Hash)
12
14
  "#{response.body['title']}: #{response.body['detail']}"
13
15
  else
14
16
  response.body.to_s
@@ -1,3 +1,3 @@
1
1
  module MailChimp3
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailchimp3
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Morgan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-29 00:00:00.000000000 Z
11
+ date: 2015-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday