Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions lib/bronto/contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions test/contact_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down