From b1d3425c42745de619593bfa84e905c21fb41659 Mon Sep 17 00:00:00 2001 From: Andy Dymond Date: Wed, 30 Oct 2024 18:27:36 -0600 Subject: [PATCH] Add support for Lists API (#272) * Add list commands * Add list item commands && update castle required files * update specs and add list_item/count and list/all commands * Add list & list_item apis and tests * Add client actions for list and list item apis * Pluralize list & list item namespaces and add client specs for added actions * Rename lists/all to lists/get_all * Update lib/castle/client_actions/list_items.rb Co-authored-by: Igor Pstyga * update client action calls to have option signature of apis called --------- Co-authored-by: Igor Pstyga --- lib/castle.rb | 28 ++++++++++ lib/castle/api/list_items/archive.rb | 21 ++++++++ lib/castle/api/list_items/count.rb | 21 ++++++++ lib/castle/api/list_items/create.rb | 21 ++++++++ lib/castle/api/list_items/get.rb | 21 ++++++++ lib/castle/api/list_items/query.rb | 21 ++++++++ lib/castle/api/list_items/unarchive.rb | 21 ++++++++ lib/castle/api/list_items/update.rb | 21 ++++++++ lib/castle/api/lists/create.rb | 21 ++++++++ lib/castle/api/lists/delete.rb | 21 ++++++++ lib/castle/api/lists/get.rb | 21 ++++++++ lib/castle/api/lists/get_all.rb | 21 ++++++++ lib/castle/api/lists/query.rb | 21 ++++++++ lib/castle/api/lists/update.rb | 21 ++++++++ lib/castle/client.rb | 3 ++ lib/castle/client_actions/list_items.rb | 34 +++++++++++++ lib/castle/client_actions/lists.rb | 30 +++++++++++ lib/castle/commands/list_items/archive.rb | 22 ++++++++ lib/castle/commands/list_items/count.rb | 22 ++++++++ lib/castle/commands/list_items/create.rb | 21 ++++++++ lib/castle/commands/list_items/get.rb | 22 ++++++++ lib/castle/commands/list_items/query.rb | 23 +++++++++ lib/castle/commands/list_items/unarchive.rb | 22 ++++++++ lib/castle/commands/list_items/update.rb | 21 ++++++++ lib/castle/commands/lists/create.rb | 19 +++++++ lib/castle/commands/lists/delete.rb | 19 +++++++ lib/castle/commands/lists/get.rb | 19 +++++++ lib/castle/commands/lists/get_all.rb | 16 ++++++ lib/castle/commands/lists/query.rb | 20 ++++++++ lib/castle/commands/lists/update.rb | 21 ++++++++ .../lib/castle/api/list_items/archive_spec.rb | 18 +++++++ spec/lib/castle/api/list_items/count_spec.rb | 22 ++++++++ spec/lib/castle/api/list_items/create_spec.rb | 22 ++++++++ spec/lib/castle/api/list_items/get_spec.rb | 18 +++++++ spec/lib/castle/api/list_items/query_spec.rb | 22 ++++++++ .../castle/api/list_items/unarchive_spec.rb | 18 +++++++ spec/lib/castle/api/list_items/update_spec.rb | 21 ++++++++ spec/lib/castle/api/lists/create_spec.rb | 22 ++++++++ spec/lib/castle/api/lists/delete_spec.rb | 18 +++++++ spec/lib/castle/api/lists/get_all_spec.rb | 18 +++++++ spec/lib/castle/api/lists/get_spec.rb | 18 +++++++ spec/lib/castle/api/lists/query_spec.rb | 22 ++++++++ spec/lib/castle/api/lists/update_spec.rb | 22 ++++++++ spec/lib/castle/client_spec.rb | 5 ++ .../commands/list_items/archive_spec.rb | 21 ++++++++ .../castle/commands/list_items/count_spec.rb | 21 ++++++++ .../castle/commands/list_items/create_spec.rb | 22 ++++++++ .../castle/commands/list_items/get_spec.rb | 21 ++++++++ .../castle/commands/list_items/query_spec.rb | 27 ++++++++++ .../commands/list_items/unarchive_spec.rb | 21 ++++++++ .../castle/commands/list_items/update_spec.rb | 21 ++++++++ spec/lib/castle/commands/lists/create_spec.rb | 33 ++++++++++++ spec/lib/castle/commands/lists/delete_spec.rb | 21 ++++++++ .../lib/castle/commands/lists/get_all_spec.rb | 11 ++++ spec/lib/castle/commands/lists/get_spec.rb | 21 ++++++++ spec/lib/castle/commands/lists/query_spec.rb | 27 ++++++++++ spec/lib/castle/commands/lists/update_spec.rb | 29 +++++++++++ spec/support/shared_examples/list_items.rb | 51 +++++++++++++++++++ spec/support/shared_examples/lists.rb | 44 ++++++++++++++++ 59 files changed, 1292 insertions(+) create mode 100644 lib/castle/api/list_items/archive.rb create mode 100644 lib/castle/api/list_items/count.rb create mode 100644 lib/castle/api/list_items/create.rb create mode 100644 lib/castle/api/list_items/get.rb create mode 100644 lib/castle/api/list_items/query.rb create mode 100644 lib/castle/api/list_items/unarchive.rb create mode 100644 lib/castle/api/list_items/update.rb create mode 100644 lib/castle/api/lists/create.rb create mode 100644 lib/castle/api/lists/delete.rb create mode 100644 lib/castle/api/lists/get.rb create mode 100644 lib/castle/api/lists/get_all.rb create mode 100644 lib/castle/api/lists/query.rb create mode 100644 lib/castle/api/lists/update.rb create mode 100644 lib/castle/client_actions/list_items.rb create mode 100644 lib/castle/client_actions/lists.rb create mode 100644 lib/castle/commands/list_items/archive.rb create mode 100644 lib/castle/commands/list_items/count.rb create mode 100644 lib/castle/commands/list_items/create.rb create mode 100644 lib/castle/commands/list_items/get.rb create mode 100644 lib/castle/commands/list_items/query.rb create mode 100644 lib/castle/commands/list_items/unarchive.rb create mode 100644 lib/castle/commands/list_items/update.rb create mode 100644 lib/castle/commands/lists/create.rb create mode 100644 lib/castle/commands/lists/delete.rb create mode 100644 lib/castle/commands/lists/get.rb create mode 100644 lib/castle/commands/lists/get_all.rb create mode 100644 lib/castle/commands/lists/query.rb create mode 100644 lib/castle/commands/lists/update.rb create mode 100644 spec/lib/castle/api/list_items/archive_spec.rb create mode 100644 spec/lib/castle/api/list_items/count_spec.rb create mode 100644 spec/lib/castle/api/list_items/create_spec.rb create mode 100644 spec/lib/castle/api/list_items/get_spec.rb create mode 100644 spec/lib/castle/api/list_items/query_spec.rb create mode 100644 spec/lib/castle/api/list_items/unarchive_spec.rb create mode 100644 spec/lib/castle/api/list_items/update_spec.rb create mode 100644 spec/lib/castle/api/lists/create_spec.rb create mode 100644 spec/lib/castle/api/lists/delete_spec.rb create mode 100644 spec/lib/castle/api/lists/get_all_spec.rb create mode 100644 spec/lib/castle/api/lists/get_spec.rb create mode 100644 spec/lib/castle/api/lists/query_spec.rb create mode 100644 spec/lib/castle/api/lists/update_spec.rb create mode 100644 spec/lib/castle/commands/list_items/archive_spec.rb create mode 100644 spec/lib/castle/commands/list_items/count_spec.rb create mode 100644 spec/lib/castle/commands/list_items/create_spec.rb create mode 100644 spec/lib/castle/commands/list_items/get_spec.rb create mode 100644 spec/lib/castle/commands/list_items/query_spec.rb create mode 100644 spec/lib/castle/commands/list_items/unarchive_spec.rb create mode 100644 spec/lib/castle/commands/list_items/update_spec.rb create mode 100644 spec/lib/castle/commands/lists/create_spec.rb create mode 100644 spec/lib/castle/commands/lists/delete_spec.rb create mode 100644 spec/lib/castle/commands/lists/get_all_spec.rb create mode 100644 spec/lib/castle/commands/lists/get_spec.rb create mode 100644 spec/lib/castle/commands/lists/query_spec.rb create mode 100644 spec/lib/castle/commands/lists/update_spec.rb create mode 100644 spec/support/shared_examples/list_items.rb create mode 100644 spec/support/shared_examples/lists.rb diff --git a/lib/castle.rb b/lib/castle.rb index 89a09fb..4b2c389 100644 --- a/lib/castle.rb +++ b/lib/castle.rb @@ -31,6 +31,19 @@ castle/commands/risk castle/commands/start_impersonation castle/commands/track + castle/commands/lists/get_all + castle/commands/lists/create + castle/commands/lists/delete + castle/commands/lists/get + castle/commands/lists/query + castle/commands/lists/update + castle/commands/list_items/archive + castle/commands/list_items/create + castle/commands/list_items/count + castle/commands/list_items/get + castle/commands/list_items/query + castle/commands/list_items/unarchive + castle/commands/list_items/update castle/api/approve_device castle/api/authenticate castle/api/end_impersonation @@ -42,12 +55,27 @@ castle/api/risk castle/api/start_impersonation castle/api/track + castle/api/lists/get_all + castle/api/lists/create + castle/api/lists/delete + castle/api/lists/get + castle/api/lists/query + castle/api/lists/update + castle/api/list_items/archive + castle/api/list_items/create + castle/api/list_items/count + castle/api/list_items/get + castle/api/list_items/query + castle/api/list_items/unarchive + castle/api/list_items/update castle/payload/prepare castle/configuration castle/singleton_configuration castle/logger castle/failover/prepare_response castle/failover/strategy + castle/client_actions/lists + castle/client_actions/list_items castle/client castle/headers/filter castle/headers/format diff --git a/lib/castle/api/list_items/archive.rb b/lib/castle/api/list_items/archive.rb new file mode 100644 index 0000000..38bf24b --- /dev/null +++ b/lib/castle/api/list_items/archive.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Castle + module API + module ListItems + module Archive + class << self + # @param options [Hash] + # return [Hash] + def call(options = {}) + options = Castle::Utils::DeepSymbolizeKeys.call(options || {}) + http = options.delete(:http) + config = options.delete(:config) || Castle.config + + Castle::API.call(Castle::Commands::ListItems::Archive.build(options), {}, http, config) + end + end + end + end + end +end diff --git a/lib/castle/api/list_items/count.rb b/lib/castle/api/list_items/count.rb new file mode 100644 index 0000000..cb33fbf --- /dev/null +++ b/lib/castle/api/list_items/count.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Castle + module API + module ListItems + module Count + class << self + # @param options [Hash] + # return [Hash] + def call(options = {}) + options = Castle::Utils::DeepSymbolizeKeys.call(options || {}) + http = options.delete(:http) + config = options.delete(:config) || Castle.config + + Castle::API.call(Castle::Commands::ListItems::Count.build(options), {}, http, config) + end + end + end + end + end +end diff --git a/lib/castle/api/list_items/create.rb b/lib/castle/api/list_items/create.rb new file mode 100644 index 0000000..f95e8e5 --- /dev/null +++ b/lib/castle/api/list_items/create.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Castle + module API + module ListItems + module Create + class << self + # @param options [Hash] + # return [Hash] + def call(options = {}) + options = Castle::Utils::DeepSymbolizeKeys.call(options || {}) + http = options.delete(:http) + config = options.delete(:config) || Castle.config + + Castle::API.call(Castle::Commands::ListItems::Create.build(options), {}, http, config) + end + end + end + end + end +end diff --git a/lib/castle/api/list_items/get.rb b/lib/castle/api/list_items/get.rb new file mode 100644 index 0000000..e31e8d7 --- /dev/null +++ b/lib/castle/api/list_items/get.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Castle + module API + module ListItems + module Get + class << self + # @param options [Hash] + # return [Hash] + def call(options = {}) + options = Castle::Utils::DeepSymbolizeKeys.call(options || {}) + http = options.delete(:http) + config = options.delete(:config) || Castle.config + + Castle::API.call(Castle::Commands::ListItems::Get.build(options), {}, http, config) + end + end + end + end + end +end diff --git a/lib/castle/api/list_items/query.rb b/lib/castle/api/list_items/query.rb new file mode 100644 index 0000000..f7c71c0 --- /dev/null +++ b/lib/castle/api/list_items/query.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Castle + module API + module ListItems + module Query + class << self + # @param options [Hash] + # return [Hash] + def call(options = {}) + options = Castle::Utils::DeepSymbolizeKeys.call(options || {}) + http = options.delete(:http) + config = options.delete(:config) || Castle.config + + Castle::API.call(Castle::Commands::ListItems::Query.build(options), {}, http, config) + end + end + end + end + end +end diff --git a/lib/castle/api/list_items/unarchive.rb b/lib/castle/api/list_items/unarchive.rb new file mode 100644 index 0000000..67e742a --- /dev/null +++ b/lib/castle/api/list_items/unarchive.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Castle + module API + module ListItems + module Unarchive + class << self + # @param options [Hash] + # return [Hash] + def call(options = {}) + options = Castle::Utils::DeepSymbolizeKeys.call(options || {}) + http = options.delete(:http) + config = options.delete(:config) || Castle.config + + Castle::API.call(Castle::Commands::ListItems::Unarchive.build(options), {}, http, config) + end + end + end + end + end +end diff --git a/lib/castle/api/list_items/update.rb b/lib/castle/api/list_items/update.rb new file mode 100644 index 0000000..9702c9e --- /dev/null +++ b/lib/castle/api/list_items/update.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Castle + module API + module ListItems + module Update + class << self + # @param options [Hash] + # return [Hash] + def call(options = {}) + options = Castle::Utils::DeepSymbolizeKeys.call(options || {}) + http = options.delete(:http) + config = options.delete(:config) || Castle.config + + Castle::API.call(Castle::Commands::ListItems::Update.build(options), {}, http, config) + end + end + end + end + end +end diff --git a/lib/castle/api/lists/create.rb b/lib/castle/api/lists/create.rb new file mode 100644 index 0000000..da76179 --- /dev/null +++ b/lib/castle/api/lists/create.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Castle + module API + module Lists + module Create + class << self + # @param options [Hash] + # return [Hash] + def call(options = {}) + options = Castle::Utils::DeepSymbolizeKeys.call(options || {}) + http = options.delete(:http) + config = options.delete(:config) || Castle.config + + Castle::API.call(Castle::Commands::Lists::Create.build(options), {}, http, config) + end + end + end + end + end +end diff --git a/lib/castle/api/lists/delete.rb b/lib/castle/api/lists/delete.rb new file mode 100644 index 0000000..af67fee --- /dev/null +++ b/lib/castle/api/lists/delete.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Castle + module API + module Lists + module Delete + class << self + # @param options [Hash] + # return [Hash] + def call(options = {}) + options = Castle::Utils::DeepSymbolizeKeys.call(options || {}) + http = options.delete(:http) + config = options.delete(:config) || Castle.config + + Castle::API.call(Castle::Commands::Lists::Delete.build(options), {}, http, config) + end + end + end + end + end +end diff --git a/lib/castle/api/lists/get.rb b/lib/castle/api/lists/get.rb new file mode 100644 index 0000000..6c5a7b7 --- /dev/null +++ b/lib/castle/api/lists/get.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Castle + module API + module Lists + module Get + class << self + # @param options [Hash] + # return [Hash] + def call(options = {}) + options = Castle::Utils::DeepSymbolizeKeys.call(options || {}) + http = options.delete(:http) + config = options.delete(:config) || Castle.config + + Castle::API.call(Castle::Commands::Lists::Get.build(options), {}, http, config) + end + end + end + end + end +end diff --git a/lib/castle/api/lists/get_all.rb b/lib/castle/api/lists/get_all.rb new file mode 100644 index 0000000..698273d --- /dev/null +++ b/lib/castle/api/lists/get_all.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Castle + module API + module Lists + module GetAll + class << self + # @param options [Hash] + # return [Hash] + def call(options = {}) + options = Castle::Utils::DeepSymbolizeKeys.call(options || {}) + http = options.delete(:http) + config = options.delete(:config) || Castle.config + + Castle::API.call(Castle::Commands::Lists::GetAll.build, {}, http, config) + end + end + end + end + end +end diff --git a/lib/castle/api/lists/query.rb b/lib/castle/api/lists/query.rb new file mode 100644 index 0000000..09fef2c --- /dev/null +++ b/lib/castle/api/lists/query.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Castle + module API + module Lists + module Query + class << self + # @param options [Hash] + # return [Hash] + def call(options = {}) + options = Castle::Utils::DeepSymbolizeKeys.call(options || {}) + http = options.delete(:http) + config = options.delete(:config) || Castle.config + + Castle::API.call(Castle::Commands::Lists::Query.build(options), {}, http, config) + end + end + end + end + end +end diff --git a/lib/castle/api/lists/update.rb b/lib/castle/api/lists/update.rb new file mode 100644 index 0000000..dae7a40 --- /dev/null +++ b/lib/castle/api/lists/update.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Castle + module API + module Lists + module Update + class << self + # @param options [Hash] + # return [Hash] + def call(options = {}) + options = Castle::Utils::DeepSymbolizeKeys.call(options || {}) + http = options.delete(:http) + config = options.delete(:config) || Castle.config + + Castle::API.call(Castle::Commands::Lists::Update.build(options), {}, http, config) + end + end + end + end + end +end diff --git a/lib/castle/client.rb b/lib/castle/client.rb index 887d7a6..e9c2d6a 100644 --- a/lib/castle/client.rb +++ b/lib/castle/client.rb @@ -3,6 +3,9 @@ module Castle # Castle's client. class Client + include Castle::ClientActions::ListItems + include Castle::ClientActions::Lists + class << self def from_request(request, options = {}) new(options.merge(context: Castle::Context::Prepare.call(request, options))) diff --git a/lib/castle/client_actions/list_items.rb b/lib/castle/client_actions/list_items.rb new file mode 100644 index 0000000..87f2edc --- /dev/null +++ b/lib/castle/client_actions/list_items.rb @@ -0,0 +1,34 @@ +# frozen_string_literal: true +module Castle + module ClientActions + module ListItems + def archive_list_item(options = {}) + Castle::API::ListItems::Archive.call(options) + end + + def count_list_items(options = {}) + Castle::API::ListItems::Count.call(options) + end + + def create_list_item(options = {}) + Castle::API::ListItems::Create.call(options) + end + + def get_list_item(options = {}) + Castle::API::ListItems::Get.call(options) + end + + def query_list_items(options = {}) + Castle::API::ListItems::Query.call(options) + end + + def unarchive_list_item(options) + Castle::API::ListItems::Unarchive.call(options) + end + + def update_list_item(options = {}) + Castle::API::ListItems::Update.call(options) + end + end + end +end diff --git a/lib/castle/client_actions/lists.rb b/lib/castle/client_actions/lists.rb new file mode 100644 index 0000000..a2adb70 --- /dev/null +++ b/lib/castle/client_actions/lists.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true +module Castle + module ClientActions + module Lists + def create_list(options = {}) + Castle::API::Lists::Create.call(options) + end + + def delete_list(options = {}) + Castle::API::Lists::Delete.call(options) + end + + def get_all_lists(options = {}) + Castle::API::Lists::GetAll.call(options) + end + + def get_list(options = {}) + Castle::API::Lists::Get.call(options) + end + + def query_lists(options = {}) + Castle::API::Lists::Query.call(options) + end + + def update_list(options = {}) + Castle::API::Lists::Update.call(options) + end + end + end +end diff --git a/lib/castle/commands/list_items/archive.rb b/lib/castle/commands/list_items/archive.rb new file mode 100644 index 0000000..f5ad040 --- /dev/null +++ b/lib/castle/commands/list_items/archive.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Castle + module Commands + module ListItems + class Archive + class << self + # @param options [Hash] + # @return [Castle::Command] + def build(options = {}) + Castle::Validators::Present.call(options, %i[list_id list_item_id]) + + list_id = options.delete(:list_id) + list_item_id = options.delete(:list_item_id) + + Castle::Command.new("lists/#{list_id}/items/#{list_item_id}/archive", nil, :delete) + end + end + end + end + end +end diff --git a/lib/castle/commands/list_items/count.rb b/lib/castle/commands/list_items/count.rb new file mode 100644 index 0000000..00c15ac --- /dev/null +++ b/lib/castle/commands/list_items/count.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Castle + module Commands + module ListItems + class Count + class << self + # @param options [Hash] + # @return [Castle::Command] + def build(options = {}) + Castle::Validators::Present.call(options, %i[list_id]) + options[:filters]&.each { |f| Castle::Validators::Present.call(f, %i[field op value]) } + + list_id = options.delete(:list_id) + + Castle::Command.new("lists/#{list_id}/items/count", options, :post) + end + end + end + end + end +end diff --git a/lib/castle/commands/list_items/create.rb b/lib/castle/commands/list_items/create.rb new file mode 100644 index 0000000..be00cd8 --- /dev/null +++ b/lib/castle/commands/list_items/create.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Castle + module Commands + module ListItems + class Create + class << self + # @param options [Hash] + # @return [Castle::Command] + def build(options = {}) + Castle::Validators::Present.call(options, %i[list_id author primary_value]) + + list_id = options.delete(:list_id) + + Castle::Command.new("lists/#{list_id}/items", options, :post) + end + end + end + end + end +end diff --git a/lib/castle/commands/list_items/get.rb b/lib/castle/commands/list_items/get.rb new file mode 100644 index 0000000..b301670 --- /dev/null +++ b/lib/castle/commands/list_items/get.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Castle + module Commands + module ListItems + class Get + class << self + # @param options [Hash] + # @return [Castle::Command] + def build(options = {}) + Castle::Validators::Present.call(options, %i[list_id list_item_id]) + + list_id = options.delete(:list_id) + list_item_id = options.delete(:list_item_id) + + Castle::Command.new("lists/#{list_id}/items/#{list_item_id}", nil, :get) + end + end + end + end + end +end diff --git a/lib/castle/commands/list_items/query.rb b/lib/castle/commands/list_items/query.rb new file mode 100644 index 0000000..94a392a --- /dev/null +++ b/lib/castle/commands/list_items/query.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module Castle + module Commands + module ListItems + class Query + class << self + # @param options [Hash] + # @return [Castle::Command] + def build(options = {}) + Castle::Validators::Present.call(options, %i[list_id]) + options[:filters]&.each { |f| Castle::Validators::Present.call(f, %i[field op value]) } + Castle::Validators::Present.call(options[:sort], %i[field order]) if options[:sort] + + list_id = options.delete(:list_id) + + Castle::Command.new("lists/#{list_id}/items/query", options, :post) + end + end + end + end + end +end diff --git a/lib/castle/commands/list_items/unarchive.rb b/lib/castle/commands/list_items/unarchive.rb new file mode 100644 index 0000000..699882f --- /dev/null +++ b/lib/castle/commands/list_items/unarchive.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Castle + module Commands + module ListItems + class Unarchive + class << self + # @param options [Hash] + # @return [Castle::Command] + def build(options = {}) + Castle::Validators::Present.call(options, %i[list_id list_item_id]) + + list_id = options.delete(:list_id) + list_item_id = options.delete(:list_item_id) + + Castle::Command.new("lists/#{list_id}/items/#{list_item_id}/unarchive", nil, :put) + end + end + end + end + end +end diff --git a/lib/castle/commands/list_items/update.rb b/lib/castle/commands/list_items/update.rb new file mode 100644 index 0000000..2e3b13d --- /dev/null +++ b/lib/castle/commands/list_items/update.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Castle + module Commands + module ListItems + class Update + class << self + # @param options [Hash] + # @return [Castle::Command] + def build(options = {}) + Castle::Validators::Present.call(options, %i[list_id list_item_id comment]) + list_id = options.delete(:list_id) + list_item_id = options.delete(:list_item_id) + + Castle::Command.new("lists/#{list_id}/items/#{list_item_id}", options, :put) + end + end + end + end + end +end diff --git a/lib/castle/commands/lists/create.rb b/lib/castle/commands/lists/create.rb new file mode 100644 index 0000000..a2d43e5 --- /dev/null +++ b/lib/castle/commands/lists/create.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Castle + module Commands + module Lists + class Create + class << self + # @param options [Hash] + # @return [Castle::Command] + def build(options = {}) + Castle::Validators::Present.call(options, %i[name color primary_field]) + + Castle::Command.new("lists", options, :post) + end + end + end + end + end +end diff --git a/lib/castle/commands/lists/delete.rb b/lib/castle/commands/lists/delete.rb new file mode 100644 index 0000000..b9c8d5f --- /dev/null +++ b/lib/castle/commands/lists/delete.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Castle + module Commands + module Lists + class Delete + class << self + # @param options [Hash] + # @return [Castle::Command] + def build(options = {}) + Castle::Validators::Present.call(options, %i[list_id]) + + Castle::Command.new("lists/#{options[:list_id]}", nil, :delete) + end + end + end + end + end +end diff --git a/lib/castle/commands/lists/get.rb b/lib/castle/commands/lists/get.rb new file mode 100644 index 0000000..cd219e1 --- /dev/null +++ b/lib/castle/commands/lists/get.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Castle + module Commands + module Lists + class Get + class << self + # @param options [Hash] + # @return [Castle::Command] + def build(options = {}) + Castle::Validators::Present.call(options, %i[list_id]) + + Castle::Command.new("lists/#{options[:list_id]}", nil, :get) + end + end + end + end + end +end diff --git a/lib/castle/commands/lists/get_all.rb b/lib/castle/commands/lists/get_all.rb new file mode 100644 index 0000000..1fbbbd5 --- /dev/null +++ b/lib/castle/commands/lists/get_all.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module Castle + module Commands + module Lists + class GetAll + class << self + # @return [Castle::Command] + def build + Castle::Command.new("lists", nil, :get) + end + end + end + end + end +end diff --git a/lib/castle/commands/lists/query.rb b/lib/castle/commands/lists/query.rb new file mode 100644 index 0000000..415357d --- /dev/null +++ b/lib/castle/commands/lists/query.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module Castle + module Commands + module Lists + class Query + class << self + # @param options [Hash] + # @return [Castle::Command] + def build(options = {}) + options[:filters]&.each { |f| Castle::Validators::Present.call(f, %i[field op value]) } + Castle::Validators::Present.call(options[:sort], %i[field order]) if options[:sort] + + Castle::Command.new("lists/query", options, :post) + end + end + end + end + end +end diff --git a/lib/castle/commands/lists/update.rb b/lib/castle/commands/lists/update.rb new file mode 100644 index 0000000..8efe252 --- /dev/null +++ b/lib/castle/commands/lists/update.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Castle + module Commands + module Lists + class Update + class << self + # @param options [Hash] + # @return [Castle::Command] + def build(options = {}) + Castle::Validators::Present.call(options, %i[list_id]) + + list_id = options.delete(:list_id) + + Castle::Command.new("lists/#{list_id}", options, :put) + end + end + end + end + end +end diff --git a/spec/lib/castle/api/list_items/archive_spec.rb b/spec/lib/castle/api/list_items/archive_spec.rb new file mode 100644 index 0000000..1892b01 --- /dev/null +++ b/spec/lib/castle/api/list_items/archive_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +describe Castle::API::ListItems::Archive do + before do + stub_request(:any, /api.castle.io/).with(basic_auth: ['', 'secret']).to_return(status: 200, body: '{}', headers: {}) + end + + describe '.call' do + subject(:all) { described_class.call(options) } + + let(:url) { "https://api.castle.io/v1/lists/#{options[:list_id]}/items/#{options[:list_item_id]}/archive" } + let(:options) { { list_id: '123', list_item_id: '456' } } + + before { all } + + it { assert_requested :delete, url, times: 1 } + end +end diff --git a/spec/lib/castle/api/list_items/count_spec.rb b/spec/lib/castle/api/list_items/count_spec.rb new file mode 100644 index 0000000..773c99c --- /dev/null +++ b/spec/lib/castle/api/list_items/count_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +describe Castle::API::ListItems::Count do + before do + stub_request(:any, /api.castle.io/).with(basic_auth: ['', 'secret']).to_return(status: 200, body: '{}', headers: {}) + end + + describe '.call' do + subject(:all) { described_class.call(options) } + + let(:options) { { list_id: '123', filters: [{ field: 'test', op: '$eq', value: 'test' }] } } + + + before { all } + + it do + assert_requested :post, "https://api.castle.io/v1/lists/#{options[:list_id]}/items/count", times: 1 do |req| + expect(JSON.parse(req.body, symbolize_names: true)).to eq(options.except(:list_id)) + end + end + end +end diff --git a/spec/lib/castle/api/list_items/create_spec.rb b/spec/lib/castle/api/list_items/create_spec.rb new file mode 100644 index 0000000..497f791 --- /dev/null +++ b/spec/lib/castle/api/list_items/create_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +describe Castle::API::ListItems::Create do + before do + stub_request(:any, /api.castle.io/).with(basic_auth: ['', 'secret']).to_return(status: 200, body: '{}', headers: {}) + end + + describe '.call' do + subject(:all) { described_class.call(options) } + + let(:author) { { type: '$other', identifier: 'test identifier' } } + let(:options) { { list_id: '123', primary_value: 'test value', author: author, } } + + before { all } + + it do + assert_requested :post, "https://api.castle.io/v1/lists/#{options[:list_id]}/items", times: 1 do |req| + expect(JSON.parse(req.body, symbolize_names: true)).to eq(options.except(:list_id)) + end + end + end +end diff --git a/spec/lib/castle/api/list_items/get_spec.rb b/spec/lib/castle/api/list_items/get_spec.rb new file mode 100644 index 0000000..b9b8eae --- /dev/null +++ b/spec/lib/castle/api/list_items/get_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +describe Castle::API::ListItems::Get do + before do + stub_request(:any, /api.castle.io/).with(basic_auth: ['', 'secret']).to_return(status: 200, body: '{}', headers: {}) + end + + describe '.call' do + subject(:all) { described_class.call(options) } + + let(:options) { { list_id: '123', list_item_id: '456' } } + let(:url) { "https://api.castle.io/v1/lists/#{options[:list_id]}/items/#{options[:list_item_id]}" } + + before { all } + + it { assert_requested :get, url, times: 1 } + end +end diff --git a/spec/lib/castle/api/list_items/query_spec.rb b/spec/lib/castle/api/list_items/query_spec.rb new file mode 100644 index 0000000..c16be4c --- /dev/null +++ b/spec/lib/castle/api/list_items/query_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +describe Castle::API::ListItems::Query do + before do + stub_request(:any, /api.castle.io/).with(basic_auth: ['', 'secret']).to_return(status: 200, body: '{}', headers: {}) + end + + describe '.call' do + subject(:all) { described_class.call(options) } + + let(:options) { { list_id: '123', filters: [{ field: 'test', op: '$eq', value: 'test' }] } } + + + before { all } + + it do + assert_requested :post, "https://api.castle.io/v1/lists/#{options[:list_id]}/items/query", times: 1 do |req| + expect(JSON.parse(req.body, symbolize_names: true)).to eq(options.except(:list_id)) + end + end + end +end diff --git a/spec/lib/castle/api/list_items/unarchive_spec.rb b/spec/lib/castle/api/list_items/unarchive_spec.rb new file mode 100644 index 0000000..fa75b60 --- /dev/null +++ b/spec/lib/castle/api/list_items/unarchive_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +describe Castle::API::ListItems::Unarchive do + before do + stub_request(:any, /api.castle.io/).with(basic_auth: ['', 'secret']).to_return(status: 200, body: '{}', headers: {}) + end + + describe '.call' do + subject(:all) { described_class.call(options) } + + let(:url) { "https://api.castle.io/v1/lists/#{options[:list_id]}/items/#{options[:list_item_id]}/unarchive" } + let(:options) { { list_id: '123', list_item_id: '456' } } + + before { all } + + it { assert_requested :put, url, times: 1 } + end +end diff --git a/spec/lib/castle/api/list_items/update_spec.rb b/spec/lib/castle/api/list_items/update_spec.rb new file mode 100644 index 0000000..eb063e6 --- /dev/null +++ b/spec/lib/castle/api/list_items/update_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +describe Castle::API::ListItems::Update do + before do + stub_request(:any, /api.castle.io/).with(basic_auth: ['', 'secret']).to_return(status: 200, body: '{}', headers: {}) + end + + describe '.call' do + let(:url) { "https://api.castle.io/v1/lists/#{options[:list_id]}/items/#{options[:list_item_id]}" } + let(:options) { { list_id: '123', list_item_id: '456', comment: 'updating comment!' } } + subject(:all) { described_class.call(options) } + + before { all } + + it do + assert_requested :put, url, times: 1 do |req| + expect(JSON.parse(req.body, symbolize_names: true)).to eq(options.except(:list_id, :list_item_id)) + end + end + end +end diff --git a/spec/lib/castle/api/lists/create_spec.rb b/spec/lib/castle/api/lists/create_spec.rb new file mode 100644 index 0000000..eb918aa --- /dev/null +++ b/spec/lib/castle/api/lists/create_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +describe Castle::API::Lists::Create do + before do + stub_request(:any, /api.castle.io/).with(basic_auth: ['', 'secret']).to_return(status: 200, body: '{}', headers: {}) + end + + describe '.call' do + subject(:all) { described_class.call(options) } + + let(:options) { { name: 'name', color: '$red', primary_field: 'user.email' } } + + + before { all } + + it do + assert_requested :post, 'https://api.castle.io/v1/lists', times: 1 do |req| + expect(JSON.parse(req.body, symbolize_names: true)).to eq(options) + end + end + end +end diff --git a/spec/lib/castle/api/lists/delete_spec.rb b/spec/lib/castle/api/lists/delete_spec.rb new file mode 100644 index 0000000..872db4b --- /dev/null +++ b/spec/lib/castle/api/lists/delete_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +describe Castle::API::Lists::Delete do + before do + stub_request(:any, /api.castle.io/).with(basic_auth: ['', 'secret']).to_return(status: 200, body: '{}', headers: {}) + end + + describe '.call' do + subject(:all) { described_class.call(options) } + + let(:options) { { list_id: '123' } } + + + before { all } + + it { assert_requested :delete, "https://api.castle.io/v1/lists/#{options[:list_id]}", times: 1 } + end +end diff --git a/spec/lib/castle/api/lists/get_all_spec.rb b/spec/lib/castle/api/lists/get_all_spec.rb new file mode 100644 index 0000000..cdf7393 --- /dev/null +++ b/spec/lib/castle/api/lists/get_all_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +describe Castle::API::Lists::GetAll do + before do + stub_request(:any, /api.castle.io/).with(basic_auth: ['', 'secret']).to_return(status: 200, body: '{}', headers: {}) + end + + describe '.call' do + subject(:all) { described_class.call(options) } + + let(:options) { { } } + + + before { all } + + it { assert_requested :get, "https://api.castle.io/v1/lists", times: 1 } + end +end diff --git a/spec/lib/castle/api/lists/get_spec.rb b/spec/lib/castle/api/lists/get_spec.rb new file mode 100644 index 0000000..26aa1fb --- /dev/null +++ b/spec/lib/castle/api/lists/get_spec.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +describe Castle::API::Lists::Get do + before do + stub_request(:any, /api.castle.io/).with(basic_auth: ['', 'secret']).to_return(status: 200, body: '{}', headers: {}) + end + + describe '.call' do + subject(:all) { described_class.call(options) } + + let(:options) { { list_id: '123' } } + + + before { all } + + it { assert_requested :get, "https://api.castle.io/v1/lists/#{options[:list_id]}", times: 1 } + end +end diff --git a/spec/lib/castle/api/lists/query_spec.rb b/spec/lib/castle/api/lists/query_spec.rb new file mode 100644 index 0000000..5371f31 --- /dev/null +++ b/spec/lib/castle/api/lists/query_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +describe Castle::API::Lists::Query do + before do + stub_request(:any, /api.castle.io/).with(basic_auth: ['', 'secret']).to_return(status: 200, body: '{}', headers: {}) + end + + describe '.call' do + subject(:all) { described_class.call(options) } + + let(:options) { { filters: [{ field: 'test', op: '$eq', value: 'test' }] } } + + + before { all } + + it do + assert_requested :post, "https://api.castle.io/v1/lists/query", times: 1 do |req| + expect(JSON.parse(req.body, symbolize_names: true)).to eq(options) + end + end + end +end diff --git a/spec/lib/castle/api/lists/update_spec.rb b/spec/lib/castle/api/lists/update_spec.rb new file mode 100644 index 0000000..a75ce28 --- /dev/null +++ b/spec/lib/castle/api/lists/update_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +describe Castle::API::Lists::Update do + before do + stub_request(:any, /api.castle.io/).with(basic_auth: ['', 'secret']).to_return(status: 200, body: '{}', headers: {}) + end + + describe '.call' do + subject(:all) { described_class.call(options) } + + let(:options) { { list_id: '123', name: 'name', color: '$red' } } + + + before { all } + + it do + assert_requested :put, "https://api.castle.io/v1/lists/#{options[:list_id]}", times: 1 do |req| + expect(JSON.parse(req.body, symbolize_names: true)).to eq(options.except(:list_id)) + end + end + end +end diff --git a/spec/lib/castle/client_spec.rb b/spec/lib/castle/client_spec.rb index e10650e..bf98eb2 100644 --- a/spec/lib/castle/client_spec.rb +++ b/spec/lib/castle/client_spec.rb @@ -334,4 +334,9 @@ describe 'log' do it_behaves_like 'action request', :log end + + describe 'client action mixins' do + it_behaves_like 'it has list actions' + it_behaves_like 'it has list item actions' + end end diff --git a/spec/lib/castle/commands/list_items/archive_spec.rb b/spec/lib/castle/commands/list_items/archive_spec.rb new file mode 100644 index 0000000..3bdb0e2 --- /dev/null +++ b/spec/lib/castle/commands/list_items/archive_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +describe Castle::Commands::ListItems::Archive do + describe '.build' do + subject(:command) { described_class.build(options) } + + context 'with valid options' do + let(:options) { { list_id: 'list_id', list_item_id: 'list_item_id'} } + + it { expect(command.method).to be(:delete) } + it { expect(command.path).to eql('lists/list_id/items/list_item_id/archive') } + it { expect(command.data).to be_nil } + end + + context 'with invalid options' do + let(:options) { { list_id: 'list_id' } } + + it { expect { command }.to raise_error(Castle::InvalidParametersError) } + end + end +end diff --git a/spec/lib/castle/commands/list_items/count_spec.rb b/spec/lib/castle/commands/list_items/count_spec.rb new file mode 100644 index 0000000..8cd661d --- /dev/null +++ b/spec/lib/castle/commands/list_items/count_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +describe Castle::Commands::ListItems::Count do + describe '.build' do + subject(:command) { described_class.build(options) } + + context 'with valid options' do + let(:options) { { list_id: 'list_id', filters: [{ field: 'primary_value', op: '$eq', value: 'test'}]} } + + it { expect(command.method).to be(:post) } + it { expect(command.path).to eql('lists/list_id/items/count') } + it { expect(command.data).to eql(options.except(:list_id)) } + end + + context 'with invalid filters' do + let(:options) { { list_id: 'list_id', filters: [{ field: 'primary_field', op: '$eq' }]} } + + it { expect { command }.to raise_error(Castle::InvalidParametersError) } + end + end +end diff --git a/spec/lib/castle/commands/list_items/create_spec.rb b/spec/lib/castle/commands/list_items/create_spec.rb new file mode 100644 index 0000000..0a9d7f0 --- /dev/null +++ b/spec/lib/castle/commands/list_items/create_spec.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +describe Castle::Commands::ListItems::Create do + describe '.build' do + subject(:command) { described_class.build(options) } + + context 'with valid options' do + let(:author) { { type: '$other', identifier: 'some value' } } + let(:options) { { list_id: 'list_id', author: author, primary_value: 'some value' } } + + it { expect(command.method).to be(:post) } + it { expect(command.path).to eql('lists/list_id/items') } + it { expect(command.data).to eql(options.except(:list_id)) } + end + + context 'with invalid options' do + let(:options) { { list_id: 'list_id' } } + + it { expect { command }.to raise_error(Castle::InvalidParametersError) } + end + end +end diff --git a/spec/lib/castle/commands/list_items/get_spec.rb b/spec/lib/castle/commands/list_items/get_spec.rb new file mode 100644 index 0000000..7a8ea40 --- /dev/null +++ b/spec/lib/castle/commands/list_items/get_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +describe Castle::Commands::ListItems::Get do + describe '.build' do + subject(:command) { described_class.build(options) } + + context 'with valid options' do + let(:options) { { list_id: 'list_id', list_item_id: 'list_item_id'} } + + it { expect(command.method).to be(:get) } + it { expect(command.path).to eql('lists/list_id/items/list_item_id') } + it { expect(command.data).to be_nil } + end + + context 'with invalid options' do + let(:options) { { list_id: 'list_id' } } + + it { expect { command }.to raise_error(Castle::InvalidParametersError) } + end + end +end diff --git a/spec/lib/castle/commands/list_items/query_spec.rb b/spec/lib/castle/commands/list_items/query_spec.rb new file mode 100644 index 0000000..01ae9d4 --- /dev/null +++ b/spec/lib/castle/commands/list_items/query_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +describe Castle::Commands::ListItems::Query do + describe '.build' do + subject(:command) { described_class.build(options) } + + context 'with valid options' do + let(:options) { { list_id: 'list_id', filters: [{ field: 'primary_value', op: '$eq', value: 'test'}]} } + + it { expect(command.method).to be(:post) } + it { expect(command.path).to eql('lists/list_id/items/query') } + it { expect(command.data).to eql(options.except(:list_id)) } + end + + context 'with invalid filters' do + let(:options) { { list_id: 'list_id', filters: [{ field: 'primary_field', op: '$eq' }]} } + + it { expect { command }.to raise_error(Castle::InvalidParametersError) } + end + + context 'with no list_id' do + let(:options) { { filters: [{ field: 'primary_field', op: '$eq', value: 'test' }]} } + + it { expect { command }.to raise_error(Castle::InvalidParametersError) } + end + end +end diff --git a/spec/lib/castle/commands/list_items/unarchive_spec.rb b/spec/lib/castle/commands/list_items/unarchive_spec.rb new file mode 100644 index 0000000..ed4fe37 --- /dev/null +++ b/spec/lib/castle/commands/list_items/unarchive_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +describe Castle::Commands::ListItems::Unarchive do + describe '.build' do + subject(:command) { described_class.build(options) } + + context 'with valid options' do + let(:options) { { list_id: 'list_id', list_item_id: 'list_item_id'} } + + it { expect(command.method).to be(:put) } + it { expect(command.path).to eql('lists/list_id/items/list_item_id/unarchive') } + it { expect(command.data).to be_nil } + end + + context 'with invalid options' do + let(:options) { { list_id: 'list_id' } } + + it { expect { command }.to raise_error(Castle::InvalidParametersError) } + end + end +end diff --git a/spec/lib/castle/commands/list_items/update_spec.rb b/spec/lib/castle/commands/list_items/update_spec.rb new file mode 100644 index 0000000..0ad56ae --- /dev/null +++ b/spec/lib/castle/commands/list_items/update_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +describe Castle::Commands::ListItems::Update do + describe '.build' do + subject(:command) { described_class.build(options) } + + context 'with valid options' do + let(:options) { { list_id: 'list_id', list_item_id: 'list_item_id', comment: 'updating item' } } + + it { expect(command.method).to be(:put) } + it { expect(command.path).to eql('lists/list_id/items/list_item_id') } + it { expect(command.data).to eql(options.except(:list_id, :list_item_id)) } + end + + context 'with invalid options' do + let(:options) { { list_id: 'list_id', list_item_id: 'list_item_id' } } + + it { expect { command }.to raise_error(Castle::InvalidParametersError) } + end + end +end diff --git a/spec/lib/castle/commands/lists/create_spec.rb b/spec/lib/castle/commands/lists/create_spec.rb new file mode 100644 index 0000000..2cf18b9 --- /dev/null +++ b/spec/lib/castle/commands/lists/create_spec.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +describe Castle::Commands::Lists::Create do + describe '.build' do + subject(:command) { described_class.build(options) } + + let(:options) { { name: 'list_name', color: '$green', primary_field: 'user.email' } } + + context 'with valid options' do + it { expect(command.method).to be(:post) } + it { expect(command.path).to eql('lists') } + it { expect(command.data).to eql(options) } + end + + context 'without name' do + let(:options) { { color: '$green', primary_field: 'user.email' } } + + it { expect { command }.to raise_error(Castle::InvalidParametersError) } + end + + context 'without color' do + let(:options) { { name: 'list_name', primary_field: 'user.email' } } + + it { expect { command }.to raise_error(Castle::InvalidParametersError) } + end + + context 'without primary_field' do + let(:options) { { name: 'list_name', color: '$green' } } + + it { expect { command }.to raise_error(Castle::InvalidParametersError) } + end + end +end diff --git a/spec/lib/castle/commands/lists/delete_spec.rb b/spec/lib/castle/commands/lists/delete_spec.rb new file mode 100644 index 0000000..72986c0 --- /dev/null +++ b/spec/lib/castle/commands/lists/delete_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +describe Castle::Commands::Lists::Delete do + describe '.build' do + subject(:command) { described_class.build(options) } + + let(:options) { { list_id: 'list_id' } } + + context 'with valid options' do + it { expect(command.method).to be(:delete) } + it { expect(command.path).to eql('lists/list_id') } + it { expect(command.data).to be_nil } + end + + context 'without list_id' do + let(:options) { {} } + + it { expect { command }.to raise_error(Castle::InvalidParametersError) } + end + end +end diff --git a/spec/lib/castle/commands/lists/get_all_spec.rb b/spec/lib/castle/commands/lists/get_all_spec.rb new file mode 100644 index 0000000..96caabc --- /dev/null +++ b/spec/lib/castle/commands/lists/get_all_spec.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +describe Castle::Commands::Lists::GetAll do + describe '.build' do + subject(:command) { described_class.build } + + it { expect(command.method).to be(:get) } + it { expect(command.path).to eql('lists') } + it { expect(command.data).to be_nil } + end +end diff --git a/spec/lib/castle/commands/lists/get_spec.rb b/spec/lib/castle/commands/lists/get_spec.rb new file mode 100644 index 0000000..17e2ca0 --- /dev/null +++ b/spec/lib/castle/commands/lists/get_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +describe Castle::Commands::Lists::Get do + describe '.build' do + subject(:command) { described_class.build(options) } + + let(:options) { { list_id: 'list_id' } } + + context 'with valid options' do + it { expect(command.method).to be(:get) } + it { expect(command.path).to eql('lists/list_id') } + it { expect(command.data).to be_nil } + end + + context 'without list_id' do + let(:options) { {} } + + it { expect { command }.to raise_error(Castle::InvalidParametersError) } + end + end +end diff --git a/spec/lib/castle/commands/lists/query_spec.rb b/spec/lib/castle/commands/lists/query_spec.rb new file mode 100644 index 0000000..f78645d --- /dev/null +++ b/spec/lib/castle/commands/lists/query_spec.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +describe Castle::Commands::Lists::Query do + describe '.build' do + subject(:command) { described_class.build(options) } + + context 'with valid options' do + let(:options) { { filters: [{ field: 'primary_field', op: '$eq', value: 'test' }]} } + + it { expect(command.method).to be(:post) } + it { expect(command.path).to eql('lists/query') } + it { expect(command.data).to eq(options.except(:list_id)) } + end + + context 'with invalid filters' do + let(:options) { { filters: [{ field: 'primary_field', op: '$eq' }]} } + + it { expect { command }.to raise_error(Castle::InvalidParametersError) } + end + + context 'with invalid sort' do + let(:options) { { sort: { field: 'primary_field' } } } + + it { expect { command }.to raise_error(Castle::InvalidParametersError) } + end + end +end diff --git a/spec/lib/castle/commands/lists/update_spec.rb b/spec/lib/castle/commands/lists/update_spec.rb new file mode 100644 index 0000000..7f4a607 --- /dev/null +++ b/spec/lib/castle/commands/lists/update_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +describe Castle::Commands::Lists::Update do + describe '.build' do + subject(:command) { described_class.build(options) } + + context 'with valid options' do + let(:options) do + { + list_id: 'list_id', + name: 'name', + description: 'description', + color: '$yellow', + default_item_archivation_time: 6000 + } + end + + it { expect(command.method).to be(:put) } + it { expect(command.path).to eql('lists/list_id') } + it { expect(command.data).to eql(options.except(:list_id)) } + end + + context 'without list_id' do + let(:options) { { name: 'name' } } + + it { expect { command }.to raise_error(Castle::InvalidParametersError) } + end + end +end diff --git a/spec/support/shared_examples/list_items.rb b/spec/support/shared_examples/list_items.rb new file mode 100644 index 0000000..f6ada76 --- /dev/null +++ b/spec/support/shared_examples/list_items.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true +RSpec.shared_examples 'it has list item actions' do + describe 'archive_list_item' do + it do + client.archive_list_item(list_id: '1234', list_item_id: '5678') + assert_requested :delete, 'https://api.castle.io/v1/lists/1234/items/5678/archive', times: 1 + end + end + + describe 'count_list_items' do + it do + client.count_list_items(list_id: '1234', filters: [{ field: 'email', op: 'eq', value: 'test' }]) + assert_requested :post, 'https://api.castle.io/v1/lists/1234/items/count', times: 1 + end + end + + describe 'create_list_item' do + it do + client.create_list_item(list_id: '1234', author: { type: 'other', identifier: '1234' }, primary_value: 'email') + assert_requested :post, 'https://api.castle.io/v1/lists/1234/items', times: 1 + end + end + + describe 'get_list_item' do + it do + client.get_list_item(list_id: '1234', list_item_id: '5678') + assert_requested :get, 'https://api.castle.io/v1/lists/1234/items/5678', times: 1 + end + end + + describe 'query_list_items' do + it do + client.query_list_items(list_id: '1234', filters: [{ field: 'email', op: 'eq', value: 'test' }]) + assert_requested :post, 'https://api.castle.io/v1/lists/1234/items/query', times: 1 + end + end + + describe 'unarchive_list_item' do + it do + client.unarchive_list_item(list_id: '1234', list_item_id: '5678') + assert_requested :put, 'https://api.castle.io/v1/lists/1234/items/5678/unarchive', times: 1 + end + end + + describe 'update_list_item' do + it do + client.update_list_item(list_id: '1234', list_item_id: '5678', comment: 'updating comment') + assert_requested :put, 'https://api.castle.io/v1/lists/1234/items/5678', times: 1 + end + end +end diff --git a/spec/support/shared_examples/lists.rb b/spec/support/shared_examples/lists.rb new file mode 100644 index 0000000..43d061f --- /dev/null +++ b/spec/support/shared_examples/lists.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true +RSpec.shared_examples 'it has list actions' do + describe 'create_list' do + it do + client.create_list(name: 'list name', color: '$green', primary_field: 'user.email') + assert_requested :post, 'https://api.castle.io/v1/lists', times: 1 + end + end + + describe 'delete_list' do + it do + client.delete_list(list_id: 'list_id') + assert_requested :delete, 'https://api.castle.io/v1/lists/list_id', times: 1 + end + end + + describe 'get_all_lists' do + it do + client.get_all_lists + assert_requested :get, 'https://api.castle.io/v1/lists', times: 1 + end + end + + describe 'get_list' do + it do + client.get_list(list_id: 'list_id') + assert_requested :get, 'https://api.castle.io/v1/lists/list_id', times: 1 + end + end + + describe 'query_lists' do + it do + client.query_lists(filters: [{ field: 'user.email', op: 'eq', value: 'test' }]) + assert_requested :post, 'https://api.castle.io/v1/lists/query', times: 1 + end + end + + describe 'update_list' do + it do + client.update_list(list_id: 'list_id', name: 'list name', color: '$green', primary_field: 'user.email') + assert_requested :put, 'https://api.castle.io/v1/lists/list_id', times: 1 + end + end +end