Skip to content

Commit

Permalink
Merge pull request #12 from southgard/feature/subject-endpoints
Browse files Browse the repository at this point in the history
Add Subject endpoints
  • Loading branch information
xaviRodri committed Mar 25, 2022
2 parents 18ce74d + 3de99c2 commit 93f44ef
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
50 changes: 50 additions & 0 deletions lib/subject.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
defmodule ExIsbndb.Subject do
@moduledoc """
The `ExIsbndb.Subject` module contains all the available endpoints for Subjects.
"""

alias ExIsbndb.Client

@doc """
Returns details of a Subject and a list of Books that belong to it.
Params required:
* name (string) - name of the Subject
## Examples
iex> ExIsbndb.Subject.get("drama")
{:ok, %Finch.Response{body: "...", headers: [...], status: 200}}
"""
@spec get(binary()) :: {:ok, Finch.Response.t()} | {:error, Exception.t()}
def get(name) when is_binary(name), do: Client.request(:get, "subject/#{URI.encode(name)}")

@doc """
Returns all the Subjects that match the given query.
Params required:
* query (string) - string used to search
Params available:
* page (integer) - page number of the Subjects to be searched
* page_size (integer) - number of Subjects to be searched per page
Any other parameters will be ignored.
## Examples
iex> ExIsbndb.Subject.search(%{query: "epic", page: 1, page_size: 5})
{:ok, %Finch.Response{body: "...", headers: [...], status: 200}}
"""
@spec search(map()) :: {:ok, Finch.Response.t()} | {:error, Exception.t()}
def search(%{query: query} = params) when is_binary(query) do
params = %{page: params[:page], pageSize: params[:page_size]}

Client.request(:get, "subjects/#{URI.encode(query)}", params)
end
end
48 changes: 48 additions & 0 deletions test/subject_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
defmodule ExIsbndb.SubjectTest do
use ExUnit.Case
import Mock
alias ExIsbndb.Subject

describe "get/1" do
@valid_name "drama"
@not_binary_name :drama

test "Returns a response when the name is valid" do
with_mock(Finch, [:passthrough],
request: fn _request, _server ->
{:ok, %Finch.Response{body: "Subject...", headers: [], status: 200}}
end
) do
assert {:ok, %Finch.Response{}} = Subject.get(@valid_name)
end
end

test "Raises an error if the name is not a binary" do
assert_raise FunctionClauseError, fn -> Subject.get(@not_binary_name) end
end
end

describe "search/1" do
@valid_params %{query: "epic", page: 1, page_size: 10}
@not_query_params %{page: 1, page_size: 10}
@not_binary_query_params %{query: :query, page: 1, page_size: 10}

test "Returns a response when the params are valid" do
with_mock(Finch, [:passthrough],
request: fn _request, _server ->
{:ok, %Finch.Response{body: "Subject...", headers: [], status: 200}}
end
) do
assert {:ok, %Finch.Response{}} = Subject.search(@valid_params)
end
end

test "Raises an error if query not provided" do
assert_raise FunctionClauseError, fn -> Subject.search(@not_query_params) end
end

test "Raises an error if query is not a binary" do
assert_raise FunctionClauseError, fn -> Subject.search(@not_binary_query_params) end
end
end
end

0 comments on commit 93f44ef

Please sign in to comment.