Skip to content

Commit

Permalink
Merge branch 'markadrianagbuya-sync_multiple_leads'
Browse files Browse the repository at this point in the history
  • Loading branch information
David Santoso committed Apr 13, 2015
2 parents 95e94dc + 1754989 commit f88a9e6
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/markety/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Markety
class Client
include Markety::Command::GetLead
include Markety::Command::SyncLead
include Markety::Command::SyncMultipleLeads
include Markety::Command::GetCustomObject
include Markety::Command::SyncCustomObject
include Markety::Command::ListOperation
Expand Down
1 change: 1 addition & 0 deletions lib/markety/command.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'markety/command/get_lead'
require 'markety/command/sync_lead'
require 'markety/command/sync_multiple_leads'
require 'markety/command/get_custom_object'
require 'markety/command/sync_custom_object'
require 'markety/command/list_operation'
Expand Down
22 changes: 22 additions & 0 deletions lib/markety/command/sync_multiple_leads.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Markety
module Command
module SyncMultipleLeads

def sync_multiple_leads(leads, dedup_enabled=true)
send_request(:sync_multiple_leads, sync_lead_request_hash(leads, dedup_enabled))
end

private

def sync_lead_request_hash(leads, dedup_enabled)
{
"leadRecordList" => {
"leadRecord" => leads.map(&:synchronisation_hash),
},
"dedupEnabled" => dedup_enabled
}
end

end
end
end
20 changes: 16 additions & 4 deletions lib/markety/lead.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ def ==(other)
# hydrates an instance from a savon hash returned from the marketo API
def self.from_hash(savon_hash)
lead = Lead.new(email: savon_hash[:email], idnum:savon_hash[:id].to_i)

unless savon_hash[:lead_attribute_list].nil?
if savon_hash[:lead_attribute_list][:attribute].kind_of? Hash
attributes = [savon_hash[:lead_attribute_list][:attribute]]
else
attributes = savon_hash[:lead_attribute_list][:attribute]
end

attributes.each do |attribute|
lead.set_attribute(attribute[:attr_name], attribute[:attr_value], attribute[:attr_type])
end
end

lead
end

Expand All @@ -56,9 +56,21 @@ def get_attribute_type(name)
@types[name]
end

def synchronisation_hash
keys_hash.merge({"leadAttributeList" => {"attribute" => attributes_soap_array}})
end

private
def attributes_soap_array()

def keys_hash
keys_hash = {}
keys_hash.merge!({"id" => idnum}) unless idnum.nil?
keys_hash.merge!({"foreignSysPersonId" => foreign_sys_person_id}) unless foreign_sys_person_id.nil?
keys_hash.merge!({"Email" => email}) unless email.nil?
keys_hash
end

def attributes_soap_array
arr = []
@attributes.each_pair do |name,value|
arr << {attr_name: name, attr_type: self.get_attribute_type(name), attr_value: value }
Expand Down
2 changes: 2 additions & 0 deletions lib/markety/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'markety/response/get_lead_response'
require 'markety/response/get_custom_object_response'
require 'markety/response/sync_lead_response'
require 'markety/response/lead_response'
require 'markety/response/sync_multiple_leads_response'
require 'markety/response/sync_custom_object_response'
require 'markety/response/list_operation_response'

Expand Down
25 changes: 25 additions & 0 deletions lib/markety/response/lead_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Markety
module Response
class LeadResponse

attr_accessor :status, :error_message, :lead_id

def initialize(response)
self.status = response[:status]
self.error_message = response[:error]
self.lead_id = response[:lead_id]
end

def success?
!failed?
end

private

def failed?
status == "FAILED"
end

end
end
end
3 changes: 3 additions & 0 deletions lib/markety/response/response_factory.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'markety/response/generic_response'
require 'markety/response/get_lead_response'
require 'markety/response/sync_lead_response'
require 'markety/response/sync_multiple_leads_response'
require 'markety/response/list_operation_response'

