From 3c32e2d9e267a79cdd2dc94d05438563c9ed650b Mon Sep 17 00:00:00 2001 From: David Amrani Date: Fri, 7 Mar 2025 09:49:28 -0500 Subject: [PATCH] add list and delete to users --- .gitignore | 6 +- README.md | 16 ++++++ lib/embed_workflow/users.rb | 17 ++++++ spec/users_spec.rb | 111 ++++++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 spec/users_spec.rb diff --git a/.gitignore b/.gitignore index e9224e1..c4bd92c 100644 --- a/.gitignore +++ b/.gitignore @@ -46,4 +46,8 @@ .rvmrc # Used by RuboCop. Remote config files pulled in from inherit_from directive. -# .rubocop-https?--* \ No newline at end of file +# .rubocop-https?--* +.env* +!.env.example + +/kb \ No newline at end of file diff --git a/README.md b/README.md index 741c7cb..09a4773 100644 --- a/README.md +++ b/README.md @@ -129,6 +129,22 @@ EmbedWorkflow::Users.upsert(name: "Jane Smith", id: user["id"], config: config) EmbedWorkflow::Users.fetch(key: "api-user-1") ``` +### List users + +```ruby +# List all users +EmbedWorkflow::Users.list + +# List with pagination +EmbedWorkflow::Users.list(starting_after: "550e8400-e29b-41d4-a716-446655440000", limit: 10) +``` + +### Delete a user + +```ruby +EmbedWorkflow::Users.delete(key: "api-user-1") +``` + ### Catch a webhook ```ruby diff --git a/lib/embed_workflow/users.rb b/lib/embed_workflow/users.rb index 9344a1d..df0a0ba 100644 --- a/lib/embed_workflow/users.rb +++ b/lib/embed_workflow/users.rb @@ -28,6 +28,23 @@ def upsert(key:, name: nil, email: nil, config: nil) def fetch(key:) get_request(path: "#{RESOURCE_BASE_PATH}/#{key}") end + + def list(starting_after: nil, ending_before: nil, limit: nil) + params = { + starting_after: starting_after, + ending_before: ending_before, + limit: limit + }.compact + + get_request( + path: RESOURCE_BASE_PATH, + params: params + ) + end + + def delete(key:) + delete_request(path: "#{RESOURCE_BASE_PATH}/#{key}") + end end end end diff --git a/spec/users_spec.rb b/spec/users_spec.rb new file mode 100644 index 0000000..9f70620 --- /dev/null +++ b/spec/users_spec.rb @@ -0,0 +1,111 @@ +# frozen_string_literal: true + +require_relative "../lib/embed_workflow" +require "net/http" + +describe "users" do + before do + allow_any_instance_of(EmbedWorkflow::Client) + .to receive(:execute_request) + .with(instance_of(Net::HTTPRequest)) + .and_return("response") + end + + describe "#list" do + before do + allow_any_instance_of(EmbedWorkflow::Client) + .to receive(:get_request) + .with({ path: "/api/v1/users", params: {} }) + .and_return("response") + end + + it "sends the correct parameters to the users API" do + EmbedWorkflow::Users.list + end + + context "with pagination parameters" do + before do + allow_any_instance_of(EmbedWorkflow::Client) + .to receive(:get_request) + .with({ path: "/api/v1/users", params: { starting_after: "550e8400-e29b-41d4-a716-446655440000", limit: 10 } }) + .and_return("response") + end + + it "sends the correct pagination parameters" do + EmbedWorkflow::Users.list(starting_after: "550e8400-e29b-41d4-a716-446655440000", limit: 10) + end + end + end + + describe "#fetch" do + before do + allow_any_instance_of(EmbedWorkflow::Client) + .to receive(:get_request) + .with({ path: "/api/v1/users/api-user-1" }) + .and_return("response") + end + + it "sends the correct parameters to the users API" do + EmbedWorkflow::Users.fetch(key: "api-user-1") + end + end + + describe "#delete" do + before do + allow_any_instance_of(EmbedWorkflow::Client) + .to receive(:delete_request) + .with({ path: "/api/v1/users/api-user-1" }) + .and_return("response") + end + + it "sends the correct parameters to the users API" do + EmbedWorkflow::Users.delete(key: "api-user-1") + end + end + + describe "#upsert" do + context "with minimum parameters" do + before do + allow_any_instance_of(EmbedWorkflow::Client) + .to receive(:put_request) + .with({ + path: "/api/v1/users/api-user-1", + body: { key: "api-user-1" } + }) + .and_return("response") + end + + it "sends the correct parameters to the users API" do + EmbedWorkflow::Users.upsert(key: "api-user-1") + end + end + + context "with all parameters" do + before do + config = { user_data: { foo: "bar" } } + allow_any_instance_of(EmbedWorkflow::Client) + .to receive(:put_request) + .with({ + path: "/api/v1/users/api-user-1", + body: { + key: "api-user-1", + name: "Jane Doe", + email: "jane@example.com", + config: config + } + }) + .and_return("response") + end + + it "sends the correct parameters to the users API" do + config = { user_data: { foo: "bar" } } + EmbedWorkflow::Users.upsert( + key: "api-user-1", + name: "Jane Doe", + email: "jane@example.com", + config: config + ) + end + end + end +end