mailchimp 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,5 +14,4 @@
14
14
  * [Dave Worth](https://github.com/daveworth)
15
15
  * [Mike Skalnik](https://github.com/skalnik)
16
16
  * [Kristopher Murata](https://github.com/krsmurata)
17
- * [Michael Klishin](https://github.com/michaelklishin)
18
- * [Ben Simpson](https://github.com/bsimpson)
17
+ * [Michael Klishin](https://github.com/michaelklishin)
@@ -63,9 +63,15 @@ or
63
63
  ### Export API usage
64
64
 
65
65
  In addition to the standard API you can make calls to the
66
- [MailChimp Export API](http://apidocs.mailchimp.com/export/1.0/) using a Mailchimp::Export object:
67
-
68
- mailchimp_export = Mailchimp::Export.new(@api_key)
66
+ [MailChimp Export API](http://apidocs.mailchimp.com/export/1.0/) using a APIExport object. Given an existing
67
+ Mailchimp::API object you can request a new Mailchimp::APIExporter object:
68
+
69
+ api = Mailchimp::API.new(@api_key)
70
+ mailchimp_export = api.get_exporter
71
+
72
+ or you can construct a new object directly:
73
+
74
+ mailchimp_export = Mailchimp::APIExport.new(@api_key)
69
75
 
70
76
  Calling Export API functions is identical to making standard API calls but the
71
77
  return value is an Enumerator which loops over the lines returned from the
@@ -10,6 +10,7 @@ require "mailchimp/api"
10
10
  require "mailchimp/sts"
11
11
  require "mailchimp/mandrill"
12
12
  require 'mailchimp/export'
13
+ require "mailchimp/api_error"
13
14
 
14
15
  module Mailchimp
15
16
  end
@@ -40,7 +40,7 @@ module Mailchimp
40
40
  end
41
41
 
42
42
  if @throws_exceptions && response.is_a?(Hash) && response["error"]
43
- raise "Error from MailChimp API: #{response["error"]} (code #{response["code"]})"
43
+ raise Mailchimp::APIError.new(response['error'],response['code'])
44
44
  end
45
45
 
46
46
  response
@@ -0,0 +1,10 @@
1
+ module Mailchimp
2
+ class APIError < RuntimeError
3
+ attr_accessor :error, :code
4
+ def initialize(error,code)
5
+ super("Error from MailChimp API: #{error} (code #{code})")
6
+ @error = error
7
+ @code = code
8
+ end
9
+ end
10
+ end
@@ -6,12 +6,13 @@ module Mailchimp
6
6
  include HTTParty
7
7
  default_timeout 30
8
8
 
9
- attr_accessor :api_key, :timeout, :options
9
+ attr_accessor :api_key, :timeout, :options, :throws_exceptions
10
10
 
11
11
  def initialize(api_key = nil, default_parameters = {})
12
12
  @api_key = api_key || ENV['MAILCHIMP_API_KEY'] || nil
13
13
  @timeout = default_parameters.delete(:timeout)
14
14
  @default_params = default_parameters
15
+ @throws_exceptions = false
15
16
  end
16
17
 
17
18
  def dc_from_api_key
@@ -39,7 +40,7 @@ module Mailchimp
39
40
  response = self.class.post(url, :body => params, :timeout => timeout)
40
41
  begin; response = JSON.parse(response.body); rescue; response = response.body ;end
41
42
  if @throws_exceptions && response.is_a?(Hash) && response["error"]
42
- raise "Error from MailChimp API: #{response["error"]} (code #{response["code"]})"
43
+ raise Mailchimp::APIError response["error"], response["code"]
43
44
  end
44
45
  response
45
46
  end
@@ -1,8 +1,5 @@
1
1
  module Mailchimp
2
2
  class Export < Base
3
-
4
- format :plain
5
-
6
3
  def initialize(api_key = nil, default_parameters = {})
7
4
  super(api_key, {:apikey => api_key}.merge(default_parameters))
8
5
  end
@@ -20,7 +17,7 @@ module Mailchimp
20
17
  lines = response.body.lines
21
18
  if @throws_exceptions
22
19
  first_line_object = JSON.parse(lines.first) if lines.first
23
- raise "Error from MailChimp Export API: #{first_line_object["error"]} (code #{first_line_object["code"]})" if first_line_object.is_a?(Hash) && first_line_object["error"]
20
+ raise Mailchimp::APIError.new(first_line_object["error"],first_line_object["code"]) if first_line_object.is_a?(Hash) && first_line_object["error"]
24
21
  end
25
22
 
26
23
  lines
@@ -1,4 +1,4 @@
1
- class MailchimpPsuedoJSONParser < HTTParty::Parser
1
+ class MailchimpPseudoJSONParser < HTTParty::Parser
2
2
  # Unfornately Mailchimp's API returns invalid JSON for some valid requests,
3
3
  # specifically mandrill's users/ping. We need to just pass that through instead
4
4
  # of blowing up inside of HTTParty so that we can do something with it later.
@@ -4,7 +4,7 @@ end
4
4
 
5
5
  module Mailchimp
6
6
  class Mandrill < Base
7
- parser MailchimpPsuedoJSONParser
7
+ parser MailchimpPseudoJSONParser
8
8
 
9
9
  def initialize(api_key = nil, default_parameters = {})
10
10
  super(api_key, {
@@ -1,3 +1,3 @@
1
1
  module Mailchimp
2
- VERSION = "0.0.8"
2
+ VERSION = "0.0.9"
3
3
  end
@@ -5,8 +5,8 @@ require "mailchimp/version"
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "mailchimp"
7
7
  s.version = Mailchimp::VERSION
8
- s.authors = ["Chris Kelly"]
9
- s.email = ["chris@highgroove.com"]
8
+ s.authors = ["Chris Kelly", "Dave Worth"]
9
+ s.email = ["dave@bignerdranch.com"]
10
10
  s.homepage = "https://github.com/mailchimp/mailchimp-gem"
11
11
  s.summary = %q{Mailchimp APIs in Ruby}
12
12
  s.description = %q{This provides Ruby access to (eventually) all of Mailchimp's APIs}
@@ -17,9 +17,9 @@ Gem::Specification.new do |s|
17
17
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
-
20
+
21
21
  s.add_dependency('httparty')
22
-
22
+
23
23
  s.add_development_dependency('ruby-debug19')
24
24
  s.add_development_dependency('rake')
25
25
  s.add_development_dependency('shoulda')
@@ -115,22 +115,26 @@ class ApiTest < Test::Unit::TestCase
115
115
  @returns = Struct.new(:body).new(["array", "entries"].to_json)
116
116
  end
117
117
 
118
- should "throw exception if configured to and the API replies with a JSON hash containing a key called 'error'" do
119
- Mailchimp::API.stubs(:post).returns(Struct.new(:body).new({'error' => 'bad things'}.to_json))
118
+ should "not throw exception if the API replies with a JSON hash containing a key called 'error'" do
119
+ Mailchimp::API.stubs(:post).returns(Struct.new(:body).new({'error' => 'bad things', 'code' => '254'}.to_json))
120
120
  assert_nothing_raised do
121
121
  result = @api.say_hello
122
+ assert_equal 'bad things', result['error']
123
+ assert_equal '254', result['code']
122
124
  end
123
-
124
- ap result
125
125
  end
126
126
 
127
127
  should "throw exception if configured to and the API replies with a JSON hash containing a key called 'error'" do
128
128
  @api.throws_exceptions = true
129
- Mailchimp::API.stubs(:post).returns(Struct.new(:body).new({'error' => 'bad things'}.to_json))
130
- assert_raise RuntimeError do
129
+ Mailchimp::API.stubs(:post).returns(Struct.new(:body).new({'error' => 'bad things', 'code' => '254'}.to_json))
130
+ e = assert_raise Mailchimp::APIError do
131
131
  @api.say_hello
132
132
  end
133
+
134
+ assert_equal 'bad things', e.error
135
+ assert_equal '254', e.code
133
136
  end
137
+
134
138
  should 'allow one to check api key validity' do
135
139
  Mailchimp::API.stubs(:post).returns(Struct.new(:body).new(%q{"Everything's Chimpy!"}.to_json))
136
140
  assert_equal true, @api.valid_api_key?
@@ -21,23 +21,24 @@ class ExportTest < Test::Unit::TestCase
21
21
  end
22
22
 
23
23
  should "not throw exception if the Export API replies with a JSON hash containing a key called 'error'" do
24
- Mailchimp::Export.stubs(:post).returns(Struct.new(:body).new({'error' => 'bad things'}.to_json))
24
+ Mailchimp::Export.stubs(:post).returns(Struct.new(:body).new({'error' => 'bad things', 'code' => '123'}.to_json))
25
25
 
26
26
  assert_nothing_raised do
27
27
  @api.say_hello(@body)
28
28
  end
29
29
  end
30
- =begin #pending throw exception decision
30
+
31
31
  should "throw exception if configured to and the Export API replies with a JSON hash containing a key called 'error'" do
32
32
  @api.throws_exceptions = true
33
33
  params = {:body => @body, :timeout => nil}
34
34
  Mailchimp::Export.stubs(:post).returns(Struct.new(:body).new({'error' => 'bad things', 'code' => '123'}.to_json))
35
35
 
36
- assert_raise RuntimeError do
36
+ e = assert_raise Mailchimp::APIError do
37
37
  @api.say_hello(@body)
38
38
  end
39
+ assert_equal 'bad things', e.error
40
+ assert_equal '123', e.code
39
41
  end
40
- =end
41
42
  end
42
43
  private
43
44
 
@@ -49,4 +50,4 @@ class ExportTest < Test::Unit::TestCase
49
50
  end.returns(Struct.new(:body).new("") )
50
51
  end
51
52
 
52
- end
53
+ end
metadata CHANGED
@@ -1,19 +1,20 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailchimp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Chris Kelly
9
+ - Dave Worth
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2013-02-18 00:00:00.000000000 Z
13
+ date: 2013-07-11 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: httparty
16
- requirement: &70361207201980 !ruby/object:Gem::Requirement
17
+ requirement: !ruby/object:Gem::Requirement
17
18
  none: false
18
19
  requirements:
19
20
  - - ! '>='
@@ -21,10 +22,15 @@ dependencies:
21
22
  version: '0'
22
23
  type: :runtime
23
24
  prerelease: false
24
- version_requirements: *70361207201980
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
25
31
  - !ruby/object:Gem::Dependency
26
32
  name: ruby-debug19
27
- requirement: &70361207201240 !ruby/object:Gem::Requirement
33
+ requirement: !ruby/object:Gem::Requirement
28
34
  none: false
29
35
  requirements:
30
36
  - - ! '>='
@@ -32,10 +38,15 @@ dependencies:
32
38
  version: '0'
33
39
  type: :development
34
40
  prerelease: false
35
- version_requirements: *70361207201240
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
36
47
  - !ruby/object:Gem::Dependency
37
48
  name: rake
38
- requirement: &70361207200740 !ruby/object:Gem::Requirement
49
+ requirement: !ruby/object:Gem::Requirement
39
50
  none: false
40
51
  requirements:
41
52
  - - ! '>='
@@ -43,10 +54,15 @@ dependencies:
43
54
  version: '0'
44
55
  type: :development
45
56
  prerelease: false
46
- version_requirements: *70361207200740
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
47
63
  - !ruby/object:Gem::Dependency
48
64
  name: shoulda
49
- requirement: &70361207200240 !ruby/object:Gem::Requirement
65
+ requirement: !ruby/object:Gem::Requirement
50
66
  none: false
51
67
  requirements:
52
68
  - - ! '>='
@@ -54,10 +70,15 @@ dependencies:
54
70
  version: '0'
55
71
  type: :development
56
72
  prerelease: false
57
- version_requirements: *70361207200240
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
58
79
  - !ruby/object:Gem::Dependency
59
80
  name: mocha
60
- requirement: &70361207199720 !ruby/object:Gem::Requirement
81
+ requirement: !ruby/object:Gem::Requirement
61
82
  none: false
62
83
  requirements:
63
84
  - - ! '>='
@@ -65,10 +86,15 @@ dependencies:
65
86
  version: '0'
66
87
  type: :development
67
88
  prerelease: false
68
- version_requirements: *70361207199720
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
69
95
  - !ruby/object:Gem::Dependency
70
96
  name: cover_me
71
- requirement: &70361207199260 !ruby/object:Gem::Requirement
97
+ requirement: !ruby/object:Gem::Requirement
72
98
  none: false
73
99
  requirements:
74
100
  - - ! '>='
@@ -76,10 +102,15 @@ dependencies:
76
102
  version: '0'
77
103
  type: :development
78
104
  prerelease: false
79
- version_requirements: *70361207199260
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
80
111
  - !ruby/object:Gem::Dependency
81
112
  name: fakeweb
82
- requirement: &70361207198760 !ruby/object:Gem::Requirement
113
+ requirement: !ruby/object:Gem::Requirement
83
114
  none: false
84
115
  requirements:
85
116
  - - ! '>='
@@ -87,10 +118,15 @@ dependencies:
87
118
  version: '0'
88
119
  type: :development
89
120
  prerelease: false
90
- version_requirements: *70361207198760
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
91
127
  description: This provides Ruby access to (eventually) all of Mailchimp's APIs
92
128
  email:
93
- - chris@highgroove.com
129
+ - dave@bignerdranch.com
94
130
  executables: []
95
131
  extensions: []
96
132
  extra_rdoc_files: []
@@ -109,6 +145,7 @@ files:
109
145
  - examples/sts_example.rb
110
146
  - lib/mailchimp.rb
111
147
  - lib/mailchimp/api.rb
148
+ - lib/mailchimp/api_error.rb
112
149
  - lib/mailchimp/base.rb
113
150
  - lib/mailchimp/export.rb
114
151
  - lib/mailchimp/ext/httparty.rb
@@ -120,7 +157,6 @@ files:
120
157
  - mailchimp.gemspec
121
158
  - test/integration/api_test.rb
122
159
  - test/integration/base_test.rb
123
- - test/integration/export_test.rb
124
160
  - test/integration/mandrill_test.rb
125
161
  - test/integration/sts_test.rb
126
162
  - test/lib/api_test.rb
@@ -150,14 +186,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
186
  version: '0'
151
187
  requirements: []
152
188
  rubyforge_project: mailchimp
153
- rubygems_version: 1.8.17
189
+ rubygems_version: 1.8.25
154
190
  signing_key:
155
191
  specification_version: 3
156
192
  summary: Mailchimp APIs in Ruby
157
193
  test_files:
158
194
  - test/integration/api_test.rb
159
195
  - test/integration/base_test.rb
160
- - test/integration/export_test.rb
161
196
  - test/integration/mandrill_test.rb
162
197
  - test/integration/sts_test.rb
163
198
  - test/lib/api_test.rb
@@ -1,34 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ExportIntegrationTest < Test::Unit::TestCase
4
-
5
- should "send request List Data from the Mailchimp Export API" do
6
-
7
- FakeWeb.register_uri(
8
- :post,
9
- 'http://us2.api.mailchimp.com/export/1.0/list/',
10
- body: '["Email Address","First Name","Last Name","Salesforce ID","EMAIL_TYPE","MEMBER_RATING","OPTIN_TIME","OPTIN_IP","CONFIRM_TIME","CONFIRM_IP","LATITUDE","LONGITUDE","GMTOFF","DSTOFF","TIMEZONE","CC","REGION","LAST_CHANGED"]
11
- ["highgroove@example.com","Daniel","Rice","00Qd0000005MKaEEAW","html",2,"2011-10-06 20:06:46","173.160.74.121","2011-10-06 20:07:28","173.160.74.121","33.7490000","-84.3880000","-5","-4","America\/Kentucky\/Monticello","US","GA","2012-03-12 17:45:18"]
12
- ["chimpy@example.com","Daniel","Rice","00Qd0000006AjjtEAC","html",2,"",null,"2012-03-08 20:52:31","173.160.74.121",null,null,null,null,null,null,null,"2012-03-12 17:45:20"]
13
- ["wolfbrain@example.com","George P","Burdell","00Qd0000006AjjuEAC","html",2,"",null,"2012-03-08 20:53:10","173.160.74.121",null,null,null,null,null,null,null,"2012-03-12 17:45:22"]'
14
- )
15
-
16
- m = Mailchimp::Export.new('abc123-us2')
17
- response = m.list(:id => 'xxxxxxxx')
18
-
19
- assert_kind_of Enumerator, response
20
-
21
- end
22
-
23
- should "get campaign subscriber activity" do
24
-
25
- FakeWeb.register_uri(
26
- :post,
27
- 'http://us2.api.mailchimp.com/export/1.0/campaignSubscriberActivity/',
28
- body: '{"highgroove@example.com":[{"action":"open","timestamp":"2012-03-12 20:14:17","url":null,"ip":"184.77.22.196"}]} {"wolfbrain@example.com":[{"action":"open","timestamp":"2012-03-12 20:14:20","url":null,"ip":"184.77.22.196"}]}'.to_json
29
- )
30
- response = Mailchimp::Export.new('abc123-us2').campaign_subscriber_activity(:id => 'xxxxxxxx')
31
-
32
- assert_kind_of Enumerator, response
33
- end
34
- end