Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add id support to customer and anonymous events #85

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
5 changes: 5 additions & 0 deletions lib/customerio/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def create_anonymous_event(anonymous_id, event_name, attributes = {})
def create_event(url:, event_name:, anonymous_id: nil, attributes: {})
body = { :name => event_name, :data => attributes }
body[:timestamp] = attributes[:timestamp] if valid_timestamp?(attributes[:timestamp])
body[:id] = attributes[:id] if valid_ulid?(attributes[:id])
body[:anonymous_id] = anonymous_id unless is_empty?(anonymous_id)

@client.request_and_verify_response(:post, url, body)
Expand All @@ -179,6 +180,10 @@ def valid_timestamp?(timestamp)
timestamp && timestamp.is_a?(Integer) && timestamp > 999999999 && timestamp < 100000000000
end

def valid_ulid?(id)
id && id.is_a?(String) && id.length == 26
end

def is_empty?(val)
val.nil? || (val.is_a?(String) && val.strip == "")
end
Expand Down
65 changes: 65 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,54 @@ def json(data)
client.track(5, "purchase", type: "socks", price: "13.99", timestamp: "Hello world")
end

it "allows sending of an id" do
stub_request(:post, api_uri('/api/v1/customers/5/events')).
with(body: json({
name: "purchase",
data: {
type: "socks",
price: "13.99",
id: "01G653VCS2Z4KQYQJCBVVB2CCW"
},
id: "01G653VCS2Z4KQYQJCBVVB2CCW"
})).
to_return(status: 200, body: "", headers: {})

client.track(5, "purchase", type: "socks", price: "13.99", id: "01G653VCS2Z4KQYQJCBVVB2CCW")
end

it "doesn't send id if id isn't a string" do
stub_request(:post, api_uri('/api/v1/customers/5/events')).
with(body: json({
name: "purchase",
data: {
type: "socks",
price: "13.99",
id: 1234
}
})).

to_return(status: 200, body: "", headers: {})

client.track(5, "purchase", type: "socks", price: "13.99", id: 1234)
end

it "doesn't send id if id isn't a ULID" do
stub_request(:post, api_uri('/api/v1/customers/5/events')).
with(body: json({
name: "purchase",
data: {
type: "socks",
price: "13.99",
id: "1234"
}
})).

to_return(status: 200, body: "", headers: {})

client.track(5, "purchase", type: "socks", price: "13.99", id: "1234")
end

context "tracking an anonymous event" do
let(:anon_id) { "anon-id" }

Expand Down Expand Up @@ -425,6 +473,23 @@ def json(data)
client.track_anonymous(anon_id, "purchase", type: "socks", price: "13.99", timestamp: 1561231234)
end

it "allows sending of an id" do
stub_request(:post, api_uri('/api/v1/events')).
with(body: {
anonymous_id: anon_id,
name: "purchase",
data: {
type: "socks",
price: "13.99",
id: "01G653VCS2Z4KQYQJCBVVB2CCW"
},
id: "01G653VCS2Z4KQYQJCBVVB2CCW"
}).
to_return(status: 200, body: "", headers: {})

client.track_anonymous(anon_id, "purchase", type: "socks", price: "13.99", id: "01G653VCS2Z4KQYQJCBVVB2CCW")
end

it "raises an error if POST doesn't return a 2xx response code" do
stub_request(:post, api_uri('/api/v1/events')).
with(body: { anonymous_id: anon_id, name: "purchase", data: {} }).
Expand Down