Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customer_id to be specified in identify URL #102

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 10 additions & 2 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 @@ -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

Expand Down
8 changes: 8 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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: {})

client.identify_customer_id(customer_id: "cio_5", id: "5")
end
end

describe "#delete" do
Expand Down