From 2d3b01ed98e4b5216c6eb91953b74c5f4e12823b Mon Sep 17 00:00:00 2001 From: Richard Dawe Date: Fri, 8 Dec 2023 14:49:13 +0000 Subject: [PATCH 1/2] Allow id and email identifiers to be updated when cio_id specified --- README.md | 20 +++++++++++++++++++- lib/customerio/client.rb | 12 ++++++++++-- spec/client_spec.rb | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b438fda..bcfd5a2 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ if you pass along the current subscription plan (free / basic / premium) for you set up triggers which are only sent to customers who have subscribed to a particular plan (e.g. "premium"). -You'll want to indentify your customers when they sign up for your app and any time their +You'll want to identify your customers when they sign up for your app and any time their key information changes. This keeps [Customer.io](https://customer.io) up to date with your customer information. ```ruby @@ -95,6 +95,24 @@ $customerio.identify( ) ``` +### Updating customers + +You can use the identify operation to update customers. +If you need to change the `id` or `email` identifiers for a customer, +you will need to pass in the `cio_id` identifier. +`cio_id` is a unique identifier set by Customer.io, used to reference a person, +and cannot be changed. + +E.g.: if the customer created in the identify operation above was given the `cio_id` of `"f000000d"`, you could change its ID and email address using: + +```ruby +$customerio.identify( + :cio_id => "f000000d", + :id => 1005, + :email => "bob.fullname@example.com" +) +``` + ### Deleting customers Deleting a customer will remove them, and all their information from diff --git a/lib/customerio/client.rb b/lib/customerio/client.rb index ffb8754..d322ff4 100644 --- a/lib/customerio/client.rb +++ b/lib/customerio/client.rb @@ -151,9 +151,17 @@ def merge_customers_path def create_or_update(attributes = {}) attributes = Hash[attributes.map { |(k,v)| [ k.to_sym, v ] }] - raise MissingIdAttributeError.new("Must provide a customer id") if is_empty?(attributes[:id]) + if is_empty?(attributes[:id]) && is_empty?(attributes[:cio_id]) + raise MissingIdAttributeError.new("Must provide a customer id") + end - url = customer_path(attributes[:id]) + # Use cio_id as the identifier, as present, + # to allow the id and email identifiers to be updated. + customer_id = attributes[:id] + if !is_empty?(attributes[:cio_id]) + customer_id = "cio_" + attributes[:cio_id] + end + url = customer_path(customer_id) @client.request_and_verify_response(:put, url, attributes) end diff --git a/spec/client_spec.rb b/spec/client_spec.rb index dfd9321..41df3cd 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -172,6 +172,7 @@ def json(data) it "requires an id attribute" do lambda { client.identify(email: "customer@example.com") }.should raise_error(Customerio::Client::MissingIdAttributeError) lambda { client.identify(id: "") }.should raise_error(Customerio::Client::MissingIdAttributeError) + lambda { client.identify(cio_id: "") }.should raise_error(Customerio::Client::MissingIdAttributeError) end it 'should not raise errors when attribute keys are strings' do @@ -183,6 +184,45 @@ def json(data) lambda { client.identify(attributes) }.should_not raise_error() end + + it 'uses cio_id for customer id, when present, for id updates' do + stub_request(:put, api_uri('/api/v1/customers/cio_347f00d')).with( + body: { + cio_id: "347f00d", + id: 5 + }).to_return(status: 200, body: "", headers: {}) + + client.identify({ + cio_id: "347f00d", + id: 5 + }) + end + + it 'uses cio_id for customer id, when present, for email updates' do + stub_request(:put, api_uri('/api/v1/customers/cio_347f00d')).with( + body: { + cio_id: "347f00d", + email: "different.customer@example.com" + }).to_return(status: 200, body: "", headers: {}) + + client.identify({ + cio_id: "347f00d", + email: "different.customer@example.com" + }) + end + + it 'allows updates with cio_id as the only id' do + stub_request(:put, api_uri('/api/v1/customers/cio_347f00d')).with( + body: { + cio_id: "347f00d", + location: "here" + }).to_return(status: 200, body: "", headers: {}) + + client.identify({ + cio_id: "347f00d", + location: "here" + }) + end end describe "#delete" do From 997c1faf2b94f10334aa01bcae6421dc66b99a2a Mon Sep 17 00:00:00 2001 From: Richard Dawe Date: Fri, 8 Dec 2023 14:52:01 +0000 Subject: [PATCH 2/2] Fix typo --- lib/customerio/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/customerio/client.rb b/lib/customerio/client.rb index d322ff4..e49a6cc 100644 --- a/lib/customerio/client.rb +++ b/lib/customerio/client.rb @@ -155,7 +155,7 @@ def create_or_update(attributes = {}) raise MissingIdAttributeError.new("Must provide a customer id") end - # Use cio_id as the identifier, as present, + # Use cio_id as the identifier, if present, # to allow the id and email identifiers to be updated. customer_id = attributes[:id] if !is_empty?(attributes[:cio_id])