leadsquared 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.rspec +1 -0
- data/.travis.yml +8 -0
- data/Gemfile +18 -0
- data/Gemfile.lock +120 -0
- data/LICENSE.txt +20 -0
- data/README.md +40 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/leadsquared.gemspec +93 -0
- data/lib/leadsquared.rb +15 -0
- data/lib/leadsquared/activity.rb +71 -0
- data/lib/leadsquared/api_connection.rb +35 -0
- data/lib/leadsquared/client.rb +41 -0
- data/lib/leadsquared/config.rb +21 -0
- data/lib/leadsquared/engine.rb +5 -0
- data/lib/leadsquared/invalid_request_error.rb +7 -0
- data/lib/leadsquared/lead.rb +106 -0
- data/spec/leadsquared/activity_spec.rb +147 -0
- data/spec/leadsquared/client_spec.rb +72 -0
- data/spec/leadsquared/lead_spec.rb +324 -0
- data/spec/leadsquared_spec.rb +7 -0
- data/spec/spec_helper.rb +30 -0
- metadata +222 -0
@@ -0,0 +1,35 @@
|
|
1
|
+
module Leadsquared
|
2
|
+
class ApiConnection
|
3
|
+
attr_reader :connection
|
4
|
+
|
5
|
+
def initialize(service)
|
6
|
+
@connection = Leadsquared::Client.new
|
7
|
+
@service = service
|
8
|
+
end
|
9
|
+
|
10
|
+
protected
|
11
|
+
|
12
|
+
def url_with_service(action)
|
13
|
+
@service + action
|
14
|
+
end
|
15
|
+
|
16
|
+
def handle_response(response)
|
17
|
+
case response.status
|
18
|
+
when 200
|
19
|
+
return JSON.parse response.body
|
20
|
+
when 400
|
21
|
+
raise InvalidRequestError.new("Bad Request")
|
22
|
+
when 401
|
23
|
+
raise InvalidRequestError.new("Unauthorized Request")
|
24
|
+
when 404
|
25
|
+
raise InvalidRequestError.new("API Not Found")
|
26
|
+
when 500
|
27
|
+
message = response.body.try(:[], "ExceptionMessage")
|
28
|
+
raise InvalidRequestError.new("Internal Error: #{message}")
|
29
|
+
else
|
30
|
+
raise InvalidRequestError.new("Unknown Error#{response.body}")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
module Leadsquared
|
4
|
+
class Client
|
5
|
+
ENDPOINT = 'https://api.leadsquared.com'.freeze
|
6
|
+
HEADERS = {'Content-Type' => 'application/json', 'Accept' => 'application/json'}.freeze
|
7
|
+
|
8
|
+
attr_reader :key, :secret, :endpoint
|
9
|
+
|
10
|
+
def initialize(key = nil, secret = nil, endpoint = nil)
|
11
|
+
@key = key || Leadsquared.config.key
|
12
|
+
@secret = secret || Leadsquared.config.secret
|
13
|
+
@endpoint = endpoint || Leadsquared.config.endpoint || ENDPOINT
|
14
|
+
raise ArgumentError.new("Missing key or secret") unless @secret and @key
|
15
|
+
end
|
16
|
+
|
17
|
+
def post(url, params = {}, body = nil)
|
18
|
+
conn = Faraday.new(url: @endpoint)
|
19
|
+
merged_params = {accessKey: @key, secretKey: @secret}.merge(params)
|
20
|
+
response = conn.post(url) do |req|
|
21
|
+
req.headers = HEADERS
|
22
|
+
req.params = merged_params
|
23
|
+
req.body = body if body
|
24
|
+
end
|
25
|
+
|
26
|
+
response
|
27
|
+
end
|
28
|
+
|
29
|
+
def get(url, params = {})
|
30
|
+
conn = Faraday.new(url: @endpoint)
|
31
|
+
merged_params = {accessKey: @key, secretKey: @secret}.merge(params)
|
32
|
+
response = conn.get(url) do |req|
|
33
|
+
req.headers = HEADERS
|
34
|
+
req.params = merged_params
|
35
|
+
end
|
36
|
+
|
37
|
+
response
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module Leadsquared
|
4
|
+
|
5
|
+
class Config
|
6
|
+
attr_accessor :key, :secret, :endpoint, :logger
|
7
|
+
|
8
|
+
def logger
|
9
|
+
@logger ||= Logger.new(STDERR)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.configure
|
14
|
+
yield config
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.config
|
18
|
+
@config ||= Config.new
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'active_support/core_ext/object/try'
|
3
|
+
|
4
|
+
module Leadsquared
|
5
|
+
class Lead < ApiConnection
|
6
|
+
SERVICE = '/v2/LeadManagement.svc/'.freeze
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
super(SERVICE)
|
10
|
+
end
|
11
|
+
|
12
|
+
def get_meta_data
|
13
|
+
url = url_with_service("LeadsMetaData.Get")
|
14
|
+
response = connection.get(url, {})
|
15
|
+
handle_response response
|
16
|
+
end
|
17
|
+
|
18
|
+
def get_lead_by_id(lead_id)
|
19
|
+
url = url_with_service("Leads.GetById")
|
20
|
+
response = connection.get(url, {id: lead_id})
|
21
|
+
parsed_response = handle_response response
|
22
|
+
parsed_response.first
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_lead_by_email(email)
|
26
|
+
url = url_with_service("Leads.GetByEmailaddress")
|
27
|
+
response = connection.get(url, {emailaddress: email})
|
28
|
+
parsed_response = handle_response response
|
29
|
+
parsed_response.first
|
30
|
+
end
|
31
|
+
|
32
|
+
def quick_search(key)
|
33
|
+
url = url_with_service("Leads.GetByQuickSearch")
|
34
|
+
response = connection.get(url, {key: key})
|
35
|
+
handle_response response
|
36
|
+
end
|
37
|
+
|
38
|
+
def create_lead(email = nil, first_name = nil, last_name = nil)
|
39
|
+
url = url_with_service("Lead.Create")
|
40
|
+
body = [
|
41
|
+
{
|
42
|
+
"Attribute": "EmailAddress",
|
43
|
+
"Value": email
|
44
|
+
},
|
45
|
+
{
|
46
|
+
"Attribute": "FirstName",
|
47
|
+
"Value": first_name
|
48
|
+
},
|
49
|
+
{
|
50
|
+
"Attribute": "LastName",
|
51
|
+
"Value": last_name
|
52
|
+
}
|
53
|
+
]
|
54
|
+
response = connection.post(url, {}, body.to_json)
|
55
|
+
parsed_response = handle_response response
|
56
|
+
parsed_response["Message"]["Id"]
|
57
|
+
end
|
58
|
+
|
59
|
+
def update_lead(lead_id, values_hash = {})
|
60
|
+
url = url_with_service("Lead.Update")
|
61
|
+
body = values_hash.map {|key, val| {"Attribute" => key, "Value" => val} }
|
62
|
+
response = connection.post(url, {leadId: lead_id}, body.to_json)
|
63
|
+
parsed_response = handle_response response
|
64
|
+
parsed_response["Status"]
|
65
|
+
end
|
66
|
+
|
67
|
+
def create_or_update(email = nil, first_name = nil, last_name = nil, phone = nil, search_by = "EmailAddress")
|
68
|
+
url = url_with_service("Lead.CreateOrUpdate")
|
69
|
+
body = [
|
70
|
+
{
|
71
|
+
"Attribute": "EmailAddress",
|
72
|
+
"Value": email
|
73
|
+
},
|
74
|
+
{
|
75
|
+
"Attribute": "FirstName",
|
76
|
+
"Value": first_name
|
77
|
+
},
|
78
|
+
{
|
79
|
+
"Attribute": "LastName",
|
80
|
+
"Value": last_name
|
81
|
+
},
|
82
|
+
{
|
83
|
+
"Attribute": "Phone",
|
84
|
+
"Value": phone
|
85
|
+
},
|
86
|
+
{
|
87
|
+
"Attribute": "SearchBy",
|
88
|
+
"Value": search_by
|
89
|
+
}
|
90
|
+
]
|
91
|
+
response = connection.post(url, {}, body.to_json)
|
92
|
+
parsed_response = handle_response response
|
93
|
+
parsed_response["Message"]["Id"]
|
94
|
+
end
|
95
|
+
|
96
|
+
def visitor_to_lead(prospect_id, values_hash = {})
|
97
|
+
url = url_with_service("Lead.Convert")
|
98
|
+
body = values_hash.map {|key, val| {"Attribute" => key, "Value" => val} }
|
99
|
+
response = connection.post(url, {leadId: prospect_id}, body.to_json)
|
100
|
+
parsed_response = handle_response response
|
101
|
+
parsed_response["Status"]
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
@@ -0,0 +1,147 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Leadsquared::Lead do
|
4
|
+
let(:mock_connection) { double("connection") }
|
5
|
+
let(:service) { '/v2/ProspectActivity.svc/' }
|
6
|
+
let(:lead_id) { "3131ea6a-bb20-4457-b183-ddf6d8716dfe" }
|
7
|
+
let(:email) { "test@example.com" }
|
8
|
+
let(:current_utc_time) { Time.now.utc.to_s.gsub(/ UTC$/, "") }
|
9
|
+
subject { Leadsquared::Activity.new }
|
10
|
+
|
11
|
+
before do
|
12
|
+
expect(Leadsquared::Client).to receive(:new).and_return mock_connection
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#get_activities" do
|
16
|
+
let(:url) { "#{service}Retrieve" }
|
17
|
+
let(:activity_event_id) { 201 }
|
18
|
+
let(:body) do
|
19
|
+
{
|
20
|
+
"Parameter" => {"ActivityEvent" => activity_event_id},
|
21
|
+
"Paging" => {"Offset" => 0, "RowCount" => 10}
|
22
|
+
}
|
23
|
+
end
|
24
|
+
let(:success_response) do
|
25
|
+
{
|
26
|
+
"RecordCount" => 0,
|
27
|
+
"ProspectActivities" => []
|
28
|
+
}
|
29
|
+
end
|
30
|
+
let(:valid_response) { double("response", status: 200, body: success_response.to_json) }
|
31
|
+
|
32
|
+
it "valid request with existing id" do
|
33
|
+
expect(mock_connection).to receive(:post).with(url, {leadId: lead_id}, body.to_json).and_return valid_response
|
34
|
+
subject.get_activities(lead_id, activity_event_id)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "#create_activity_type" do
|
39
|
+
let(:url) { "#{service}CreateType" }
|
40
|
+
let(:score) { 10 }
|
41
|
+
let(:activity_name) { "New Activity" }
|
42
|
+
let(:description) { "Some Description" }
|
43
|
+
let(:body) do
|
44
|
+
{
|
45
|
+
"ActivityEventName" => activity_name,
|
46
|
+
"Score" => score,
|
47
|
+
"Description" => description,
|
48
|
+
"Direction" => 0
|
49
|
+
}
|
50
|
+
end
|
51
|
+
let(:success_response) do
|
52
|
+
{
|
53
|
+
"Status": "Success",
|
54
|
+
"Message": {
|
55
|
+
"Id": "206"
|
56
|
+
}
|
57
|
+
}
|
58
|
+
end
|
59
|
+
let(:valid_response) { double("response", status: 200, body: success_response.to_json) }
|
60
|
+
|
61
|
+
it "activity event" do
|
62
|
+
expect(mock_connection).to receive(:post).with(url, {}, body.to_json).and_return valid_response
|
63
|
+
response = subject.create_activity_type(activity_name, score, description)
|
64
|
+
expect(response).to eq("206")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "#create" do
|
69
|
+
let(:url) { "#{service}Create" }
|
70
|
+
let(:id) { "d91bd87a-7bca-11e5-a199-22000aa4133b" }
|
71
|
+
let(:event_id) { "201" }
|
72
|
+
let(:body) do
|
73
|
+
{
|
74
|
+
"EmailAddress" => email,
|
75
|
+
"ActivityEvent" => event_id,
|
76
|
+
"ActivityNote" => nil,
|
77
|
+
"ActivityDateTime" => current_utc_time,
|
78
|
+
"FirstName" => nil,
|
79
|
+
"LastName" => nil
|
80
|
+
}
|
81
|
+
end
|
82
|
+
|
83
|
+
let(:success_response) do
|
84
|
+
{
|
85
|
+
"Status": "Success",
|
86
|
+
"Message": {
|
87
|
+
"Id": id
|
88
|
+
}
|
89
|
+
}
|
90
|
+
end
|
91
|
+
let(:valid_response) { double("response", status: 200, body: success_response.to_json) }
|
92
|
+
|
93
|
+
before do
|
94
|
+
Timecop.freeze(Time.local(2015))
|
95
|
+
end
|
96
|
+
|
97
|
+
after do
|
98
|
+
Timecop.return
|
99
|
+
end
|
100
|
+
|
101
|
+
it "valid activity" do
|
102
|
+
expect(mock_connection).to receive(:post).with(url, {}, body.to_json).and_return valid_response
|
103
|
+
response = subject.create(email, event_id)
|
104
|
+
expect(response).to eq(id)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe "#create_lead_activity" do
|
109
|
+
let(:url) { "#{service}Create" }
|
110
|
+
let(:id) { "d91bd87a-7bca-11e5-a199-22000aa4133b" }
|
111
|
+
let(:event_id) { "201" }
|
112
|
+
let(:notes) { "Some Description" }
|
113
|
+
let(:body) do
|
114
|
+
{
|
115
|
+
"RelatedProspectId" => lead_id,
|
116
|
+
"ActivityEvent" => event_id,
|
117
|
+
"ActivityNote" => notes,
|
118
|
+
"ActivityDateTime" => current_utc_time
|
119
|
+
}
|
120
|
+
end
|
121
|
+
|
122
|
+
let(:success_response) do
|
123
|
+
{
|
124
|
+
"Status": "Success",
|
125
|
+
"Message": {
|
126
|
+
"Id": id
|
127
|
+
}
|
128
|
+
}
|
129
|
+
end
|
130
|
+
let(:valid_response) { double("response", status: 200, body: success_response.to_json) }
|
131
|
+
|
132
|
+
before do
|
133
|
+
Timecop.freeze(Time.local(2015))
|
134
|
+
end
|
135
|
+
|
136
|
+
after do
|
137
|
+
Timecop.return
|
138
|
+
end
|
139
|
+
|
140
|
+
it "valid activity" do
|
141
|
+
expect(mock_connection).to receive(:post).with(url, {leadId: lead_id}, body.to_json).and_return valid_response
|
142
|
+
response = subject.create_lead_activity(lead_id, event_id, notes)
|
143
|
+
expect(response).to eq(id)
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Leadsquared::Client do
|
4
|
+
let(:client) { Leadsquared::Client.new('token', 'pass', 'http://localhost:8088/mock') }
|
5
|
+
|
6
|
+
describe "Valid Request" do
|
7
|
+
let(:service) { "service1" }
|
8
|
+
let(:body) do
|
9
|
+
{key1: 'value1', key2: 'value2'}
|
10
|
+
end
|
11
|
+
let(:auth_params) { "?accessKey=token&secretKey=pass" }
|
12
|
+
|
13
|
+
it "#post" do
|
14
|
+
blk = lambda do |req|
|
15
|
+
expect(req.body).to match(body)
|
16
|
+
end
|
17
|
+
stub_request(:post, "http://localhost:8088/mock/#{service}#{auth_params}").with(&blk).
|
18
|
+
to_return(status: 200, body: "{\"id\": \"123456\"}")
|
19
|
+
response = client.post(service, {}, body)
|
20
|
+
expect(response.status).to eql(200)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "#get" do
|
24
|
+
params = {xid: '1'}
|
25
|
+
stub_request(:get, "http://localhost:8088/mock/#{service}#{auth_params}&xid=1").
|
26
|
+
to_return(status: 200, body: body.to_json)
|
27
|
+
response = client.get(service, params)
|
28
|
+
expect(response.status).to eql(200)
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "Invalid Request" do
|
34
|
+
pending "TODO"
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "Configuration" do
|
38
|
+
it "requires token and secret" do
|
39
|
+
expect { Leadsquared::Client.new }.to raise_error(ArgumentError)
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "preset" do
|
43
|
+
before do
|
44
|
+
Leadsquared.configure do |config|
|
45
|
+
config.key = 'mylogin'
|
46
|
+
config.secret = 'mypassword'
|
47
|
+
config.endpoint = "http://google.com"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
after do
|
52
|
+
Leadsquared.configure do |config|
|
53
|
+
config.key = nil
|
54
|
+
config.secret = nil
|
55
|
+
config.endpoint = Leadsquared::Client::ENDPOINT
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
it "configures the client" do
|
60
|
+
Leadsquared.configure do |config|
|
61
|
+
config.key = 'mylogin'
|
62
|
+
config.secret = 'mypassword'
|
63
|
+
config.endpoint = "http://google.com"
|
64
|
+
end
|
65
|
+
client = Leadsquared::Client.new
|
66
|
+
expect(client.key).to eq('mylogin')
|
67
|
+
expect(client.secret).to eq('mypassword')
|
68
|
+
expect(client.endpoint).to eq("http://google.com")
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,324 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Leadsquared::Lead do
|
4
|
+
let(:mock_connection) { double("connection") }
|
5
|
+
let(:lead_id) { "3131ea6a-bb20-4457-b183-ddf6d8716dfe" }
|
6
|
+
let(:email) { "test@example.com" }
|
7
|
+
let(:first_name) { "Bob" }
|
8
|
+
let(:last_name) { "Zeiger" }
|
9
|
+
|
10
|
+
let(:invalid_response_body) do
|
11
|
+
{
|
12
|
+
"Status" => "Error",
|
13
|
+
"ExceptionType" => "MXDuplicateEntryException",
|
14
|
+
"ExceptionMessage" => "Duplicate entry 'john.smith@acmeconsulting.co' for key 'UQ_Prospect_Base_EmailAddress'"
|
15
|
+
}
|
16
|
+
end
|
17
|
+
let(:invalid_response) { double("response", status: 500, body: invalid_response_body) }
|
18
|
+
let(:service) { '/v2/LeadManagement.svc/' }
|
19
|
+
|
20
|
+
subject { Leadsquared::Lead.new }
|
21
|
+
|
22
|
+
before do
|
23
|
+
expect( Leadsquared::Client).to receive(:new).and_return mock_connection
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#get_lead_by_id" do
|
27
|
+
let(:url) { "#{service}Leads.GetById" }
|
28
|
+
let(:valid_response) { double("response", status: 200, body: success_response.to_json) }
|
29
|
+
let(:empty_response) { double("response", status: 200, body: [].to_json) }
|
30
|
+
let(:success_response) do
|
31
|
+
[
|
32
|
+
{
|
33
|
+
"ProspectID" => " Lead Id ",
|
34
|
+
"FirstName" => "Syed",
|
35
|
+
"LastName" => "Rizwan Ali",
|
36
|
+
"EmailAddress" => "rizwan@yopmail.com",
|
37
|
+
"Company" => "Roga",
|
38
|
+
"SourceReferrer" => ""
|
39
|
+
}
|
40
|
+
]
|
41
|
+
end
|
42
|
+
|
43
|
+
it "valid request with existing id" do
|
44
|
+
expect(mock_connection).to receive(:get).with(url, {id: lead_id}).and_return valid_response
|
45
|
+
response = subject.get_lead_by_id(lead_id)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "with missing id" do
|
49
|
+
missing_id = "12345"
|
50
|
+
expect(mock_connection).to receive(:get).with(url, {id: missing_id}).and_return empty_response
|
51
|
+
response = subject.get_lead_by_id(missing_id)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "#get_lead_by_email" do
|
56
|
+
let(:url) { "#{service}Leads.GetByEmailaddress" }
|
57
|
+
let(:email) { "test@example.com" }
|
58
|
+
let(:valid_response) { double("response", status: 200, body: success_response.to_json) }
|
59
|
+
let(:empty_response) { double("response", status: 200, body: [].to_json) }
|
60
|
+
let(:success_response) do
|
61
|
+
[
|
62
|
+
{
|
63
|
+
"ProspectID" => " Lead Email ",
|
64
|
+
"FirstName" => "Syed",
|
65
|
+
"LastName" => "Rizwan Ali",
|
66
|
+
"EmailAddress" => "rizwan@yopmail.com",
|
67
|
+
"Company" => "Roga",
|
68
|
+
"SourceReferrer" => ""
|
69
|
+
}
|
70
|
+
]
|
71
|
+
end
|
72
|
+
|
73
|
+
it "valid request with existing id" do
|
74
|
+
expect(mock_connection).to receive(:get).with(url, {emailaddress: email}).and_return valid_response
|
75
|
+
response = subject.get_lead_by_email(email)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "with missing id" do
|
79
|
+
expect(mock_connection).to receive(:get).with(url, {emailaddress: email}).and_return empty_response
|
80
|
+
response = subject.get_lead_by_email(email)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe "#quick_search" do
|
85
|
+
let(:url) { "#{service}Leads.GetByQuickSearch" }
|
86
|
+
let(:key) { "Jhon" }
|
87
|
+
let(:valid_response) { double("response", status: 200, body: success_response.to_json) }
|
88
|
+
let(:empty_response) { double("response", status: 200, body: [].to_json) }
|
89
|
+
let(:success_response) do
|
90
|
+
[
|
91
|
+
{
|
92
|
+
"ProspectID": "16837df6-ec85werwer9b1f3a902",
|
93
|
+
"FirstName" => "Syed",
|
94
|
+
"LastName" => "Rizwan Ali",
|
95
|
+
"EmailAddress" => "rizwan@yopmail.com",
|
96
|
+
"Company" => "Roga",
|
97
|
+
"SourceReferrer" => ""
|
98
|
+
},
|
99
|
+
{
|
100
|
+
"ProspectID": "16837df6-ec85werwer9b1f3a903",
|
101
|
+
"FirstName" => "Syed2",
|
102
|
+
"LastName" => "Rizwan Ali",
|
103
|
+
"EmailAddress" => "rizwan2@yopmail.com",
|
104
|
+
"Company" => "Roga",
|
105
|
+
"SourceReferrer" => ""
|
106
|
+
}
|
107
|
+
]
|
108
|
+
end
|
109
|
+
|
110
|
+
it "valid request with existing id" do
|
111
|
+
expect(mock_connection).to receive(:get).with(url, {key: key}).and_return valid_response
|
112
|
+
response = subject.quick_search(key)
|
113
|
+
end
|
114
|
+
|
115
|
+
it "with missing id" do
|
116
|
+
expect(mock_connection).to receive(:get).with(url, {key: key}).and_return empty_response
|
117
|
+
response = subject.quick_search(key)
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
describe "#visitor_to_lead" do
|
123
|
+
let(:url) { "#{service}Lead.Convert" }
|
124
|
+
let(:values_hash) do
|
125
|
+
{"OwnerId" => "1234567"}
|
126
|
+
end
|
127
|
+
let(:success_response) do
|
128
|
+
{
|
129
|
+
"Status" => "Success",
|
130
|
+
"Message" => {
|
131
|
+
"AffectedRows" => 1
|
132
|
+
}
|
133
|
+
}
|
134
|
+
end
|
135
|
+
let(:body) do
|
136
|
+
[
|
137
|
+
{
|
138
|
+
"Attribute": "OwnerId",
|
139
|
+
"Value": "1234567"
|
140
|
+
}
|
141
|
+
]
|
142
|
+
end
|
143
|
+
let(:valid_response) { double("response", status: 200, body: success_response.to_json) }
|
144
|
+
|
145
|
+
it "converts valid user" do
|
146
|
+
expect(mock_connection).to receive(:post).with(url, {leadId: lead_id}, body.to_json).and_return valid_response
|
147
|
+
response = subject.visitor_to_lead(lead_id, values_hash)
|
148
|
+
expect(response).to eq(success_response["Status"])
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
describe "#update" do
|
153
|
+
let(:url) { "#{service}Lead.Update" }
|
154
|
+
let(:new_values) do
|
155
|
+
{phone: "1234567", city: "NY"}
|
156
|
+
end
|
157
|
+
let(:success_response) do
|
158
|
+
{
|
159
|
+
"Status" => "Success",
|
160
|
+
"Message" => {
|
161
|
+
"AffectedRows" => 1
|
162
|
+
}
|
163
|
+
}
|
164
|
+
end
|
165
|
+
let(:failed_response) do
|
166
|
+
{
|
167
|
+
"Status" => "Error",
|
168
|
+
"ExceptionType" => "MXInvalidEntityException",
|
169
|
+
"ExceptionMessage" => "Lead does not exist in the system"
|
170
|
+
}
|
171
|
+
end
|
172
|
+
let(:body) do
|
173
|
+
[
|
174
|
+
{
|
175
|
+
"Attribute": "phone",
|
176
|
+
"Value": "1234567"
|
177
|
+
},
|
178
|
+
{
|
179
|
+
"Attribute": "city",
|
180
|
+
"Value": "NY"
|
181
|
+
}
|
182
|
+
]
|
183
|
+
end
|
184
|
+
let(:valid_response) { double("response", status: 200, body: success_response.to_json) }
|
185
|
+
let(:invalid_response) { double("response", status: 500, body: failed_response.to_json) }
|
186
|
+
|
187
|
+
it "existing lead" do
|
188
|
+
expect(mock_connection).to receive(:post).with(url, {leadId: lead_id}, body.to_json).and_return valid_response
|
189
|
+
response = subject.update_lead(lead_id, new_values)
|
190
|
+
expect(response).to eq(success_response["Status"])
|
191
|
+
end
|
192
|
+
|
193
|
+
it "missing lead" do
|
194
|
+
expect(mock_connection).to receive(:post).with(url, {leadId: lead_id}, body.to_json).and_return invalid_response
|
195
|
+
expect {
|
196
|
+
subject.update_lead(lead_id, new_values)
|
197
|
+
}.to raise_error(Leadsquared::InvalidRequestError)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
describe "#create_or_update" do
|
202
|
+
let(:success_response) do
|
203
|
+
{
|
204
|
+
"Status" => "Success",
|
205
|
+
"Message" => {
|
206
|
+
"Id" => "xxxxxxxx-Lead-Idxx-xxxx-xxxxxxxxxxx",
|
207
|
+
"AffectedRows" => 1
|
208
|
+
}
|
209
|
+
}
|
210
|
+
end
|
211
|
+
|
212
|
+
describe "search by email" do
|
213
|
+
let(:url) { "#{service}Lead.CreateOrUpdate" }
|
214
|
+
let(:valid_response) { double("response", status: 200, body: success_response.to_json) }
|
215
|
+
let(:body) do
|
216
|
+
[
|
217
|
+
{
|
218
|
+
"Attribute": "EmailAddress",
|
219
|
+
"Value": email
|
220
|
+
},
|
221
|
+
{
|
222
|
+
"Attribute": "FirstName",
|
223
|
+
"Value": first_name
|
224
|
+
},
|
225
|
+
{
|
226
|
+
"Attribute": "LastName",
|
227
|
+
"Value": last_name
|
228
|
+
},
|
229
|
+
{
|
230
|
+
"Attribute": "Phone",
|
231
|
+
"Value": nil
|
232
|
+
},
|
233
|
+
{
|
234
|
+
"Attribute": "SearchBy",
|
235
|
+
"Value": "EmailAddress"
|
236
|
+
}
|
237
|
+
]
|
238
|
+
end
|
239
|
+
it "valid request" do
|
240
|
+
expect(mock_connection).to receive(:post).with(url, {}, body.to_json).and_return valid_response
|
241
|
+
response = subject.create_or_update(email, first_name, last_name)
|
242
|
+
expect(response).to eq(success_response["Message"]["Id"])
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
describe "search by phone" do
|
247
|
+
pending "TODO"
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
describe "#create_lead" do
|
252
|
+
let(:url) { "#{service}Lead.Create" }
|
253
|
+
let(:body) do
|
254
|
+
[
|
255
|
+
{
|
256
|
+
"Attribute" => "EmailAddress",
|
257
|
+
"Value" => email
|
258
|
+
},
|
259
|
+
{
|
260
|
+
"Attribute" => "FirstName",
|
261
|
+
"Value" => first_name
|
262
|
+
},
|
263
|
+
{
|
264
|
+
"Attribute" => "LastName",
|
265
|
+
"Value" => last_name
|
266
|
+
}
|
267
|
+
]
|
268
|
+
end
|
269
|
+
let(:success_response) do
|
270
|
+
{
|
271
|
+
"Status" => "Success",
|
272
|
+
"Message" => {
|
273
|
+
"Id" => "3131ea6a-bb20-4457-b183-ddf6d8716dfe"
|
274
|
+
}
|
275
|
+
}
|
276
|
+
end
|
277
|
+
let(:valid_response) { double("response", status: 200, body: success_response.to_json) }
|
278
|
+
|
279
|
+
it "valid request with given params" do
|
280
|
+
expect(mock_connection).to receive(:post).with(url, {}, body.to_json).and_return valid_response
|
281
|
+
response = subject.create_lead(email, first_name, last_name)
|
282
|
+
expect(response).to eq(success_response["Message"]["Id"])
|
283
|
+
end
|
284
|
+
|
285
|
+
it "invalid request" do
|
286
|
+
expect(mock_connection).to receive(:post).with(url, {}, body.to_json).and_return invalid_response
|
287
|
+
expect {
|
288
|
+
subject.create_lead(email, first_name, last_name)
|
289
|
+
}.to raise_error(Leadsquared::InvalidRequestError)
|
290
|
+
end
|
291
|
+
end
|
292
|
+
|
293
|
+
describe "#get_meta_data" do
|
294
|
+
let(:success_response) do
|
295
|
+
[
|
296
|
+
{
|
297
|
+
"SchemaName" => "mx_CreationTime",
|
298
|
+
"DisplayName" => "CreationTime",
|
299
|
+
"DataType" => "Time",
|
300
|
+
"IsMandatory" => false,
|
301
|
+
"MaxLength" => "50",
|
302
|
+
"RenderType" => 21,
|
303
|
+
"RenderTypeTextValue" => "Time"
|
304
|
+
},
|
305
|
+
{
|
306
|
+
"SchemaName" => "mx_ModifiedTime",
|
307
|
+
"DisplayName" => "ModifiedTime",
|
308
|
+
"DataType" => "Time",
|
309
|
+
"IsMandatory" => false,
|
310
|
+
"MaxLength" => "50",
|
311
|
+
"RenderType" => 21,
|
312
|
+
"RenderTypeTextValue" => "Time"
|
313
|
+
}]
|
314
|
+
end
|
315
|
+
let(:valid_response) { double("response", status: 200, body: success_response.to_json) }
|
316
|
+
let(:url) { "#{service}LeadsMetaData.Get" }
|
317
|
+
|
318
|
+
it "valid request with given params" do
|
319
|
+
expect(mock_connection).to receive(:get).with(url, {}).and_return valid_response
|
320
|
+
response = subject.get_meta_data
|
321
|
+
end
|
322
|
+
|
323
|
+
end
|
324
|
+
end
|