From 386b8d389bdbab2a7b6a0ebb69a04a97ad4a389b Mon Sep 17 00:00:00 2001 From: David Amrani Date: Fri, 7 Mar 2025 10:18:29 -0500 Subject: [PATCH] pagination cleanup --- README.md | 21 ++++++++++-- lib/embed_workflow/workflows.rb | 11 +++++-- spec/users_spec.rb | 6 ++-- spec/workflows_spec.rb | 57 ++++++++++++++++++++++++++++++++- 4 files changed, 87 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 09a4773..43f110f 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,16 @@ require "embed_workflow" EmbedWorkflow.skey = "sk_live_REPLACE_ME" ``` +## Pagination + +The EmbedWorkflow API uses cursor-based pagination for list endpoints. The following pagination parameters are supported: + +- `starting_after`: Returns objects after this cursor position (exclusive) +- `ending_before`: Returns objects before this cursor position (exclusive) +- `limit`: Maximum number of objects to return (defaults to 25 if not specified) + +Both cursor parameters take an object ID as their value. The response will include the data array containing the requested objects, along with pagination metadata. + ## Usage ### Create Workflow @@ -58,7 +68,14 @@ EmbedWorkflow::Workflows.fetch(hashid: "nybra") ### List Workflows ```ruby +# Default pagination (25 items) EmbedWorkflow::Workflows.list + +# With pagination parameters +EmbedWorkflow::Workflows.list(starting_after: "550e8400-e29b-41d4-a716-446655440000", limit: 10) + +# Filter by user +EmbedWorkflow::Workflows.list(user_key: "api-user-1") ``` ### Update Workflow @@ -132,10 +149,10 @@ EmbedWorkflow::Users.fetch(key: "api-user-1") ### List users ```ruby -# List all users +# Default pagination (25 items) EmbedWorkflow::Users.list -# List with pagination +# With pagination parameters EmbedWorkflow::Users.list(starting_after: "550e8400-e29b-41d4-a716-446655440000", limit: 10) ``` diff --git a/lib/embed_workflow/workflows.rb b/lib/embed_workflow/workflows.rb index aa1f58e..c690beb 100644 --- a/lib/embed_workflow/workflows.rb +++ b/lib/embed_workflow/workflows.rb @@ -45,10 +45,17 @@ def update(hashid:, name: nil, template: nil, user_key: nil) ) end - def list(user_key: nil, starting_after: nil, ending_before: nil) + def list(user_key: nil, starting_after: nil, ending_before: nil, limit: nil) + params = { + user_key: user_key, + starting_after: starting_after, + ending_before: ending_before, + limit: limit + }.compact + get_request( path: RESOURCE_BASE_PATH, - params: { user_key: user_key, starting_after: starting_after, ending_before: ending_before }.compact + params: params ) end diff --git a/spec/users_spec.rb b/spec/users_spec.rb index 9f70620..793e460 100644 --- a/spec/users_spec.rb +++ b/spec/users_spec.rb @@ -68,7 +68,7 @@ before do allow_any_instance_of(EmbedWorkflow::Client) .to receive(:put_request) - .with({ + .with({ path: "/api/v1/users/api-user-1", body: { key: "api-user-1" } }) @@ -85,9 +85,9 @@ config = { user_data: { foo: "bar" } } allow_any_instance_of(EmbedWorkflow::Client) .to receive(:put_request) - .with({ + .with({ path: "/api/v1/users/api-user-1", - body: { + body: { key: "api-user-1", name: "Jane Doe", email: "jane@example.com", diff --git a/spec/workflows_spec.rb b/spec/workflows_spec.rb index e31374a..0551ae3 100644 --- a/spec/workflows_spec.rb +++ b/spec/workflows_spec.rb @@ -1,9 +1,64 @@ # frozen_string_literal: true require_relative "../lib/embed_workflow" +require "net/http" describe "workflows" do before do - EmbedWorkflow.skey = "sk_live_REPLACE_ME" + 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/workflows", params: {} }) + .and_return("response") + end + + it "sends the correct parameters to the workflows API" do + EmbedWorkflow::Workflows.list + end + + context "with pagination parameters" do + before do + allow_any_instance_of(EmbedWorkflow::Client) + .to receive(:get_request) + .with({ + path: "/api/v1/workflows", + params: { + starting_after: "550e8400-e29b-41d4-a716-446655440000", + limit: 10 + } + }) + .and_return("response") + end + + it "sends the correct pagination parameters" do + EmbedWorkflow::Workflows.list( + starting_after: "550e8400-e29b-41d4-a716-446655440000", + limit: 10 + ) + end + end + + context "with user_key parameter" do + before do + allow_any_instance_of(EmbedWorkflow::Client) + .to receive(:get_request) + .with({ + path: "/api/v1/workflows", + params: { user_key: "api-user-1" } + }) + .and_return("response") + end + + it "sends the correct user_key parameter" do + EmbedWorkflow::Workflows.list(user_key: "api-user-1") + end + end end end