Skip to content

Commit

Permalink
Support for deleting a person and all their data
Browse files Browse the repository at this point in the history
  • Loading branch information
kindjar committed May 10, 2018
1 parent 50bbca3 commit e339068
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ survey_responses_page1 = Delighted::Bounce.all
survey_responses_page2 = Delighted::Bounce.all(:page => 2)
```

Deleting a person and all of the data associated with them:

```ruby
# Delete by person id
Delighted::Person.delete(:id => 42)
# Delete by email address
Delighted::Person.delete(:email => "test@example.com")
# Delete by phone number
Delighted::Person.delete(:phone_number => "+14155551212")
```

Deleting pending survey requests

```ruby
Expand Down
1 change: 1 addition & 0 deletions lib/delighted.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
require 'delighted/operations/create'
require 'delighted/operations/retrieve'
require 'delighted/operations/update'
require 'delighted/operations/delete'

require 'delighted/resources/bounce'
require 'delighted/resources/metrics'
Expand Down
20 changes: 20 additions & 0 deletions lib/delighted/operations/delete.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module Delighted
module Operations
module Delete
def self.included(klass)
klass.extend(ClassMethods)
end

module ClassMethods
def path(id = nil)
id ? "#{@path}/#{id}" : @path
end

def delete(id_hash, client = Delighted.shared_client)
id = identifier_string(id_hash)
client.delete_json(path(id))
end
end
end
end
end
14 changes: 14 additions & 0 deletions lib/delighted/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ def expandable_attributes
def singleton_resource?
!!@singleton_resource
end

def identifier_string(id_hash)
raise ArgumentError, "must pass Hash" unless Hash === id_hash

id_key = id_hash.keys.detect { |k| !id_hash[k].to_s.empty? }
raise ArgumentError, "must pass an identifier name and value" unless id_key
id_value = id_hash[id_key]

if id_key.to_s == "id"
id_value
else
"#{id_key}:#{id_value}"
end
end
end

undef :id if method_defined?(:id)
Expand Down
1 change: 1 addition & 0 deletions lib/delighted/resources/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ class Person < Resource
self.path = "/people"

include Operations::Create
include Operations::Delete
end
end
33 changes: 33 additions & 0 deletions test/delighted_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,39 @@ def test_deleting_pending_survey_requests_for_a_person
assert_kind_of Hash, result
assert_equal({ :ok => true }, result)
end

def test_deleting_a_person_by_id
uri = URI.parse("https://api.delightedapp.com/v1/people/57")
headers = { 'Authorization' => @auth_header, "Accept" => "application/json", 'Content-Type' => 'application/json', 'User-Agent' => "Delighted RubyGem #{Delighted::VERSION}" }
response = Delighted::HTTPResponse.new(202, {}, Delighted::JSON.dump({ :ok => true }))
mock_http_adapter.expects(:request).with(:delete, uri, headers, nil).once.returns(response)

result = Delighted::Person.delete(:id => 57)
assert_kind_of Hash, result
assert_equal({ :ok => true }, result)
end

def test_deleting_a_person_by_email
uri = URI.parse("https://api.delightedapp.com/v1/people/email:foo@bar.com")
headers = { 'Authorization' => @auth_header, "Accept" => "application/json", 'Content-Type' => 'application/json', 'User-Agent' => "Delighted RubyGem #{Delighted::VERSION}" }
response = Delighted::HTTPResponse.new(202, {}, Delighted::JSON.dump({ :ok => true }))
mock_http_adapter.expects(:request).with(:delete, uri, headers, nil).once.returns(response)

result = Delighted::Person.delete(:email => "foo@bar.com")
assert_kind_of Hash, result
assert_equal({ :ok => true }, result)
end

def test_deleting_a_person_by_phone_number
uri = URI.parse("https://api.delightedapp.com/v1/people/phone_number:+14155551212")
headers = { 'Authorization' => @auth_header, "Accept" => "application/json", 'Content-Type' => 'application/json', 'User-Agent' => "Delighted RubyGem #{Delighted::VERSION}" }
response = Delighted::HTTPResponse.new(202, {}, Delighted::JSON.dump({ :ok => true }))
mock_http_adapter.expects(:request).with(:delete, uri, headers, nil).once.returns(response)

result = Delighted::Person.delete(:phone_number => "+14155551212")
assert_kind_of Hash, result
assert_equal({ :ok => true }, result)
end
end

class Delighted::SurveyResponseTest < Delighted::TestCase
Expand Down

0 comments on commit e339068

Please sign in to comment.