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
32 changes: 21 additions & 11 deletions lib/mrkt/concerns/crud_leads.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Mrkt
module CrudLeads
MAX_LEADS_SIZE = 300

def get_leads(filter_type, filter_values, fields: nil, batch_size: nil, next_page_token: nil)
params = {
filterType: filter_type,
Expand All @@ -13,22 +15,30 @@ def get_leads(filter_type, filter_values, fields: nil, batch_size: nil, next_pag
end

def createupdate_leads(leads, action: 'createOrUpdate', lookup_field: nil, partition_name: nil, async_processing: nil)
post('/rest/v1/leads.json') do |req|
params = {
action: action,
input: leads
}
params[:lookupField] = lookup_field if lookup_field
params[:partitionName] = partition_name if partition_name
params[:asyncProcessing] = async_processing if async_processing
# see also: http://developers.marketo.com/documentation/rest/createupdate-leads/
# > You can update a maximum of 300 leads with a single API call.
leads.each_slice(MAX_LEADS_SIZE).map do |_leads|
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you should flatten the output to not break the API with a new response type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the output to not break the API

Agree.
But when leads is over 300, we call multiple requests to marketo and receive multiple responses.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But...I begin to thought that this method should be keep it simple.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point that's why I wrote that you should flatten the response, with #flat_map.

post('/rest/v1/leads.json') do |req|
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method became a bit too complex, you should extract the inner block of the map to a separate method.

params = {
action: action,
input: _leads
}
params[:lookupField] = lookup_field if lookup_field
params[:partitionName] = partition_name if partition_name
params[:asyncProcessing] = async_processing if async_processing

json_payload(req, params)
json_payload(req, params)
end
end
end

def delete_leads(leads)
delete('/rest/v1/leads.json') do |req|
json_payload(req, input: map_lead_ids(leads))
# see also: http://developers.marketo.com/documentation/rest/delete-lead/
# > The max batch size for this endpoint is 300 leads.
leads.each_slice(MAX_LEADS_SIZE).map do |_leads|
delete('/rest/v1/leads.json') do |req|
json_payload(req, input: map_lead_ids(_leads))
end
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/concerns/crud_leads_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
.to_return(json_stub(response_stub))
end

it { is_expected.to eq(response_stub) }
it { is_expected.to eq([response_stub]) }
end

describe '#delete_leads' do
Expand Down Expand Up @@ -101,7 +101,7 @@
.to_return(json_stub(response_stub))
end

it { is_expected.to eq(response_stub) }
it { is_expected.to eq([response_stub]) }
end

describe '#associate_lead' do
Expand Down