Skip to content

Commit 21ffad6

Browse files
committed
Merge pull request #13 from cakejelly/user-identities
Adds methods for managing user identities
2 parents 861010b + ac45b7e commit 21ffad6

File tree

5 files changed

+127
-6
lines changed

5 files changed

+127
-6
lines changed

lib/layer/resources/user.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,36 @@ def conversations
1616
def messages
1717
Layer::ResourceProxy.new(client, self, Layer::Resources::Message)
1818
end
19+
20+
def create_identity(params)
21+
client.post(identity_url, body: params.to_json)
22+
end
23+
24+
def 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"
48+
end
1949
end
2050
end
2151
end

spec/layer/resources/rich_content_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require "spec_helper"
2-
require "webmock/rspec"
32

43
describe Layer::Resources::RichContent do
54
let(:client) { Layer::Platform::Client.new.client }

spec/layer/resources/user_spec.rb

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22

33
describe Layer::Resources::User do
44
let(:client) { Layer::Platform::Client.new }
5+
let(:http_client) { instance_double("Layer::HttpClient") }
6+
let(:user) { client.users.find("some_user") }
7+
8+
before do
9+
allow(client).to receive(:client).and_return(http_client)
10+
end
511

612
describe ".find" do
713
it "should return instance of User" do
8-
user = client.users.find("jake")
914
expect(user).to be_instance_of(described_class)
1015
end
1116

1217
it "shouldn't send any request" do
1318
expect(Layer::HttpClient).to_not receive(:find)
14-
client.users.find("jake")
19+
user
1520
end
1621
end
1722

1823
describe "#blocks" do
1924
it "should instantiate new ResourceProxy for Block" do
20-
user = client.users.find("jake")
2125
blocks = user.blocks
2226
base = blocks.instance_variable_get("@base")
2327
resource = blocks.instance_variable_get("@resource")
@@ -30,7 +34,6 @@
3034

3135
describe "#conversations" do
3236
it "should instantiate new ResourceProxy for Conversation" do
33-
user = client.users.find("jake")
3437
conversations = user.conversations
3538
base = conversations.instance_variable_get("@base")
3639
resource = conversations.instance_variable_get("@resource")
@@ -43,7 +46,6 @@
4346

4447
describe "#messages" do
4548
it "should instantiate new ResourceProxy for Message" do
46-
user = client.users.find("jake")
4749
messages = user.messages
4850
base = messages.instance_variable_get("@base")
4951
resource = messages.instance_variable_get("@resource")
@@ -53,4 +55,70 @@
5355
expect(resource).to eq(Layer::Resources::Message)
5456
end
5557
end
58+
59+
describe "#create_identity" do
60+
it "should create identity for a user" do
61+
allow(http_client).to receive(:post).and_return("")
62+
expect(http_client).to receive(:post).
63+
with("users/#{user.id}/identity", body: user_identity_params.to_json)
64+
65+
user.create_identity(user_identity_params)
66+
end
67+
end
68+
69+
describe "#identity" do
70+
before do
71+
allow(http_client).to receive(:get).and_return(user_identity_params)
72+
expect(http_client).to receive(:get).with("users/#{user.id}/identity")
73+
end
74+
75+
it "should retrieve the users identity" do
76+
user.identity
77+
end
78+
79+
it "should return a hash containing the users identity" do
80+
identity = user.identity
81+
expect(identity).to be_instance_of(Hash)
82+
end
83+
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
56124
end

spec/layer_helper.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,27 @@ def webhook_params
6868
config: {:key1=>"value1", :key2=>"value2"}
6969
}
7070
end
71+
72+
def user_identity_params
73+
{
74+
display_name: "Frodo the Dodo",
75+
avatar_url: "http://sillylordoftheringspictures.com/frodo-riding-a-dodo.png",
76+
first_name: "Frodo",
77+
last_name: "Baggins",
78+
phone_number: "13791379137",
79+
email_address: "frodo@sillylordoftheringspictures.com",
80+
metadata: {
81+
level: "35",
82+
race: "Dodo"
83+
}
84+
}
85+
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
7194
end

spec/spec_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'layer/api'
33
require 'vcr'
44
require 'layer_helper'
5+
require "webmock/rspec"
56

67
VCR.configure do |c|
78
c.cassette_library_dir = "spec/cassettes"

0 commit comments

Comments
 (0)