mailchimp3 1.1.0 → 1.2.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: 86cbb578a02bfc1b1f33bf8a3394f5a4d324adb8
4
- data.tar.gz: 41a06107a92fce74c0a282972fc9994acd6d7ec5
3
+ metadata.gz: 10e1e2194f6330bc8836f6449e0d4a34cc94d951
4
+ data.tar.gz: 9bd5c607f83a710f2a93a70a449407ad4a210daf
5
5
  SHA512:
6
- metadata.gz: 2f88cecce1c849cfdcb88a77dd4a8b73a4effbd1b0539e7f979d1d2db4d06871c4914de90e058d261860972246efadde5e667d5ee09f3f05cb51b40eb4577cb4
7
- data.tar.gz: 40cbb2dcf74b50a58a6faf091904fa24d37c0eeb25a42a1997bff0738d90f592638bfbbd7fb805723b6f62290290c881f8cb4222ab2799ab0595cf20acbd19f8
6
+ metadata.gz: 03f478d9191b5b0d92534ff773cea17d207de3d14624cc50a2ce2b0d5638bd3e09283a90cd8c87b0d182fa3b779076b7b6cdb4dcffce82326b3c7bcc92016156
7
+ data.tar.gz: 06caf2489afca52ecdba0ffcb28b5fb2f4e310708b3b3230432b9a1dbdac9907e40f33c023aef22ad04bd25f181cfe3c86130cfbde6382ad9d6a535e048193e9
data/README.md CHANGED
@@ -112,7 +112,7 @@ gem install mailchimp3
112
112
 
113
113
  ```ruby
114
114
  api = MailChimp3.new(
115
- oauth_auth_token: user.mailchimp_auth_token,
115
+ oauth_access_token: user.mailchimp_auth_token,
116
116
  dc: user.mailchimp_data_center
117
117
  )
118
118
  ```
@@ -1,5 +1,5 @@
1
1
  require 'faraday'
2
- require 'faraday_middleware'
2
+ require 'json'
3
3
 
4
4
  module MailChimp3
5
5
  class Endpoint
@@ -65,33 +65,39 @@ module MailChimp3
65
65
  private
66
66
 
67
67
  def _build_response(result)
68
- case result.status
68
+ body = _parse_body(result)
69
+ case (status = result.status)
69
70
  when 200..299
70
- result.body
71
+ body
71
72
  when 400
72
- fail Errors::BadRequest, result
73
+ fail Errors::BadRequest, status: status, body: body
73
74
  when 401
74
- fail Errors::Unauthorized, result
75
+ fail Errors::Unauthorized, status: status, body: body
75
76
  when 403
76
- fail Errors::Forbidden, result
77
+ fail Errors::Forbidden, status: status, body: body
77
78
  when 404
78
- fail Errors::NotFound, result
79
+ fail Errors::NotFound, status: status, body: body
79
80
  when 405
80
- fail Errors::MethodNotAllowed, result
81
+ fail Errors::MethodNotAllowed, status: status, body: body
81
82
  when 422
82
- fail Errors::UnprocessableEntity, result
83
+ fail Errors::UnprocessableEntity, status: status, body: body
83
84
  when 400..499
84
- fail Errors::ClientError, result
85
+ fail Errors::ClientError, status: status, body: body
85
86
  when 500
86
- fail Errors::InternalServerError, result
87
+ fail Errors::InternalServerError, status: status, body: body
87
88
  when 500..599
88
- fail Errors::ServerError, result
89
+ fail Errors::ServerError, status: status, body: body
89
90
  else
90
- binding.pry
91
- fail "unknown status #{result.status}"
91
+ fail "unknown status #{status}"
92
92
  end
93
93
  end
94
94
 
95
+ def _parse_body(result)
96
+ JSON.parse(result.body)
97
+ rescue JSON::ParserError
98
+ raise Errors::ServerError, status: result.status, body: result.body
99
+ end
100
+
95
101
  def _build_endpoint(path)
96
102
  @cache[path] ||= begin
97
103
  self.class.new(
@@ -114,7 +120,6 @@ module MailChimp3
114
120
  def _connection
115
121
  @connection ||= Faraday.new(url: url) do |faraday|
116
122
  faraday.adapter :excon
117
- faraday.response :json, content_type: /\bjson$/
118
123
  if @basic_auth_key
119
124
  faraday.basic_auth '', @basic_auth_key
120
125
  elsif @oauth_access_token
@@ -6,16 +6,16 @@ module MailChimp3
6
6
  class BaseError < StandardError
7
7
  attr_reader :status, :details
8
8
 
9
- def initialize(response)
10
- @status = response.status
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)
14
- "#{response.body['title']}: #{response.body['detail']}"
9
+ def initialize(body:, status:)
10
+ @status = status
11
+ @message = if body.is_a?(Hash) && body['error']
12
+ "#{body['name']}: #{body['error']}"
13
+ elsif body.is_a?(Hash)
14
+ "#{body['title']}: #{body['detail']}"
15
15
  else