module Markety
Expand All @@ -15,6 +16,8 @@ def self.create_response(cmd_type,savon_response)
GetLeadResponse.new(savon_response)
when :sync_lead
SyncLeadResponse.new(savon_response)
when :sync_multiple_leads
SyncMultipleLeadsResponse.new(savon_response)
when :list_operation
ListOperationResponse.new(savon_response)
when :get_custom_objects
Expand Down
21 changes: 21 additions & 0 deletions lib/markety/response/sync_multiple_leads_response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Markety
module Response
# Response class for SyncLead commands
class SyncMultipleLeadsResponse < GenericResponse

def initialize(response)
super(:sync_multiple_leads_response, response)
end

def lead_responses
response_hashes.map do |response_hash|
LeadResponse.new(response_hash)
end
end

def response_hashes
[to_hash.fetch(:success_sync_multiple_leads, {}).fetch(:result, {}).fetch(:sync_status_list, {}).fetch(:sync_status, {})].flatten
end
end
end
end
2 changes: 1 addition & 1 deletion lib/markety/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Markety
VERSION = "2.2.1"
VERSION = "2.3.1"
end
16 changes: 16 additions & 0 deletions spec/fixtures/leads/sync_multiple_leads/single_response.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://www.marketo.com/mktows/">
<SOAP-ENV:Body>
<ns1:successSyncMultipleLeads>
<result>
<syncStatusList>
<syncStatus>
<leadId>1090240</leadId>
<status>UPDATED</status>
<error xsi:nil="true" />
</syncStatus>
</syncStatusList>
</result>
</ns1:successSyncMultipleLeads>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
21 changes: 21 additions & 0 deletions spec/fixtures/leads/sync_multiple_leads/successful_response.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="http://www.marketo.com/mktows/">
<SOAP-ENV:Body>
<ns1:successSyncMultipleLeads>
<result>
<syncStatusList>
<syncStatus>
<leadId>1090240</leadId>
<status>UPDATED</status>
<error xsi:nil="true" />
</syncStatus>
<syncStatus>
<leadId>1090239</leadId>
<status>UPDATED</status>
<error xsi:nil="true" />
</syncStatus>
</syncStatusList>
</result>
</ns1:successSyncMultipleLeads>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
20 changes: 20 additions & 0 deletions spec/markety/command/sync_multiple_leads_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'spec_helper'

module Markety
module Command
describe SyncMultipleLeads do

let(:client){ double("client", send_request: nil) }

describe '#sync_multiple_leads' do
it "calls send_request on the client with the correct params" do
lead = double(Lead, synchronisation_hash: {"lead" => "hash1"})
lead2 = double(Lead, synchronisation_hash: {"lead" => "hash2"})
client.extend(SyncMultipleLeads)
client.sync_multiple_leads([lead, lead2])
expect(client).to have_received(:send_request).with(:sync_multiple_leads, {"leadRecordList" => {"leadRecord" => [{"lead" => "hash1"}, {"lead" => "hash2"}] }, "dedupEnabled" => true})
end
end
end
end
end
48 changes: 38 additions & 10 deletions spec/markety/lead_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
module Markety
EMAIL = '[email protected]'
IDNUM = 93480938


describe Lead do
it "should store the idnum" do
lead_record = Lead.new(email:EMAIL, idnum:IDNUM)
Expand Down Expand Up @@ -54,32 +54,32 @@ module Markety
:foreign_sys_type => nil,
:lead_attribute_list => nil
}

actual = Lead.from_hash(savon_hash)
expected = Lead.new(email:EMAIL, idnum:IDNUM)

actual.should == expected
end

# When there is only one attribute, Marketo returns a Hash, not an Array
it 'should be instantiable from savon hash with only one attribute' do
savon_hash = {
:id => IDNUM,
:email => EMAIL,
:foreign_sys_person_id => nil,
:foreign_sys_type => nil,
:lead_attribute_list =>
:lead_attribute_list =>
{:attribute => { :attr_name => 'FirstName', :attr_value => 'Yaya', :attr_type => 'string'}}
}

