From 99a1dd2a478fd9c2d095b7f0ee15b33c4dfcbd18 Mon Sep 17 00:00:00 2001 From: Di Wu Date: Fri, 7 Dec 2018 16:53:22 -0500 Subject: [PATCH 1/2] add updateContacts endpoint --- lib/bronto/contact.rb | 23 +++++++++++++++++++++-- test/contact_test.rb | 10 ++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/bronto/contact.rb b/lib/bronto/contact.rb index 2f7cf51..7538f92 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) @@ -66,9 +85,9 @@ def save def to_hash if id.present? - { id: id, email: email, fields: fields.values.map(&:to_hash) } + { id: id, email: email, fields: fields.values.map(&:to_hash), status: status } else - { email: email, fields: fields.values.map(&:to_hash) } + { email: email, fields: fields.values.map(&:to_hash), status: status } end end 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 From 37cb75624db98064816d78f3d0ba07dd1e683455 Mon Sep 17 00:00:00 2001 From: Di Wu Date: Mon, 10 Dec 2018 13:44:28 -0500 Subject: [PATCH 2/2] update to_hash --- lib/bronto/contact.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/bronto/contact.rb b/lib/bronto/contact.rb index 7538f92..0101bc6 100644 --- a/lib/bronto/contact.rb +++ b/lib/bronto/contact.rb @@ -84,11 +84,10 @@ def save end def to_hash - if id.present? - { id: id, email: email, fields: fields.values.map(&:to_hash), status: status } - else - { email: email, fields: fields.values.map(&:to_hash), status: status } - 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)