diff --git a/README.md b/README.md index bcfd5a2..d41b443 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,29 @@ $customerio.identify( ) ``` -### Updating customers +### Updating customers: Using other IDs + +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 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 => "bob@example.com", + :id => 5, + :email => "bob@example.com", + :created_at => customer.created_at.to_i, + :first_name => "Bob", + :plan => "basic" +) +``` + +### Updating customers: Changing identifiers You can use the identify operation to update customers. If you need to change the `id` or `email` identifiers for a customer, diff --git a/lib/customerio/client.rb b/lib/customerio/client.rb index e49a6cc..f3a5dcd 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)) @@ -161,6 +165,14 @@ def create_or_update(attributes = {}) if !is_empty?(attributes[:cio_id]) customer_id = "cio_" + attributes[:cio_id] end + create_or_update_customer_id(customer_id, attributes) + end + + def create_or_update_customer_id(customer_id, attributes = {}) + attributes = Hash[attributes.map { |(k,v)| [ k.to_sym, v ] }] + if is_empty?(customer_id) + raise MissingIdAttributeError.new("Must provide a customer 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 41df3cd..b59f9a0 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -225,6 +225,50 @@ def json(data) end end + describe "#identify_customer_id" do + it "uses provided id rather than id" do + stub_request(:put, api_uri('/api/v1/customers/1234')). + with(body: json(id: "5")). + to_return(status: 200, body: "", headers: {}) + + client.identify_customer_id( + customer_id: "1234", + id: "5" + ) + end + + it "uses provided cio_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 + + it "uses provided email rather than id" do + stub_request(:put, api_uri('/api/v1/customers/customer@example.com')). + with(body: json(id: "5")). + to_return(status: 200, body: "", headers: {}) + + client.identify_customer_id( + customer_id: "customer@example.com", + id: "5" + ) + end + + it "requires a customer_id attribute" do + lambda { client.identify_customer_id() }.should raise_error(Customerio::Client::MissingIdAttributeError) + lambda { client.identify_customer_id(customer_id: "") }.should raise_error(Customerio::Client::MissingIdAttributeError) + # None of these are customer_id + lambda { client.identify_customer_id(id: "5") }.should raise_error(Customerio::Client::MissingIdAttributeError) + lambda { client.identify_customer_id(cio_id: "cio_5") }.should raise_error(Customerio::Client::MissingIdAttributeError) + lambda { client.identify_customer_id(email: "customer@example.com") }.should raise_error(Customerio::Client::MissingIdAttributeError) + end + end + describe "#delete" do it "sends a DELETE request to the customer.io's event API" do stub_request(:delete, api_uri('/api/v1/customers/5')). @@ -650,7 +694,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)