Skip to content

Commit

Permalink
refactor(CE): linting fix
Browse files Browse the repository at this point in the history
  • Loading branch information
xyfer17 committed Dec 30, 2024
1 parent 6cb76bb commit 34c5d59
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
# frozen_string_literal: true

require 'uri'
require 'net/http'
require 'json'
require "uri"
require "net/http"
require "json"

module Multiwoven
module Integrations
module Destination
module Mixpanel
include Multiwoven::Integrations::Core


class Client < DestinationConnector
prepend Multiwoven::Integrations::Core::RateLimiter

Expand Down Expand Up @@ -81,11 +80,10 @@ def process_records(records, stream)
end

def send_to_mixpanel(record, stream_name)

stream_config = {
"UserProfiles" => {
endpoint: "#{MIXPANEL_BASE_URL}/engage#profile-set",
payload: ->(record) {
payload: lambda { |record|
[{
"$token" => @api_token,
"$distinct_id" => record[:id],
Expand All @@ -95,38 +93,37 @@ def send_to_mixpanel(record, stream_name)
},
"Events" => {
endpoint: "#{MIXPANEL_BASE_URL}/track",
payload: ->(record) {
payload: lambda { |record|
[{
"event" => record[:name],
"properties" => record[:properties].merge("token" => @api_token)
}]
}
}
}

config = stream_config[stream_name]
raise "Unsupported stream: #{stream_name}" unless config

endpoint = config[:endpoint]
payload = config[:payload].call(record)

send_request(endpoint, payload)
end


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["accept"] = "text/plain"
request["content-type"] = "application/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)
handle_response(response)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,68 +79,66 @@
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 connection is valid" do
before do
stub_request(:post, "#{base_url}/track")
.to_return(status: 200, body: { status: 1 }.to_json)
end

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')
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

end

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

it 'returns a failed connection status with an error message' do
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.')
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" }

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")
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

end

describe "#meta_data" do
it "serves its GitHub image URL as an icon" do
Expand All @@ -165,9 +163,9 @@ def sync_config
catalog.streams.each do |stream|
case stream.name
when "UserProfiles"
expect(stream.supported_sync_modes).to eql(["full_refresh", "incremental"])
expect(stream.supported_sync_modes).to eql(%w[full_refresh incremental])
when "Events"
expect(stream.supported_sync_modes).to eql(["full_refresh", "incremental"])
expect(stream.supported_sync_modes).to eql(%w[full_refresh incremental])
end
end
end
Expand Down

0 comments on commit 34c5d59

Please sign in to comment.