diff --git a/lib/mkto_rest.rb b/lib/mkto_rest.rb index 2f8e1eb..6ddf108 100644 --- a/lib/mkto_rest.rb +++ b/lib/mkto_rest.rb @@ -94,7 +94,7 @@ def create_leads(leads, action = 'createOnly', partition = nil) def upsert_leads(leads, lookupField = :email, action = 'createOrUpdate') post( action: action, - lookupField: sym.to_s, + lookupField: lookupField.to_s, input: leads ) end diff --git a/spec/mkto_rest_helper.rb b/spec/mkto_rest_helper.rb index 422e838..a11bdfa 100644 --- a/spec/mkto_rest_helper.rb +++ b/spec/mkto_rest_helper.rb @@ -25,10 +25,15 @@ def set_create_leads_stub_request(leads, hostname, token, options = nil) } url = "https://#{hostname}/rest/v1/leads.json" # taken from dev.marekto.com + preferred_action = options && options.include?(:action) ? options[:action] : 'createOnly' req_body = { - 'action' => 'createOnly', - 'input' => [] + 'action' => preferred_action } + + req_body['lookupField'] = 'email' if options && options.include?(:upsert) + + req_body['input'] = [] + if options req_body['partitionName'] = options[:partition] if options[:partition] req_body['lookupField'] = options[:lookupField] if options[:lookupField] diff --git a/spec/mkto_rest_spec.rb b/spec/mkto_rest_spec.rb index 0a91155..4307eb3 100644 --- a/spec/mkto_rest_spec.rb +++ b/spec/mkto_rest_spec.rb @@ -84,6 +84,16 @@ end let(:partition) { 'bizdev' } + + describe '#upsert_leads' do + let(:updated_leads) { authenticated_client.upsert_leads(sample_leads) } + + it 'inserts or updates the given leads' do + set_create_leads_stub_request(sample_leads, hostname, token, action: 'createOrUpdate', upsert: true) + updated_leads + end + end + it 'can be updated by id' do set_update_lead_stub_request(:id, 1, { 'someFieldX' => 'new_value' }, hostname, token) lead1.update({ 'someFieldX' => 'new_value' }, :id)