gibbon 1.2.1 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of gibbon might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.travis.yml +0 -2
- data/CHANGELOG.md +10 -6
- data/README.markdown +45 -149
- data/gibbon.gemspec +9 -7
- data/lib/gibbon.rb +3 -4
- data/lib/gibbon/api_request.rb +167 -0
- data/lib/gibbon/mailchimp_error.rb +1 -1
- data/lib/gibbon/request.rb +65 -0
- data/lib/gibbon/version.rb +2 -2
- data/spec/gibbon/gibbon_spec.rb +40 -240
- metadata +17 -18
- data/lib/gibbon/api.rb +0 -38
- data/lib/gibbon/api_category.rb +0 -110
- data/lib/gibbon/export.rb +0 -106
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6f3bc3884010b6bc30397d25c746ca29f6b026cf
|
4
|
+
data.tar.gz: acf67934d1a27976e15324502773b0029978f3a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 72ddec2ab87bdb95d5bcb25f0728eeece2a70daaf49a990444c2dfd4b97c28e093c07b644a0538383684e05482977ec8eeb7c4d4210e7d5e25aa8bd2710a5835
|
7
|
+
data.tar.gz: a20ba104983c405b478dad32c3cc507521c80dd3b36e39cf1d208201bf526a1bc02eec889b370ad946b39a4f83988db648ed8de01f58e1a2dd9dd30d44ea7b72
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
## [Unreleased][unreleased]
|
2
|
-
|
3
|
-
## [
|
4
|
-
-
|
2
|
+
|
3
|
+
## [2.0.0] - 2015-7-28
|
4
|
+
- Support for API 3.0. Usage syntax has changed. Please check out the readme.
|
5
|
+
- Update MultiJSON dependency to 1.11.0
|
6
|
+
- Switch to Faraday
|
7
|
+
- Fix: Handle empty response payloads on delete
|
5
8
|
|
6
9
|
## [1.2.0] - 2015-07-16
|
7
10
|
- Same as 1.1.6 but rereleased because it's a breaking change
|
@@ -18,7 +21,8 @@
|
|
18
21
|
## [1.1.4] - 2012-11-04
|
19
22
|
- Fix JSON::ParserError on export calls that return blank results
|
20
23
|
|
21
|
-
[
|
22
|
-
[
|
24
|
+
[unreleased]: https://github.com/amro/gibbon/compare/v2.0.0...HEAD
|
25
|
+
[2.0.0]: https://github.com/amro/gibbon/compare/v1.2.0...v2.0.0
|
26
|
+
[1.2.0]: https://github.com/amro/gibbon/compare/v1.1.5...v1.2.0
|
27
|
+
[1.1.5]: https://github.com/amro/gibbon/compare/v1.1.5...v1.1.4
|
23
28
|
[1.1.4]: https://github.com/amro/gibbon/compare/v1.1.3...v1.1.4
|
24
|
-
[1.1.5]: https://github.com/amro/gibbon/compare/v1.1.4...v1.1.5
|
data/README.markdown
CHANGED
@@ -1,16 +1,14 @@
|
|
1
1
|
# gibbon
|
2
2
|
|
3
|
-
Gibbon is an API wrapper for MailChimp's [
|
3
|
+
Gibbon is an API wrapper for MailChimp's [API](http://kb.mailchimp.com/api/).
|
4
4
|
|
5
5
|
[![Build Status](https://secure.travis-ci.org/amro/gibbon.png)](http://travis-ci.org/amro/gibbon)
|
6
6
|
[![Dependency Status](https://gemnasium.com/amro/gibbon.png)](https://gemnasium.com/amro/gibbon)
|
7
7
|
##Important Notes
|
8
8
|
|
9
|
-
Gibbon now targets MailChimp API
|
9
|
+
Gibbon now targets MailChimp API 3.0, which is substantially different from the previous API. Please use Gibbon 1.1.x if you need to use API 2.0.
|
10
10
|
|
11
|
-
|
12
|
-
* Errors are raised by default
|
13
|
-
* Timeouts can be specified per request during initialization
|
11
|
+
Please read MailChimp's [Getting Started Guide](http://kb.mailchimp.com/api/article/api-3-overview).
|
14
12
|
|
15
13
|
##Installation
|
16
14
|
|
@@ -22,197 +20,95 @@ A MailChimp account and API key. You can see your API keys [here](http://admin.m
|
|
22
20
|
|
23
21
|
##Usage
|
24
22
|
|
25
|
-
|
23
|
+
First, create an instance Gibbon::Request:
|
26
24
|
|
27
|
-
|
25
|
+
gibbon = Gibbon::Request.new(api_key: "your_api_key")
|
28
26
|
|
29
|
-
|
27
|
+
You can set an individual request's timeout like this:
|
30
28
|
|
31
|
-
|
29
|
+
gibbon.timeout = 10
|
32
30
|
|
33
|
-
|
34
|
-
|
35
|
-
Gibbon::API.throws_exceptions = false
|
31
|
+
Now you can make requests using the resources defined in [MailChimp's docs](http://kb.mailchimp.com/api/resources). Resource IDs
|
32
|
+
are specified inline and a `CRUD` (`create`, `retrieve`, `update`, or `delete`) verb initiates the request.
|
36
33
|
|
37
|
-
|
34
|
+
gibbon.lists.retrieve
|
38
35
|
|
39
|
-
|
40
|
-
Gibbon::Export.timeout = 15
|
41
|
-
Gibbon::Export.throws_exceptions = false
|
36
|
+
Retrieving a specific list looks like:
|
42
37
|
|
43
|
-
|
38
|
+
gibbon.lists(list_id).retrieve
|
44
39
|
|
45
|
-
|
40
|
+
Retrieving a specific list's members looks like:
|
46
41
|
|
47
|
-
|
42
|
+
gibbon.lists(list_id).members.retrieve
|
48
43
|
|
49
|
-
You can also
|
44
|
+
You can also specify `headers`, `params`, and `body` when calling a `CRUD` method. For example:
|
50
45
|
|
51
|
-
|
46
|
+
gibbon.lists.retrieve(headers: {"SomeHeader": "SomeHeaderValue"}, params: {"query_param": "query_param_value"})
|
52
47
|
|
53
|
-
|
48
|
+
Of course, `body` is only supported on `create` and `update` calls. Those map to HTTP `POST` and `PATCH` verbs.
|
54
49
|
|
55
|
-
|
56
|
-
object with a given category (e.g. campaigns.list). The API calls may be made with either camelcase or underscore
|
57
|
-
separated formatting as you see in the "More Advanced Examples" section below.
|
50
|
+
You can set `api_key` and `timeout` globally:
|
58
51
|
|
59
|
-
|
52
|
+
Gibbon::Request.api_key = "your_api_key"
|
53
|
+
Gibbon::Request.timeout = 15
|
60
54
|
|
61
|
-
|
55
|
+
For example, you could set the values above in an `initializer` file in your `Rails` app (e.g. your\_app/config/initializers/gibbon.rb).
|
62
56
|
|
63
|
-
|
57
|
+
Assuming you've set an `api_key` on Gibbon, you can conveniently make API calls on the class itself:
|
64
58
|
|
65
|
-
|
59
|
+
Gibbon::Request.lists.retrieve
|
66
60
|
|
67
|
-
|
68
|
-
|
69
|
-
Similarly, to fetch your first 100 lists:
|
61
|
+
You can also set the environment variable `MAILCHIMP_API_KEY` and Gibbon will use it when you create an instance:
|
70
62
|
|
71
|
-
|
63
|
+
gb = Gibbon::Request.new
|
72
64
|
|
73
|
-
|
65
|
+
MailChimp's [resource documentation](http://kb.mailchimp.com/api/resources) is a list of available resources. Substitute an underscore if
|
66
|
+
a resource name contains a hyphen.
|
74
67
|
|
75
|
-
|
68
|
+
### Fetching Campaigns
|
76
69
|
|
77
|
-
### More Advanced Examples
|
78
70
|
|
79
|
-
|
71
|
+
campaigns = gb.campaigns.retrieve
|
80
72
|
|
81
|
-
|
73
|
+
### Fetching Lists
|
82
74
|
|
83
|
-
|
75
|
+
Similarly, to fetch your lists
|
84
76
|
|
85
|
-
gb.lists.
|
77
|
+
lists = gb.lists.retrieve
|
86
78
|
|
87
|
-
|
79
|
+
### More Advanced Examples
|
88
80
|
|
89
|
-
List
|
81
|
+
List subscribers for a list:
|
90
82
|
|
91
|
-
gb.lists
|
83
|
+
gb.lists(list_id).members.retrieve
|
92
84
|
|
93
85
|
Subscribe a member to a list:
|
94
86
|
|
95
|
-
gb.lists.
|
96
|
-
|
97
|
-
Here's an example showing pagination. The following code fetches the first page of 100 members subscribed to your list:
|
98
|
-
|
99
|
-
gb.lists.members({:id => list_id, :opts => {:start => 0, :limit => 100}})
|
100
|
-
|
101
|
-
or
|
102
|
-
|
103
|
-
Batch subscribe members to a list:
|
104
|
-
|
105
|
-
gb.lists.batch_subscribe(:id => list_id, :batch => [{:email => {:email => "email1"}, :merge_vars => {:FNAME => "FirstName1", :LNAME => "LastName1"}},{:email => {:email =>"email2"}, :merge_vars => {:FNAME => "FirstName2", :LNAME => "LastName2"}}])
|
106
|
-
|
107
|
-
> Note: This will send welcome emails to the new subscribers
|
108
|
-
|
109
|
-
If you want to update the existing members you need to send the boolean update_existing in true
|
110
|
-
|
111
|
-
gb.lists.batch_subscribe(:id => list_id, :batch => [{:email => {:email => "email1"}, :merge_vars => {:FNAME => "FirstName1", :LNAME => "LastName1"}}], :update_existing => true)
|
112
|
-
|
113
|
-
> Note: The `email` hash can also accept either a unique email id or a list email id. Please see the [lists/batch-subscribe](http://apidocs.mailchimp.com/api/2.0/lists/batch-subscribe.php) documentation for more information.
|
87
|
+
gb.lists(list_id).members.create(body: {email_address: "email_address", status: "subscribed", merge_fields: {FNAME: "First Name", LNAME: "Last Name"}})
|
114
88
|
|
115
89
|
You can also unsubscribe a member from a list:
|
116
90
|
|
117
|
-
gb.lists.
|
118
|
-
|
119
|
-
> Note: :delete_member defaults to false, meaning the member stays on your mailchimp list as "unsubscribed". See [Api Docs](http://apidocs.mailchimp.com/api/2.0/lists/unsubscribe.php) for details of options.
|
91
|
+
gb.lists(list_id).members(member_id).update(body: { status: "unsubscribed" })
|
120
92
|
|
121
|
-
Fetch
|
93
|
+
Fetch the number of opens for a campaign
|
122
94
|
|
123
|
-
email_stats = gb.reports
|
124
|
-
|
125
|
-
or
|
126
|
-
|
127
|
-
Create a campaign:
|
128
|
-
|
129
|
-
gb.campaigns.create({type: "regular", options: {list_id: list_id, subject: "Gibbon is cool", from_email: "you@example.com", from_name: "Darth Vader", generate_text: true}, content: {html: "<html><head></head><body><h1>Foo</h1><p>Bar</p></body></html>"}})
|
95
|
+
email_stats = gb.reports("13e9a94053").retrieve["opens"]
|
130
96
|
|
131
97
|
Overriding Gibbon's API endpoint (i.e. if using an access token from OAuth and have the `api_endpoint` from the [metadata](http://apidocs.mailchimp.com/oauth2/)):
|
132
98
|
|
133
|
-
Gibbon::
|
134
|
-
Gibbon::
|
135
|
-
|
136
|
-
### Setting timeouts
|
137
|
-
|
138
|
-
Gibbon defaults to a 30 second timeout. You can optionally set your own timeout (in seconds) like so:
|
139
|
-
|
140
|
-
gb = Gibbon::API.new("your_api_key", {:timeout => 5})
|
141
|
-
|
142
|
-
or
|
143
|
-
|
144
|
-
gb.timeout = 5
|
99
|
+
Gibbon::Request.api_endpoint = "https://us1.api.mailchimp.com"
|
100
|
+
Gibbon::Request.api_key = your_access_token_or_api_key
|
145
101
|
|
146
102
|
### Error handling
|
147
103
|
|
148
|
-
|
149
|
-
|
150
|
-
If you set the `throws_exceptions` boolean attribute to false, for a given instance,
|
151
|
-
then Gibbon will not raise exceptions. This allows you to handle errors manually. The
|
152
|
-
APIs will return a Hash with two keys "errors", a string containing some textual
|
153
|
-
information about the error, and "code", the numeric code of the error.
|
154
|
-
|
155
|
-
If you rescue Gibbon::MailChimpError, you are provided with the error message itself as well as
|
156
|
-
a `code` attribute that you can map onto the API's error list. The API docs list possible errors
|
157
|
-
at the bottom of each page. Here's how you might do that:
|
158
|
-
|
159
|
-
begin
|
160
|
-
g.lists.subscribe(...)
|
161
|
-
rescue Gibbon::MailChimpError => e
|
162
|
-
# do something with e.message here
|
163
|
-
# do something wiht e.code here
|
164
|
-
end
|
165
|
-
|
166
|
-
Some API endpoints, like `[lists/batch-subscribe](http://apidocs.mailchimp.com/api/2.0/lists/batch-subscribe.php)`
|
167
|
-
return errors to let you know that some of your actions failed, but some suceeded. Gibbon will not
|
168
|
-
raise Gibbon::MailChimpError for these endpoints because the key for the success count varies from endpoint to endpoint.
|
169
|
-
This makes it difficult to determine whether all of your actions failed in a generic way. **Because of this, you're responsible
|
170
|
-
for checking the response body for the `errors` array in these cases.**
|
171
|
-
|
172
|
-
> Note: In an effort to make Gibbon easier to use, errors are raised automatically as of version 0.4.0.
|
173
|
-
|
174
|
-
### Export API usage
|
175
|
-
|
176
|
-
In addition to the primary API, you can make calls to the [Export API](http://apidocs.mailchimp.com/export/1.0/) using an instance of GibbonExport. Given an existing instance of Gibbon, you can request a new GibbonExporter object:
|
177
|
-
|
178
|
-
g = Gibbon::API.new("your_api_key")
|
179
|
-
gibbon_export = g.get_exporter
|
180
|
-
|
181
|
-
or you can construct a new object directly:
|
182
|
-
|
183
|
-
gibbon_export = Gibbon::Export.new("your_api_key")
|
184
|
-
|
185
|
-
Making calls to Export API endpoints is similar to making standard API calls but the
|
186
|
-
return value is an Enumerator which loops over the lines returned from the
|
187
|
-
Export API. This is because the data returned from the Export API is a stream
|
188
|
-
of JSON objects rather than a single JSON array.
|
189
|
-
|
190
|
-
For example, dumping list members via the "list" method works like this:
|
191
|
-
|
192
|
-
gibbon_export.list({:id => *list_id*})
|
193
|
-
|
194
|
-
One can also use this in a streaming fashion, where each row is parsed on it comes in like this:
|
195
|
-
|
196
|
-
gibbon_export.list({:id => *list_id*}) { |row| *do_sth_with* row }
|
197
|
-
|
198
|
-
For the streaming functionality, it is important to supply an explicit block / procedure to the export functions, not an implicit one. So, the preceding and following one will work. Please note this method also includes a counter (*i*, starting at 0) telling which row of data you're receiving:
|
199
|
-
```
|
200
|
-
method = Proc.new do |row, i|
|
201
|
-
*do_sth_with* row
|
202
|
-
end
|
203
|
-
gibbon_export.list(params, &method)
|
204
|
-
```
|
104
|
+
Gibbon raises an error when the API returns an error.
|
205
105
|
|
206
|
-
|
207
|
-
|
208
|
-
gibbon_export.list({:id => *list_id*}) do |row|
|
209
|
-
*do_sth_with* row
|
210
|
-
end
|
211
|
-
```
|
106
|
+
Gibbon::MailChimpError has the following attributes: `title`, `detail`, `body`, `raw_body`, `status_code`. Some or all of these may not be
|
107
|
+
available depending on the nature of the error.
|
212
108
|
|
213
109
|
##Thanks
|
214
110
|
|
215
|
-
Thanks to everyone who
|
111
|
+
Thanks to everyone who has [contributed](https://github.com/amro/gibbon/contributors) to Gibbon's development.
|
216
112
|
|
217
113
|
##Copyright
|
218
114
|
|
data/gibbon.gemspec
CHANGED
@@ -9,12 +9,13 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.email = ["amromousa@gmail.com"]
|
10
10
|
s.homepage = "http://github.com/amro/gibbon"
|
11
11
|
|
12
|
-
s.summary = %q{A wrapper for MailChimp API
|
13
|
-
s.description = %q{A wrapper for MailChimp API
|
12
|
+
s.summary = %q{A wrapper for MailChimp API 3.0}
|
13
|
+
s.description = %q{A wrapper for MailChimp API 3.0}
|
14
14
|
s.license = "MIT"
|
15
15
|
|
16
|
-
s.post_install_message = "IMPORTANT: Gibbon now targets MailChimp API
|
17
|
-
Please
|
16
|
+
s.post_install_message = "IMPORTANT: Gibbon now targets MailChimp API 3.0, which is very different from API 2.0.\n \
|
17
|
+
Please install Gibbon 1.2.0 if you need to use API 2.0.\nGibbon's API has changed substantially \
|
18
|
+
between versions 1.x and 2.x."
|
18
19
|
|
19
20
|
s.rubyforge_project = "gibbon"
|
20
21
|
|
@@ -22,11 +23,12 @@ Gem::Specification.new do |s|
|
|
22
23
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
23
24
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
24
25
|
s.require_paths = ["lib"]
|
26
|
+
s.required_ruby_version = '>= 2.0.0'
|
25
27
|
|
26
|
-
s.add_dependency('
|
27
|
-
s.add_dependency('multi_json', '>= 1.
|
28
|
+
s.add_dependency('faraday', '>= 0.9.1')
|
29
|
+
s.add_dependency('multi_json', '>= 1.11.0')
|
28
30
|
|
29
31
|
s.add_development_dependency 'rake'
|
30
|
-
s.add_development_dependency "rspec", "3.
|
32
|
+
s.add_development_dependency "rspec", "3.2.0"
|
31
33
|
|
32
34
|
end
|
data/lib/gibbon.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
|
-
require '
|
1
|
+
require 'faraday'
|
2
2
|
require 'multi_json'
|
3
3
|
require 'cgi'
|
4
4
|
|
5
5
|
require 'gibbon/gibbon_error'
|
6
6
|
require 'gibbon/mailchimp_error'
|
7
|
-
require 'gibbon/
|
8
|
-
require 'gibbon/
|
9
|
-
require 'gibbon/export'
|
7
|
+
require 'gibbon/request'
|
8
|
+
require 'gibbon/api_request'
|
10
9
|
|
11
10
|
module Gibbon
|
12
11
|
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
module Gibbon
|
2
|
+
class APIRequest
|
3
|
+
def initialize(builder: nil)
|
4
|
+
@request_builder = builder
|
5
|
+
end
|
6
|
+
|
7
|
+
def post(params: nil, headers: nil, body: nil)
|
8
|
+
validate_api_key
|
9
|
+
|
10
|
+
begin
|
11
|
+
response = self.rest_client.post do |request|
|
12
|
+
configure_request(request: request, params: params, headers: headers, body: MultiJson.dump(body))
|
13
|
+
end
|
14
|
+
parse_response(response.body)
|
15
|
+
rescue => e
|
16
|
+
handle_error(e)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def patch(params: nil, headers: nil, body: nil)
|
21
|
+
validate_api_key
|
22
|
+
|
23
|
+
begin
|
24
|
+
response = self.rest_client.patch do |request|
|
25
|
+
configure_request(request: request, params: params, headers: headers, body: MultiJson.dump(body))
|
26
|
+
end
|
27
|
+
parse_response(response.body)
|
28
|
+
rescue => e
|
29
|
+
handle_error(e)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def get(params: nil, headers: nil)
|
34
|
+
validate_api_key
|
35
|
+
|
36
|
+
begin
|
37
|
+
response = self.rest_client.get do |request|
|
38
|
+
configure_request(request: request, params: params, headers: headers)
|
39
|
+
end
|
40
|
+
parse_response(response.body)
|
41
|
+
rescue => e
|
42
|
+
handle_error(e)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def delete(params: nil, headers: nil)
|
47
|
+
validate_api_key
|
48
|
+
|
49
|
+
begin
|
50
|
+
response = self.rest_client.delete do |request|
|
51
|
+
configure_request(request: request, params: params, headers: headers)
|
52
|
+
end
|
53
|
+
parse_response(response.body)
|
54
|
+
rescue => e
|
55
|
+
handle_error(e)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
protected
|
60
|
+
|
61
|
+
# Convenience accessors
|
62
|
+
|
63
|
+
def api_key
|
64
|
+
@request_builder.api_key
|
65
|
+
end
|
66
|
+
|
67
|
+
def api_endpoint
|
68
|
+
@request_builder.api_endpoint
|
69
|
+
end
|
70
|
+
|
71
|
+
def timeout
|
72
|
+
@request_builder.timeout
|
73
|
+
end
|
74
|
+
|
75
|
+
# Helpers
|
76
|
+
|
77
|
+
def handle_error(error)
|
78
|
+
error_to_raise = nil
|
79
|
+
|
80
|
+
begin
|
81
|
+
error_to_raise = MailChimpError.new(error.message)
|
82
|
+
|
83
|
+
if error.is_a?(Faraday::Error::ClientError) && error.response
|
84
|
+
parsed_response = MultiJson.load(error.response[:body])
|
85
|
+
|
86
|
+
if parsed_response
|
87
|
+
error_to_raise.body = parsed_response
|
88
|
+
error_to_raise.title = parsed_response["title"] if parsed_response["title"]
|
89
|
+
error_to_raise.detail = parsed_response["detail"] if parsed_response["detail"]
|
90
|
+
end
|
91
|
+
|
92
|
+
error_to_raise.status_code = error.response[:status]
|
93
|
+
error_to_raise.raw_body = error.response[:body]
|
94
|
+
end
|
95
|
+
rescue MultiJson::ParseError
|
96
|
+
error_to_raise.message = error.message
|
97
|
+
error_to_raise.status_code = error.response[:status]
|
98
|
+
end
|
99
|
+
|
100
|
+
raise error_to_raise
|
101
|
+
end
|
102
|
+
|
103
|
+
def configure_request(request: nil, params: nil, headers: nil, body: nil)
|
104
|
+
if request
|
105
|
+
request.params.merge!(params) if params
|
106
|
+
request.headers['Content-Type'] = 'application/json'
|
107
|
+
request.headers.merge!(headers) if headers
|
108
|
+
request.body = body if body
|
109
|
+
request.options.timeout = self.timeout
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def rest_client
|
114
|
+
client = Faraday.new(url: self.api_url) do |faraday|
|
115
|
+
faraday.response :raise_error
|
116
|
+
faraday.adapter Faraday.default_adapter
|
117
|
+
end
|
118
|
+
client.basic_auth('apikey', self.api_key)
|
119
|
+
client
|
120
|
+
end
|
121
|
+
|
122
|
+
def parse_response(response_body)
|
123
|
+
parsed_response = nil
|
124
|
+
|
125
|
+
if response_body && !response_body.empty?
|
126
|
+
begin
|
127
|
+
parsed_response = MultiJson.load(response_body)
|
128
|
+
rescue MultiJson::ParseError
|
129
|
+
error = MailChimpError.new("Unparseable response: #{response_body}")
|
130
|
+
error.title = "UNPARSEABLE_RESPONSE"
|
131
|
+
error.status_code = 500
|
132
|
+
raise error
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
parsed_response
|
137
|
+
end
|
138
|
+
|
139
|
+
def validate_api_key
|
140
|
+
api_key = self.api_key
|
141
|
+
unless api_key && (api_key["-"] || self.api_endpoint)
|
142
|
+
raise Gibbon::GibbonError, "You must set an api_key prior to making a call"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def api_url
|
147
|
+
base_api_url + @request_builder.path
|
148
|
+
end
|
149
|
+
|
150
|
+
def base_api_url
|
151
|
+
computed_api_endpoint = "https://#{get_data_center_from_api_key}api.mailchimp.com"
|
152
|
+
"#{self.api_endpoint || computed_api_endpoint}/3.0/"
|
153
|
+
end
|
154
|
+
|
155
|
+
def get_data_center_from_api_key
|
156
|
+
# Return an empty string for invalid API keys so Gibbon hits the main endpoint
|
157
|
+
data_center = ""
|
158
|
+
|
159
|
+
if self.api_key && self.api_key["-"]
|
160
|
+
# Add a period since the data_center is a subdomain and it keeps things dry
|
161
|
+
data_center = "#{self.api_key.split('-').last}."
|
162
|
+
end
|
163
|
+
|
164
|
+
data_center
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|