Skip to content

Commit ac45b7e

Browse files
committed
Adds methods for updating, replace & deleting user identities
1 parent 39a0cdc commit ac45b7e

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

lib/layer/resources/user.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,33 @@ def messages
1818
end
1919

2020
def create_identity(params)
21-
client.post("#{url}/identity", body: params.to_json)
21+
client.post(identity_url, body: params.to_json)
2222
end
2323

2424
def identity
25-
client.get("#{url}/identity")
25+
client.get(identity_url)
26+
end
27+
28+
def update_identity(params)
29+
client.patch(
30+
identity_url,
31+
body: params.to_json,
32+
headers: client.layer_patch_header
33+
)
34+
end
35+
36+
def replace_identity(params)
37+
client.put(identity_url, body: params.to_json)
38+
end
39+
40+
def destroy_identity
41+
client.delete(identity_url)
42+
end
43+
44+
private
45+
46+
def identity_url
47+
"#{url}/identity"
2648
end
2749
end
2850
end

spec/layer/resources/user_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,44 @@
8181
expect(identity).to be_instance_of(Hash)
8282
end
8383
end
84+
85+
describe "#update_identity" do
86+
let(:patch_header) { { 'Content-Type' => 'application/vnd.layer-patch+json' } }
87+
88+
it "should update the users identity" do
89+
allow(http_client).to receive(:patch).and_return("")
90+
allow(http_client).to receive(:layer_patch_header).and_return(patch_header)
91+
92+
expect(http_client).to receive(:patch).
93+
with(
94+
"users/#{user.id}/identity",
95+
body: user_identity_operations.to_json,
96+
headers: patch_header
97+
)
98+
99+
user.update_identity(user_identity_operations)
100+
end
101+
end
102+
103+
describe "#replace_identity" do
104+
it "should replace the users identity" do
105+
allow(http_client).to receive(:put).and_return("")
106+
expect(http_client).to receive(:put).
107+
with(
108+
"users/#{user.id}/identity",
109+
body: user_identity_params.to_json
110+
)
111+
112+
user.replace_identity(user_identity_params)
113+
end
114+
end
115+
116+
describe "#delete_identity" do
117+
it "should make a request to delete the users identity" do
118+
allow(http_client).to receive(:delete).and_return("")
119+
expect(http_client).to receive(:delete).with("users/#{user.id}/identity")
120+
121+
user.destroy_identity
122+
end
123+
end
84124
end

spec/layer_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,12 @@ def user_identity_params
8383
}
8484
}
8585
end
86+
87+
def user_identity_operations
88+
[
89+
{operation: "set", property: "last_name", value: "Dodo"},
90+
{operation: "set", property: "phone_number", value: ""},
91+
{operation: "set", property: "metadata.level", value: "2"}
92+
]
93+
end
8694
end

0 commit comments

Comments
 (0)