Skip to content

Commit

Permalink
refactor(CE): remove unwanted code
Browse files Browse the repository at this point in the history
  • Loading branch information
xyfer17 committed Dec 29, 2024
1 parent 124eaee commit 57ab09c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module Destination
module Mixpanel
include Multiwoven::Integrations::Core

BASE_URL = "https://api.mixpanel.com"

class Client < DestinationConnector
prepend Multiwoven::Integrations::Core::RateLimiter
Expand Down Expand Up @@ -85,7 +84,7 @@ def send_to_mixpanel(record, stream_name)

stream_config = {
"UserProfiles" => {
endpoint: "#{BASE_URL}/engage#profile-set",
endpoint: "#{MIXPANEL_BASE_URL}/engage#profile-set",
payload: ->(record) {
[{
"$token" => @api_token,
Expand All @@ -95,7 +94,7 @@ def send_to_mixpanel(record, stream_name)
}
},
"Events" => {
endpoint: "#{BASE_URL}/track",
endpoint: "#{MIXPANEL_BASE_URL}/track",
payload: ->(record) {
[{
"event" => record[:name],
Expand All @@ -117,18 +116,18 @@ def send_to_mixpanel(record, stream_name)

def send_request(endpoint, payload)
url = URI(endpoint)

http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true

request = Net::HTTP::Post.new(url)
request["accept"] = 'text/plain'
request["content-type"] = 'application/json'
request.body = payload.map { |item| item.transform_keys(&:to_s) }.to_json


formatted_payload = payload.is_a?(Array) ? payload.map { |item| item.transform_keys(&:to_s) } : payload.transform_keys(&:to_s)
request.body = formatted_payload.to_json

response = http.request(request)
puts response.read_body

handle_response(response)
end

Expand All @@ -152,24 +151,10 @@ def validate_connection
"event" => "Test Event",
"properties" => { "$token" => @api_token }
}
response = send_request("#{BASE_URL}/track", test_payload)
response = send_request("#{MIXPANEL_BASE_URL}/track", test_payload)
raise "Connection failed" unless response["status"] == 1
end

def log_request_response(level, args, response)
{ level: level, message: "Mixpanel request with #{args}: #{response}" }
end

def tracking_message(success, failure, log_message_array)
{
sync_id: @sync_config.sync_id,
sync_run_id: @sync_config.sync_run_id,
success_count: success,
failure_count: failure,
logs: log_message_array
}
end

def load_catalog
read_json(CATALOG_SPEC_PATH)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,83 +61,86 @@

let(:records) do
[
{ id: "123", properties: { name: "John Doe", email: "[email protected]" } },
{ id: "124", properties: { name: "Jane Doe", email: "[email protected]" } }
{ id: "123", name: "All Events", properties: { name: "John Doe", email: "[email protected]" } }
]
end

describe "#check_connection" do
context "when the connection is successful" do
before do
stub_request(:post, "#{base_url}/track")
.to_return(status: 200, body: '', headers: {})
end

it "returns a successful connection status" do
allow(client).to receive(:authenticate_client).and_return(true)

response = client.check_connection(connection_config)
let(:profile_body) do
[
{
"$token" => "api_token",
"$distinct_id" => "123",
"$set" => {
"name" => "John Doe",
"email" => "[email protected]"
}
}
].to_json
end

expect(response).to be_a(Multiwoven::Integrations::Protocol::MultiwovenMessage)
expect(response.connection_status.status).to eq("succeeded")
end
describe "#check_connection" do
context 'when connection is valid' do
before do
stub_request(:post, "#{base_url}/track")
.to_return(status: 200, body: { status: 1 }.to_json)
end

context "when the connection fails" do
it "returns a failed connection status with an error message" do
allow(client).to receive(:authenticate_client).and_raise(StandardError.new("connection failed"))
it 'returns a success status' do
result = subject.check_connection(connection_config)
expect(result.type).to eq('connection_status')
expect(result.connection_status.status).to eq('succeeded')
end

end

response = client.check_connection(connection_config)
context 'when the connection fails' do
before do
stub_request(:post, "https://api.mixpanel.com/track")
.to_return(status: 401, body: 'Unauthorized')
end

expect(response).to be_a(Multiwoven::Integrations::Protocol::MultiwovenMessage)
expect(response.connection_status.status).to eq("failed")
it 'returns a failed connection status with an error message' do
result = subject.check_connection(connection_config)
expect(result.type).to eq('connection_status')
expect(result.connection_status.status).to eq('failed')
expect(result.connection_status.message).to eq('Authentication Error: Invalid API token.')
end
end
end
end

# describe "#write" do
# context "when writing user profiles" do
# let(:endpoint) { "#{base_url}/engage#profile-set" }

# before do
# stub_request(:post, endpoint)
# .to_return(status: 200, body: '{"status": "ok"}', headers: {})
# end

# it "increments the success count" do
# response = client.write(sync_config, records)

# expect(response.tracking.success).to eq(records.size)
# expect(response.tracking.failed).to eq(0)
# log_message = response.tracking.logs.first
# expect(log_message).to be_a(Multiwoven::Integrations::Protocol::LogMessage)
# expect(log_message.level).to eql("info")
# expect(log_message.message).to include("request")
# expect(log_message.message).to include("response")
# end
# end

# context "when writing events" do
# let(:endpoint) { "#{base_url}/track" }

# before do
# stub_request(:post, endpoint)
# .to_return(status: 400, body: '{"error": "Invalid Request"}', headers: {})
# end

# it "increments the failure count" do
# response = client.write(sync_config, records)

# expect(response.tracking.failed).to eq(records.size)
# expect(response.tracking.success).to eq(0)
# log_message = response.tracking.logs.first
# expect(log_message).to be_a(Multiwoven::Integrations::Protocol::LogMessage)
# expect(log_message.level).to eql("error")
# expect(log_message.message).to include("request")
# expect(log_message.message).to include("response")
# end
# end
# end
describe "#write" do
context "when writing user profiles" do
let(:endpoint) { "#{base_url}/engage" }

before do
stub_request(:post, endpoint)
.with(
body: profile_body,
headers: {
"Accept" => "text/plain",
"Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
"Content-Type" => "application/json",
"Host" => "api.mixpanel.com",
"User-Agent" => "Ruby"
}
)
.to_return(status: 200, body: '{"status": "ok"}', headers: {})
end

it "increments the success count" do
response = client.write(sync_config, records)

expect(response.tracking.success).to eq(records.size)
expect(response.tracking.failed).to eq(0)
log_message = response.tracking.logs.first
expect(log_message).to be_a(Multiwoven::Integrations::Protocol::LogMessage)
expect(log_message.level).to eql("info")
expect(log_message.message).to include("request")
expect(log_message.message).to include("response")
end
end

end

describe "#meta_data" do
it "serves its GitHub image URL as an icon" do
Expand Down

0 comments on commit 57ab09c

Please sign in to comment.