gibbon 0.4.2 → 0.4.3

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.

data/README.markdown CHANGED
@@ -109,6 +109,11 @@ or
109
109
 
110
110
  email_stats = gb.campaignEmailStatsAIM({:cid => campaign_id, :email_address => email_array})
111
111
 
112
+ 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/)):
113
+
114
+ Gibbon.api_endpoint = "https://us1.api.mailchimp.com"
115
+ Gibbon.api_key = your_access_token_or_api_key
116
+
112
117
  ### Setting timeouts
113
118
 
114
119
  Gibbon defaults to a 30 second timeout. You can optionally set your own timeout (in seconds) like so:
@@ -159,9 +164,11 @@ The following people have contributed to Gibbon's development in some way:
159
164
  * [Mike Skalnik](https://github.com/skalnik)
160
165
  * [Kristopher Murata](https://github.com/krsmurata)
161
166
  * [Michael Klishin](https://github.com/michaelklishin)
167
+ * [Adam Loving](https://github.com/adamloving)
168
+ * [Kelly Dunn](https://github.com/kellydunn)
162
169
  * Rails for camelize gsub
163
170
 
164
171
  ##Copyright
165
172
 
166
- * Copyright (c) 2010-2012 Amro Mousa. See LICENSE.txt for details.
167
- * MailChimp (c) 2001-2012 The Rocket Science Group.
173
+ * Copyright (c) 2010-2013 Amro Mousa. See LICENSE.txt for details.
174
+ * MailChimp (c) 2001-2013 The Rocket Science Group.
data/gibbon.gemspec CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "gibbon"
6
- s.version = "0.4.2"
6
+ s.version = "0.4.3"
7
7
  s.authors = ["Amro Mousa"]
8
8
  s.email = ["amromousa@gmail.com"]
9
9
  s.homepage = "http://github.com/amro/gibbon"
@@ -23,7 +23,7 @@ Gem::Specification.new do |s|
23
23
  s.require_paths = ["lib"]
24
24
 
25
25
  s.add_dependency('httparty')
26
- s.add_dependency('json')
26
+ s.add_dependency('multi_json')
27
27
 
28
28
  s.add_development_dependency('rake')
29
29
  s.add_development_dependency('debugger')
data/lib/gibbon.rb CHANGED
@@ -7,7 +7,7 @@ class Gibbon
7
7
  format :plain
8
8
  default_timeout 30
9
9
 
10
- attr_accessor :api_key, :timeout, :throws_exceptions
10
+ attr_accessor :api_key, :api_endpoint, :timeout, :throws_exceptions
11
11
 
12
12
  MailChimpError = Class.new(StandardError)
13
13
 
@@ -15,6 +15,7 @@ class Gibbon
15
15
  @api_key = api_key || self.class.api_key || ENV['MAILCHIMP_API_KEY']
16
16
  @api_key = @api_key.strip if @api_key
17
17
 
18
+ @api_endpoint = default_parameters.delete(:api_endpoint)
18
19
  @timeout = default_parameters.delete(:timeout)
19
20
  @throws_exceptions = default_parameters.delete(:throws_exceptions)
20
21
 
@@ -42,7 +43,7 @@ class Gibbon
42
43
  # MailChimp API sometimes returns JSON fragments (e.g. true from listSubscribe)
43
44
  # so we parse after adding brackets to create a JSON array so
44
45
  # JSON.parse succeeds in those cases.
45
- parsed_response = JSON.parse('[' + response.body + ']').first
46
+ parsed_response = JSON.parse("[#{response.body}]").first
46
47
 
47
48
  if should_raise_for_response?(parsed_response)
48
49
  raise MailChimpError.new("MailChimp API Error: #{parsed_response["error"]} (code #{parsed_response["code"]})")
@@ -67,7 +68,7 @@ class Gibbon
67
68
 
68
69
  def set_instance_defaults
69
70
  @timeout = (self.class.timeout || 30) if @timeout.nil?
70
-
71
+ @api_endpoint = self.class.api_endpoint if @api_endpoint.nil?
71
72
  # Two lines because the class variable could be false and (false || true) is always true
72
73
  @throws_exceptions = self.class.throws_exceptions if @throws_exceptions.nil?
73
74
  @throws_exceptions = true if @throws_exceptions.nil?
@@ -78,14 +79,18 @@ class Gibbon
78
79
  end
79
80
 
80
81
  def base_api_url
81
- "https://#{get_data_center_from_api_key}api.mailchimp.com/1.3/?method="
82
+ "#{@api_endpoint || get_api_endpoint}/1.3/?method="
83
+ end
84
+
85
+ def get_api_endpoint
86
+ "https://#{get_data_center_from_api_key}api.mailchimp.com"
82
87
  end
83
88
 
84
89
  class << self
85
- attr_accessor :api_key, :timeout, :throws_exceptions
90
+ attr_accessor :api_key, :timeout, :throws_exceptions, :api_endpoint
86
91
 
87
92
  def method_missing(sym, *args, &block)
88
- new(self.api_key, {timeout: self.timeout, throws_exceptions: self.throws_exceptions}).send(sym, *args, &block)
93
+ new(self.api_key, {api_endpoint: self.api_endpoint, timeout: self.timeout, throws_exceptions: self.throws_exceptions}).send(sym, *args, &block)
89
94
  end
90
95
  end
91
96
 
@@ -129,4 +134,4 @@ class GibbonExport < Gibbon
129
134
 
130
135
  lines
131
136
  end
132
- end
137
+ end
data/test/test_gibbon.rb CHANGED
@@ -39,6 +39,12 @@ class TestGibbon < Test::Unit::TestCase
39
39
  @gibbon.timeout = timeout
40
40
  assert_equal(timeout, @gibbon.timeout)
41
41
  end
42
+
43
+ should "detect api endpoint from initializer parameters" do
44
+ api_endpoint = 'https://us6.api.mailchimp.com'
45
+ @gibbon = Gibbon.new(@api_key, :api_endpoint => api_endpoint)
46
+ assert_equal api_endpoint, @gibbon.api_endpoint
47
+ end
42
48
  end
43
49
 
44
50
  context "build api url" do
@@ -71,6 +77,15 @@ class TestGibbon < Test::Unit::TestCase
71
77
  expect_post("https://us1.api.mailchimp.com/1.3/?method=sayHello", {"apikey" => @api_key})
72
78
  @gibbon.say_hello
73
79
  end
80
+
81
+ # when the end user has signed in via oauth, api_key and endpoint should be supplied separately
82
+ should "not require datacenter in api key" do
83
+ @api_key = "TESTKEY"
84
+ @gibbon.api_key = @api_key
85
+ @gibbon.api_endpoint = "https://us6.api.mailchimp.com"
86
+ expect_post("https://us6.api.mailchimp.com/1.3/?method=sayHello", {"apikey" => @api_key})
87
+ @gibbon.say_hello
88
+ end
74
89
  end
75
90
 
76
91
  context "Gibbon class variables" do
@@ -78,12 +93,14 @@ class TestGibbon < Test::Unit::TestCase
78
93
  Gibbon.api_key = "123-us1"
79
94
  Gibbon.timeout = 15
80
95
  Gibbon.throws_exceptions = false
96
+ Gibbon.api_endpoint = 'https://us6.api.mailchimp.com'
81
97
  end
82
98
 
83
99
  teardown do
84
100
  Gibbon.api_key = nil
85
101
  Gibbon.timeout = nil
86
102
  Gibbon.throws_exceptions = nil
103
+ Gibbon.api_endpoint = nil
87
104
  end
88
105
 
89
106
  should "set api key on new instances" do
@@ -97,6 +114,11 @@ class TestGibbon < Test::Unit::TestCase
97
114
  should "set throws_exceptions on new instances" do
98
115
  assert_equal(Gibbon.new.throws_exceptions, Gibbon.throws_exceptions)
99
116
  end
117
+
118
+ should "set api_endpoint on new instances" do
119
+ assert Gibbon.api_endpoint
120
+ assert_equal(Gibbon.new.api_endpoint, Gibbon.api_endpoint)
121
+ end
100
122
  end
101
123
 
102
124
  context "build api body" do
@@ -163,6 +185,11 @@ class TestGibbon < Test::Unit::TestCase
163
185
  @gibbon.say_hello
164
186
  end
165
187
  end
188
+
189
+ should "not raise exception if the api returns no response body" do
190
+ Gibbon.stubs(:post).returns(Struct.new(:body).new(nil))
191
+ assert_nil @gibbon.say_hello
192
+ end
166
193
  end
167
194
 
168
195
  context "export API" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gibbon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-07 00:00:00.000000000 Z
12
+ date: 2013-02-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
@@ -28,7 +28,7 @@ dependencies:
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
30
  - !ruby/object:Gem::Dependency
31
- name: json
31
+ name: multi_json
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
@@ -143,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
143
  version: '0'
144
144
  segments:
145
145
  - 0
146
- hash: -3179845678375072173
146
+ hash: 2873655008867381541
147
147
  required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  none: false
149
149
  requirements:
@@ -152,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
152
  version: '0'
153
153
  segments:
154
154
  - 0
155
- hash: -3179845678375072173
155
+ hash: 2873655008867381541
156
156
  requirements: []
157
157
  rubyforge_project: gibbon
158
158
  rubygems_version: 1.8.24