Skip to content

Commit

Permalink
Merge pull request #1398 from operable/nmohoric/count_command
Browse files Browse the repository at this point in the history
Add operable:count command
  • Loading branch information
davejlong authored Sep 15, 2017
2 parents 79e9f02 + fc216a0 commit 01cf420
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ end
# NOTE: Do not change this value unless you know what you're doing.
# ========================================================================

config :cog, :embedded_bundle_version, "0.18.1"
config :cog, :embedded_bundle_version, "0.18.2"

# ========================================================================
# Chat Adapters
Expand Down
72 changes: 72 additions & 0 deletions lib/cog/commands/count.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
defmodule Cog.Commands.Count do
use Cog.Command.GenCommand.Base,
bundle: Cog.Util.Misc.embedded_bundle

alias Cog.Command.Service.MemoryClient

@description "Return the count of the values in the input list"

@arguments "[path]"

@examples """
seed '[{"a": 2}, {"a": -1}, {"b": 3}]' | count
> 3
seed '[{"a": 2}, {"a": -1}, {"b": 3}]' | count a
> 2
seed '[{"a": {"b": 2}}, {"b": {"b": -1}}, {"a": {"c": 3}}]' | count a.b
> 1
"""

rule "when command is #{Cog.Util.Misc.embedded_bundle}:count allow"

def handle_message(req, state) do
root = req.services_root
token = req.service_token
key = req.invocation_id
step = req.invocation_step
value = req.cog_env
args = req.args

MemoryClient.accum(root, token, key, value)

case step do
step when step in ["first", "", nil] ->
{:reply, req.reply_to, nil, state}
"last" ->
accumulated_value = MemoryClient.fetch(root, token, key)
{:ok, value} = count_by(accumulated_value, args)
MemoryClient.delete(root, token, key)
{:reply, req.reply_to, value, state}
end
end

defp count_by([%{}], []),
do: {:ok, 0}
defp count_by(items, []),
do: {:ok, Enum.count(items)}
defp count_by(items, [path]) do
path_list = path_to_list(path)

case bad_path(items, path_list) do
true ->
{:ok, 0}
false ->
count = Enum.count(items, &get_in(&1, path_list))
{:ok, count}
end
end

defp bad_path(items, path_list) do
Enum.all?(items, fn item ->
item
|> get_in(path_list)
|> is_nil
end)
end

defp path_to_list(path) do
String.split(path, ".")
end
end
61 changes: 61 additions & 0 deletions test/commands/count_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
defmodule Cog.Test.Commands.CountTest do
use Cog.CommandCase, command_module: Cog.Commands.Count

test "basic count" do
inv_id = "basic_count"
memory_accum(inv_id, %{"a" => 1})
memory_accum(inv_id, %{"b" => 3})

response = new_req(invocation_id: inv_id, cog_env: %{"a" => 2})
|> send_req()
|> unwrap()

assert(response == 3)
end

test "count by simple key" do
inv_id = "simple_key_count"
memory_accum(inv_id, %{"a" => 1})
memory_accum(inv_id, %{"b" => 3})

response = new_req(invocation_id: inv_id, cog_env: %{"a" => 2}, args: ["a"])
|> send_req()
|> unwrap()

assert(response == 2)
end

test "count by complex key" do
inv_id = "complex_key_count"
memory_accum(inv_id, %{"a" => %{"b" => 1}})
memory_accum(inv_id, %{"a" => %{"b" => 3}})

response = new_req(invocation_id: inv_id, cog_env: %{"a" => %{"c" => 2}}, args: ["a.b"])
|> send_req()
|> unwrap()

assert(response == 2)
end

test "count by incorrect key" do
inv_id = "complex_incorrect"
memory_accum(inv_id, %{"a" => %{"b" => 1}})
memory_accum(inv_id, %{"a" => %{"b" => 3}})

response = new_req(invocation_id: inv_id, cog_env: %{"a" => %{"c" => 2}}, args: ["c.d"])
|> send_req()
|> unwrap()

assert(response == 0)
end

test "count of nothing" do
inv_id = "complex_nothing"

response = new_req(invocation_id: inv_id, cog_env: %{}, args: [])
|> send_req()
|> unwrap()

assert(response == 0)
end
end

0 comments on commit 01cf420

Please sign in to comment.