actual = Lead.from_hash(savon_hash)

expected = Lead.new(email:EMAIL, idnum:IDNUM)
expected.set_attribute('FirstName', 'Yaya')

actual.should == expected
end

it "should be instantiable from a savon hash" do
savon_hash = {
:email => EMAIL,
Expand All @@ -105,5 +105,33 @@ module Markety
actual.should == expected
end

describe "synchronisation_hash" do
it "returns a correctly formatted hash for synchronisation" do
lead = Lead.new(email: "[email protected]", idnum: 123123)
lead.set_attribute('Company', 'Yaya')
lead.set_attribute('FirstName', 'James')

expect(lead.synchronisation_hash).to eq(
{
"id" => 123123,
"Email" => "[email protected]",
"leadAttributeList" => {
"attribute" => [
{
:attr_name => "Company",
:attr_value => "Yaya",
:attr_type => "string",
},
{
:attr_name => "FirstName",
:attr_value => "James",
:attr_type => "string",
}
]
}
})
end
end

end
end
51 changes: 51 additions & 0 deletions spec/markety/response/lead_response_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require 'spec_helper'

module Markety
module Response
describe LeadResponse do

describe "#success?" do
context "with a successful created status" do
it "returns true" do
response = LeadResponse.new({:lead_id=>"30609", :status=>"CREATED", :error=>nil})
expect(response.success?).to eq true
end
end
context "with a successful updated status" do
it "returns true" do
response = LeadResponse.new({:lead_id=>"30660", :status=>"UPDATED", :error=>nil})
expect(response.success?).to eq true
end
end
context "with a failed response" do
it "returns false" do
response = LeadResponse.new({:lead_id=>"60960", :status=>"FAILED", :error=>"some error"})
expect(response.success?).to eq false
end
end
end

describe "#error_message" do
it "returns the error" do
response = LeadResponse.new({:lead_id=>"60960", :status=>"FAILED", :error=>"Lead Not Found"})
expect(response.error_message).to eq "Lead Not Found"
end
end

describe "#status" do
it "returns the status" do
response = LeadResponse.new({:lead_id=>"60960", :status=>"FAILED", :error=>"Lead Not Found"})
expect(response.status).to eq "FAILED"
end
end

describe "#lead_id" do
it "returns the lead_id" do
response = LeadResponse.new({:lead_id=>"60912360", :status=>"FAILED", :error=>"Lead Not Found"})
expect(response.lead_id).to eq "60912360"
end
end

end
end
end
33 changes: 33 additions & 0 deletions spec/markety/response/sync_multiple_leads_response_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'spec_helper'

module Markety
module Response
describe SyncMultipleLeadsResponse do
let (:successful_response) {SavonHelper.create_response(File.read(File.expand_path("../../../fixtures/leads/sync_multiple_leads/successful_response.xml", __FILE__)))}
let (:single_response) {SavonHelper.create_response(File.read(File.expand_path("../../../fixtures/leads/sync_multiple_leads/single_response.xml", __FILE__)))}

describe "#lead_responses" do
context "with multiple updates" do
it "returns an array of hashes containing the lead responses" do
response = SyncMultipleLeadsResponse.new(successful_response)
first_lead_response, second_lead_response = response.lead_responses[0], response.lead_responses[1]
expect(first_lead_response.success?).to eq true
expect(first_lead_response.lead_id).to eq "1090240"
expect(second_lead_response.success?).to eq true
expect(second_lead_response.lead_id).to eq "1090239"
end
end
context "with a single update" do
it "returns an array of hashes containing the lead response" do
response = SyncMultipleLeadsResponse.new(single_response)
lead_response = response.lead_responses.first
expect(response.lead_responses.size).to eq 1
expect(lead_response.lead_id).to eq "1090240"
expect(lead_response.success?).to eq true
end
end
end

end
end
end

0 comments on commit f88a9e6

Please sign in to comment.