Skip to content

Commit

Permalink
Allow customer_id to be specified in identify URL (based on work by j…
Browse files Browse the repository at this point in the history
…rbeck)
  • Loading branch information
richdawe-cio committed Dec 8, 2023
1 parent 16e0afe commit 6292726
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 => "[email protected]",
:id => 5,
:email => "[email protected]",
: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,
Expand Down
12 changes: 12 additions & 0 deletions lib/customerio/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
46 changes: 45 additions & 1 deletion spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]')).
with(body: json(id: "5")).
to_return(status: 200, body: "", headers: {})

client.identify_customer_id(
customer_id: "[email protected]",
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: "[email protected]") }.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')).
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6292726

Please sign in to comment.