Skip to content

Commit 036113d

Browse files
authored
add time_zone support for user upsert (#14)
1 parent fcc6b2a commit 036113d

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

lib/embed_workflow/base.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22

33
module EmbedWorkflow
44
module Base
5+
UNSET = Object.new.freeze
6+
57
attr_accessor :skey
68

79
class << self
810
attr_accessor :skey
911
end
12+
13+
private
14+
15+
def prepare_params(hash)
16+
hash.reject { |_, v| v == UNSET }
17+
end
1018
end
1119
end

lib/embed_workflow/users.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ class << self
1111

1212
RESOURCE_BASE_PATH = "#{BASE_API_PATH}/users".freeze
1313

14-
def upsert(key:, name: nil, email: nil, data: nil, groups: nil)
15-
attrs = {
14+
def upsert(key:, name: UNSET, email: UNSET, data: UNSET, groups: UNSET, time_zone: UNSET)
15+
attrs = prepare_params({
1616
key: key,
1717
name: name,
1818
email: email,
1919
data: data,
20-
groups: groups
21-
}.compact
20+
groups: groups,
21+
time_zone: time_zone
22+
})
2223

2324
put_request(
2425
path: "#{RESOURCE_BASE_PATH}/#{key}",

spec/users_spec.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191
key: "api-user-1",
9292
name: "Jane Doe",
9393
email: "jane@example.com",
94+
time_zone: "America/New_York",
95+
groups: ["admin", "users"],
9496
data: data
9597
}
9698
})
@@ -103,9 +105,61 @@
103105
key: "api-user-1",
104106
name: "Jane Doe",
105107
email: "jane@example.com",
108+
time_zone: "America/New_York",
109+
groups: ["admin", "users"],
106110
data: data
107111
)
108112
end
109113
end
114+
115+
context "with nil values to unset fields" do
116+
before do
117+
allow_any_instance_of(EmbedWorkflow::Client)
118+
.to receive(:put_request)
119+
.with({
120+
path: "/api/v1/users/api-user-1",
121+
body: {
122+
key: "api-user-1",
123+
time_zone: nil,
124+
groups: nil
125+
}
126+
})
127+
.and_return("response")
128+
end
129+
130+
it "sends nil values to unset time_zone and groups" do
131+
EmbedWorkflow::Users.upsert(
132+
key: "api-user-1",
133+
time_zone: nil,
134+
groups: nil
135+
)
136+
end
137+
end
138+
139+
context "with mixed nil and provided values" do
140+
before do
141+
allow_any_instance_of(EmbedWorkflow::Client)
142+
.to receive(:put_request)
143+
.with({
144+
path: "/api/v1/users/api-user-1",
145+
body: {
146+
key: "api-user-1",
147+
name: "Jane Doe",
148+
email: "jane@example.com",
149+
groups: nil
150+
}
151+
})
152+
.and_return("response")
153+
end
154+
155+
it "sends both set and unset values correctly" do
156+
EmbedWorkflow::Users.upsert(
157+
key: "api-user-1",
158+
name: "Jane Doe",
159+
email: "jane@example.com",
160+
groups: nil
161+
)
162+
end
163+
end
110164
end
111165
end

0 commit comments

Comments
 (0)