diff --git a/lib/bronto/contact.rb b/lib/bronto/contact.rb index 2f7cf51..0101bc6 100644 --- a/lib/bronto/contact.rb +++ b/lib/bronto/contact.rb @@ -37,6 +37,25 @@ def self.save(*objs) objs end + def self.update(*objs) + objs = objs.flatten + api_key = objs.first.is_a?(String) ? objs.shift : self.api_key + + resp = request(:update, {plural_class_name => objs.map(&:to_hash)}) + + objs.each { |o| o.errors.clear } + + Array.wrap(resp[:return][:results]).each_with_index do |result, i| + if result[:is_error] + objs[i].errors.add(result[:error_code], result[:error_string]) + else + objs[i].id = result[:id] + end + end + + objs + end + def initialize(options = {}) self.fields = {} fields = options.delete(:fields) @@ -65,11 +84,10 @@ def save end def to_hash - if id.present? - { id: id, email: email, fields: fields.values.map(&:to_hash) } - else - { email: email, fields: fields.values.map(&:to_hash) } - end + obj = { email: email, fields: fields.values.map(&:to_hash) } + obj.merge!(id: id) if id.present? + obj.merge!(status: status) if status.present? + obj end def set_field(field, value) diff --git a/test/contact_test.rb b/test/contact_test.rb index f66963d..e2bf03f 100644 --- a/test/contact_test.rb +++ b/test/contact_test.rb @@ -19,6 +19,16 @@ class ContactTest < Test::Unit::TestCase assert_equal 0, @contact.errors.count end + should "update contact status" do + assert_equal nil, @contact.id + + @contact.status = 'unsub' + @contact.update + + assert_not_nil @contact.id + assert_equal 0, @contact.errors.count + end + should "destroy a contact" do @contact.save