hominid 2.1.7 → 2.1.9

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.1.7
1
+ 2.1.9
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{hominid}
8
- s.version = "2.1.7"
8
+ s.version = "2.1.9"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brian Getting", "Michael Str\303\274der"]
12
- s.date = %q{2010-08-18}
12
+ s.date = %q{2010-09-07}
13
13
  s.description = %q{Hominid is a Ruby gem that provides a wrapper for interacting with the Mailchimp email marketing service API.}
14
14
  s.email = %q{brian@terra-firma-design.com}
15
15
  s.extra_rdoc_files = [
@@ -1,3 +1,4 @@
1
+ require 'json'
1
2
  require 'xmlrpc/client'
2
3
  require 'ostruct'
3
4
 
@@ -4,9 +4,11 @@ module Hominid
4
4
  include Hominid::Helper
5
5
  include Hominid::List
6
6
  include Hominid::Security
7
-
7
+
8
8
  # MailChimp API Documentation: http://www.mailchimp.com/api/1.2/
9
9
  MAILCHIMP_API_VERSION = "1.2"
10
+ MAILCHIMP_EXPORT_API_VERSION = "1.0"
11
+ MAILCHIMP_EXPORT_PATH = "/export/#{MAILCHIMP_EXPORT_API_VERSION}/list/"
10
12
 
11
13
  def initialize(config = {})
12
14
  raise StandardError.new('Please provide your Mailchimp API key.') unless config[:api_key]
@@ -25,15 +27,17 @@ module Hominid
25
27
  @config = defaults.merge(config).freeze
26
28
  if config[:secure]
27
29
  @chimpApi = XMLRPC::Client.new2("https://#{dc}.api.mailchimp.com/#{MAILCHIMP_API_VERSION}/")
30
+ @exportApi = Net::HTTP.new("#{dc}.api.mailchimp.com", 443)
28
31
  else
29
32
  @chimpApi = XMLRPC::Client.new2("http://#{dc}.api.mailchimp.com/#{MAILCHIMP_API_VERSION}/")
33
+ @exportApi = Net::HTTP.new("#{dc}.api.mailchimp.com", 80)
30
34
  end
31
35
  end
32
-
36
+
33
37
  def apply_defaults_to(options) # :nodoc:
34
38
  @config.merge(options)
35
39
  end
36
-
40
+
37
41
  def call(method, *args) # :nodoc:
38
42
  @chimpApi.call(method, @config[:api_key], *args)
39
43
  rescue XMLRPC::FaultException => error
@@ -61,7 +65,30 @@ module Hominid
61
65
  rescue Exception => error
62
66
  raise CommunicationError.new(error.message)
63
67
  end
64
-
68
+
69
+ def call_export(list_id, status)
70
+ uri = "#{MAILCHIMP_EXPORT_PATH}?apikey=#{@config[:api_key]}&id=#{list_id}"
71
+
72
+ !status.nil? && uri += "&status=#{status}"
73
+
74
+ # Don't parse the whole thing since there could be thousands of records
75
+ out, keys = [], []
76
+ body = @exportApi.request_get(uri).body
77
+ lines = body.send(body.respond_to?(:lines) ? :lines : :to_s).to_a
78
+
79
+ lines.each_with_index do |l, i|
80
+ if i == 0
81
+ keys = JSON.parse(l).map { |k| k.gsub(" ", "_") }
82
+ else
83
+ # Magic! http://snippets.dzone.com/posts/show/302
84
+ out << Hash[*keys.zip(JSON.parse(l)).flatten]
85
+ end
86
+ end
87
+ out
88
+ rescue Exception => error
89
+ raise CommunicationError.new(error.message)
90
+ end
91
+
65
92
  def clean_merge_tags(merge_tags) # :nodoc:
66
93
  return {} unless merge_tags.is_a? Hash
67
94
  merge_tags.each do |key, value|
@@ -72,7 +99,7 @@ module Hominid
72
99
  end
73
100
  end
74
101
  end
75
-
102
+
76
103
  def hash_to_object(object) # :nodoc:
77
104
  return case object
78
105
  when Hash
@@ -90,4 +117,5 @@ module Hominid
90
117
  end
91
118
 
92
119
  end
93
- end
120
+ end
121
+
@@ -287,6 +287,19 @@ module Hominid
287
287
  def members(list_id, status = "subscribed", since = "2000-01-01 00:00:00", start = 0, limit = 100)
288
288
  call("listMembers", list_id, status, since, start, limit)
289
289
  end
290
+
291
+ # Use the Mailchimp Export API to get all list members for a list
292
+ #
293
+ # Parameters:
294
+ # * list_id (String) = The mailing list ID value.
295
+ # * status (String) = One of subscribed, unsubscribed, cleaned, updated. Defaults to subscribed.
296
+ #
297
+ # Returns:
298
+ # An array of user record structs, keyed by the merge field name
299
+ #
300
+ def export(list_id, status=nil)
301
+ call_export(list_id, status)
302
+ end
290
303
 
291
304
  # Get the list of merge tags for a given list, including their name, tag, and required setting.
292
305
  #
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hominid
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
8
  - 1
9
- - 7
10
- version: 2.1.7
9
+ - 9
10
+ version: 2.1.9
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brian Getting
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-08-18 00:00:00 -07:00
19
+ date: 2010-09-07 00:00:00 -07:00
20
20
  default_executable:
21
21
  dependencies: []
22
22