From 9580d9fc839a7e9598592ab1c065697dd2ddf0d4 Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 14 Oct 2022 11:14:45 -0600 Subject: [PATCH 1/3] Allow customer_id to be specified when identifying a customer --- lib/customerio/client.rb | 12 ++++++++++-- spec/client_spec.rb | 8 ++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/customerio/client.rb b/lib/customerio/client.rb index ffb8754..77fecce 100644 --- a/lib/customerio/client.rb +++ b/lib/customerio/client.rb @@ -29,6 +29,10 @@ def identify(attributes) create_or_update(attributes) end + def identify_customer_id(customer_id: nil, **attributes) + create_or_update_customer_id(customer_id, **attributes) + end + def delete(customer_id) raise ParamError.new("customer_id must be a non-empty string") if is_empty?(customer_id) @client.request_and_verify_response(:delete, customer_path(customer_id)) @@ -150,10 +154,14 @@ def merge_customers_path end def create_or_update(attributes = {}) + create_or_update_customer_id(attributes[:id] || attributes["id"], attributes) + end + + def create_or_update_customer_id(customer_id, attributes = {}) attributes = Hash[attributes.map { |(k,v)| [ k.to_sym, v ] }] - raise MissingIdAttributeError.new("Must provide a customer id") if is_empty?(attributes[:id]) + raise MissingIdAttributeError.new("Must provide a customer id") if is_empty?(customer_id) - url = customer_path(attributes[:id]) + 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..18d2766 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -183,6 +183,14 @@ def json(data) lambda { client.identify(attributes) }.should_not raise_error() end + + it "uses provided url_id rather than id" do + stub_request(:put, api_uri('/api/v1/customers/cio_5')). + with(body: json(id: "5")). + to_return(status: 200, body: "", headers: {}) + + client.identify_customer_id(customer_id: "cio_5", id: "5") + end end describe "#delete" do From 91e6406036eb0d534284f02e42b420ebf38d7278 Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 14 Oct 2022 11:21:36 -0600 Subject: [PATCH 2/3] Document new identify_customer_id method --- README.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b438fda..810fe44 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,25 @@ $customerio.identify( ) ``` +If you wish to specify a customer ID that is different than the one used in the `id` attribute, you can do so by using the `identify_customer_id` method: +```ruby +# Arguments +# customer_id (required) - the customer ID to use for this customer, may be an id, email address, or the cio_id. This attribute +# will be used to construct the URL but not sent in the body attributes. +# attributes (required) - a hash of information about the customer. You can pass any +# information that would be useful in your triggers. You +# must at least pass in an id, email, and created_at timestamp. + +$customerio.identify_customer_id( + :customer_id => "cio_5", + :id => 5, + :email => "bob@example.com", + :created_at => customer.created_at.to_i, + :first_name => "Bob", + :plan => "basic" +) +``` + ### Deleting customers Deleting a customer will remove them, and all their information from From fbce83c26442cd77f0f899c28aeb18b1190cea20 Mon Sep 17 00:00:00 2001 From: John Beck Date: Tue, 28 Nov 2023 09:23:20 -0600 Subject: [PATCH 3/3] incorporate feedback from jeremyw --- spec/client_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 18d2766..0dc0176 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -184,7 +184,7 @@ def json(data) lambda { client.identify(attributes) }.should_not raise_error() end - it "uses provided url_id rather than id" do + it "uses provided customer_id rather than id" do stub_request(:put, api_uri('/api/v1/customers/cio_5')). with(body: json(id: "5")). to_return(status: 200, body: "", headers: {}) @@ -618,7 +618,7 @@ def json(data) }.to raise_error(Customerio::Client::ParamError, 'timestamp must be a valid timestamp') end end - + describe "#merge_customers" do before(:each) do @client = Customerio::Client.new("SITE_ID", "API_KEY", :json => true)