From b73c590b3acbe8bf8d9b02d79b6229e4586737bd Mon Sep 17 00:00:00 2001 From: Manuel Rubio Date: Thu, 21 Jun 2018 11:08:17 +0200 Subject: [PATCH 1/3] let to specify args like skip, limit and others for views --- lib/couchdb_connector/url_helper.ex | 29 ++++++++++++++++++++++++++--- lib/couchdb_connector/view.ex | 10 +++++----- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/couchdb_connector/url_helper.ex b/lib/couchdb_connector/url_helper.ex index c5dd691..c60d895 100644 --- a/lib/couchdb_connector/url_helper.ex +++ b/lib/couchdb_connector/url_helper.ex @@ -72,9 +72,32 @@ defmodule Couchdb.Connector.UrlHelper do @doc """ Produces the URL to a specific view from a given design document. """ - @spec view_url(Types.db_properties, String.t, String.t) :: String.t - def view_url db_props, design, view do - "#{design_url(db_props, design)}/_view/#{view}" + @spec view_url(Types.db_properties, String.t, String.t, Keyword.t) :: String.t + def view_url db_props, design, view, args \\ [] do + "#{design_url(db_props, design)}/_view/#{view}#{query(args)}" + end + + defp to_bin(i) when is_integer(i), do: Integer.to_string(i) + defp to_bin(f) when is_float(f), do: Float.to_string(f) + defp to_bin(a) when is_atom(a), do: Atom.to_string(a) + defp to_bin(b) when is_binary(b), do: b + + defp prepare({key, value}) do + "#{prepare_value(key)}=#{prepare_value(value)}" + end + + defp prepare_value(value) do + value + |> to_bin() + |> URI.encode_www_form() + end + + defp query([]), do: "" + defp query(args) do + query = args + |> Enum.map(&(prepare(&1))) + |> Enum.join("&") + "?#{query}" end @doc """ diff --git a/lib/couchdb_connector/view.ex b/lib/couchdb_connector/view.ex index 94319aa..1259d6a 100644 --- a/lib/couchdb_connector/view.ex +++ b/lib/couchdb_connector/view.ex @@ -22,12 +22,12 @@ defmodule Couchdb.Connector.View do @doc """ Returns everything found for the given view in the given design document. """ - @spec fetch_all(Types.db_properties, String.t, String.t) :: {:ok, String.t} | {:error, String.t} - def fetch_all(db_props, design, view) do + @spec fetch_all(Types.db_properties, String.t, String.t, Keyword.t) :: {:ok, String.t} | {:error, String.t} + def fetch_all(db_props, design, view, args \\ []) do db_props - |> UrlHelper.view_url(design, view) - |> HTTPoison.get! - |> Handler.handle_get + |> UrlHelper.view_url(design, view, args) + |> HTTPoison.get!() + |> Handler.handle_get() end @doc """ From 3519e4322013ac5e281813bdb4f22ded5f9de32d Mon Sep 17 00:00:00 2001 From: Manuel Rubio Date: Tue, 10 Jul 2018 13:41:36 +0200 Subject: [PATCH 2/3] add test for view arguments --- test/couchdb_connector/url_helper_test.exs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/couchdb_connector/url_helper_test.exs b/test/couchdb_connector/url_helper_test.exs index 23d59ae..ed63db9 100644 --- a/test/couchdb_connector/url_helper_test.exs +++ b/test/couchdb_connector/url_helper_test.exs @@ -12,4 +12,14 @@ defmodule Couchdb.Connector.UrlHelperTest do assert UrlHelper.query_path(url, key, atom) == "http://localhost/view_name?key=\"%21%2A%27%28%29%3B%3A%40%26%3D%2B%24%2C%2F%3F%23%5B%5D\"&stale=ok" end + + test "view_url/3 with args" do + dbprops = [protocol: "http", hostname: "localhost", port: 80, database: "myapp"] + design = "users" + view = "active_users" + args = [limit: 10, skip: 5] # simulate a 5 elements page and page 2 + + assert UrlHelper.view_url(dbprops, design, view, args) == + "http://localhost:80/myapp/_design/users/_view/active_users?limit=10&skip=5" + end end From 9c75ea8ca278aa9ebd9523bd7ec5be994a573b39 Mon Sep 17 00:00:00 2001 From: Manuel Rubio Date: Tue, 10 Jul 2018 13:46:19 +0200 Subject: [PATCH 3/3] typo error in view_url test --- test/couchdb_connector/url_helper_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/couchdb_connector/url_helper_test.exs b/test/couchdb_connector/url_helper_test.exs index ed63db9..e94a057 100644 --- a/test/couchdb_connector/url_helper_test.exs +++ b/test/couchdb_connector/url_helper_test.exs @@ -14,7 +14,7 @@ defmodule Couchdb.Connector.UrlHelperTest do end test "view_url/3 with args" do - dbprops = [protocol: "http", hostname: "localhost", port: 80, database: "myapp"] + dbprops = %{protocol: "http", hostname: "localhost", port: 80, database: "myapp"} design = "users" view = "active_users" args = [limit: 10, skip: 5] # simulate a 5 elements page and page 2