Skip to content

Commit

Permalink
Move the support for :customer_id into the base identify method
Browse files Browse the repository at this point in the history
  • Loading branch information
richdawe-cio committed Dec 8, 2023
1 parent 6362c80 commit a1f7832
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 34 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ You can also use this method to make other updates to the person using the `cio_
### Updating customers: Using email address

If you need to identify a person using their email address, then you can do so
by using the `identify_customer_id` method. This allows you to specify
by passing in a customer ID to the `identify` method. This allows you to specify
a customer ID that is different than the one used in the `id` attribute. E.g.:

```ruby
Expand All @@ -131,17 +131,16 @@ a customer ID that is different than the one used in the `id` attribute. E.g.:
# 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(
$customerio.identify(
:customer_id => "[email protected]",
:id => 5,
:email => "[email protected]",
:created_at => customer.created_at.to_i,
:first_name => "Bob",
:plan => "basic"
:location => "Australia"
)
```

Note: If you want to use the `cio_id` in the `customer_id` field of `identify_customer_id`, you will need to prefix it with `"cio_"`. E.g.: `"cio_f000000d"` for a `cio_id` of `f000000d`.
Note:

* If you want to use the `cio_id` in the `customer_id` field of `identify_customer_id`, you will need to prefix it with `"cio_"`. E.g.: `"cio_f000000d"` for a `cio_id` of `f000000d`.
* The `identify` method can identify the person using one of `customer_id`, `cio_id` or `id`. The order of precedence is `customer_id` > `cio_id` > `id`.

### Deleting customers

Expand Down
29 changes: 17 additions & 12 deletions lib/customerio/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ 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 @@ -155,24 +151,33 @@ def merge_customers_path

def create_or_update(attributes = {})
attributes = Hash[attributes.map { |(k,v)| [ k.to_sym, v ] }]
if is_empty?(attributes[:id]) && is_empty?(attributes[:cio_id])
if is_empty?(attributes[:id]) && is_empty?(attributes[:cio_id]) && is_empty?(attributes[:customer_id])
raise MissingIdAttributeError.new("Must provide a customer id")
end

# Use cio_id as the identifier, if present,
# to allow the id and email identifiers to be updated.
# The person is identified by a customer ID, which is included
# in the path to the Track v1 API. Choose the ID in this order
# from highest to lowest precedence:
#
# 1. customer_id: "id", an email address, or "cio_id" value.
# Any "cio_id" values need to be prefixed "cio_"
# so that the Track v1 API knows it's a cio_id.
#
# 2. cio_id: The cio_id value (no prefix required).
#
# 3. id: The id value.
customer_id = attributes[:id]
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")
if !is_empty?(attributes[:customer_id])
customer_id = attributes[:customer_id]
end
# customer_id is not an attribute, so remove it.
attributes.delete(:customer_id)

url = customer_path(customer_id)
@client.request_and_verify_response(:put, url, attributes)
end
Expand Down
18 changes: 4 additions & 14 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ def json(data)
lambda { client.identify(email: "[email protected]") }.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)
lambda { client.identify(customer_id: "") }.should raise_error(Customerio::Client::MissingIdAttributeError)
end

it 'should not raise errors when attribute keys are strings' do
Expand Down Expand Up @@ -223,15 +224,13 @@ def json(data)
location: "here"
})
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(
client.identify(
customer_id: "1234",
id: "5"
)
Expand All @@ -242,7 +241,7 @@ def json(data)
with(body: json(id: "5")).
to_return(status: 200, body: "", headers: {})

client.identify_customer_id(
client.identify(
customer_id: "cio_5",
id: "5"
)
Expand All @@ -253,20 +252,11 @@ def json(data)
with(body: json(id: "5")).
to_return(status: 200, body: "", headers: {})

client.identify_customer_id(
client.identify(
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
Expand Down

0 comments on commit a1f7832

Please sign in to comment.