16
- response.body.to_s
16
+ body.to_s
17
17
  end
18
- @details = response.body if response.body.is_a?(Hash)
18
+ @details = body if body.is_a?(Hash)
19
19
  end
20
20
 
21
21
  def to_s
@@ -1,3 +1,3 @@
1
1
  module MailChimp3
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -68,13 +68,16 @@ describe MailChimp3::Endpoint do
68
68
  end
69
69
 
70
70
  it 'raises a NotFound error' do
71
- error = begin
72
- subject.get
73
- rescue MailChimp3::Errors::NotFound => e
74
- e
75
- end
76
- expect(error.status).to eq(404)
77
- expect(error.message).to eq('Resource Not Found: The requested resource could not be found.')
71
+ expect { subject.get }.to raise_error do |error|
72
+ expect(error).to be_a(MailChimp3::Errors::NotFound)
73
+ expect(error.status).to eq(404)
74
+ expect(error.message).to eq('Resource Not Found: The requested resource could not be found.')
75
+ expect(error.details).to eq(
76
+ 'status' => 404,
77
+ 'title' => 'Resource Not Found',
78
+ 'detail' => 'The requested resource could not be found.'
79
+ )
80
+ end
78
81
  end
79
82
  end
80
83
 
@@ -94,9 +97,7 @@ describe MailChimp3::Endpoint do
94
97
  end
95
98
 
96
99
  it 'raises a ClientError error' do
97
- expect {
98
- subject.get
99
- }.to raise_error(MailChimp3::Errors::ClientError)
100
+ expect { subject.get }.to raise_error(MailChimp3::Errors::ClientError)
100
101
  end
101
102
  end
102
103
 
@@ -116,9 +117,7 @@ describe MailChimp3::Endpoint do
116
117
  end
117
118
 
118
119
  it 'raises a ServerError error' do
119
- expect {
120
- subject.get
121
- }.to raise_error(MailChimp3::Errors::ServerError)
120
+ expect { subject.get }.to raise_error(MailChimp3::Errors::ServerError)
122
121
  end
123
122
  end
124
123
  end
@@ -132,21 +131,41 @@ describe MailChimp3::Endpoint do
132
131
  }
133
132
  end
134
133
 
135
- let(:result) do
136
- {
137
- 'id' => 'd3ed40bd7c',
138
- 'name' => 'Foo'
139
- }
140
- end
134
+ context do
135
+ let(:result) do
136
+ {
137
+ 'id' => 'd3ed40bd7c',
138
+ 'name' => 'Foo'
139
+ }
140
+ end
141
141
 
142
- before do
143
- stub_request(:post, 'https://us2.api.mailchimp.com/3.0/lists')
144
- .to_return(status: 201, body: result.to_json, headers: { 'Content-Type' => 'application/json; charset=utf-8' })
145
- @result = subject.post(resource)
142
+ before do
143
+ stub_request(:post, 'https://us2.api.mailchimp.com/3.0/lists')
144
+ .to_return(status: 201, body: result.to_json, headers: { 'Content-Type' => 'application/json; charset=utf-8' })
145
+ @result = subject.post(resource)
146
+ end
147
+
148
+ it 'returns the result of making a POST request to the endpoint' do
149
+ expect(@result).to eq(result)
150
+ end
146
151
  end
147
152
 
148
- it 'returns the result of making a POST request to the endpoint' do
149
- expect(@result).to eq(result)
153
+ context 'when the response is not valid JSON' do
154
+ let(:result) { 'bad' }
155
+
156
+ before do
157
+ stub_request(:post, 'https://us2.api.mailchimp.com/3.0/lists')
158
+ .to_return(status: 200, body: result, headers: { 'Content-Type' => 'application/json; charset=utf-8' })
159
+ end
160
+
161
+ it 'raises an error' do
162
+ expect { subject.post(resource) }.to raise_error do |error|
163
+ expect(error).to be_a(MailChimp3::Errors::ServerError)
164
+ expect(error.status).to eq(200)
165
+ expect(error.message).to eq('bad')
166
+ expect(error.details).to be_nil
167
+ end
168
+ end
150
169
  end
151
170
  end
152
171
 
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.1.0
4
+ version: 1.2.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-07-06 00:00:00.000000000 Z
11
+ date: 2015-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.9.1
27
- - !ruby/object:Gem::Dependency
28
- name: faraday_middleware
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: 0.9.1
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: 0.9.1
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: excon
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -147,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
133
  version: '0'
148
134
  requirements: []
149
135
  rubyforge_project:
150
- rubygems_version: 2.4.6
136
+ rubygems_version: 2.4.5
151
137
  signing_key:
152
138
  specification_version: 4
153
139
  summary: wrapper for MailChimp's 3.0 API
@@ -156,3 +142,4 @@ test_files:
156
142
  - spec/mailchimp3/oauth_spec.rb
157
143
  - spec/mailchimp3_spec.rb
158
144
  - spec/spec_helper.rb
145
+ has_rdoc: