Skip to content

Commit

Permalink
external library
Browse files Browse the repository at this point in the history
  • Loading branch information
hexisdylan committed Jul 24, 2022
1 parent 3abde56 commit 6f8c398
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 43 deletions.
9 changes: 9 additions & 0 deletions lib/api/bear_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Servy.Api.BearController do
def index(conv) do
json =
Servy.Wildthings.list_bears()
|> Poison.encode!()

%{conv | status: 200, resp_content_type: "application/json", resp_body: json}
end
end
1 change: 1 addition & 0 deletions lib/conv.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Servy.Conv do
path: "",
params: %{},
headers: %{},
resp_content_type: "text/html",
resp_body: "",
status: nil

Expand Down
6 changes: 5 additions & 1 deletion lib/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ defmodule Servy.Handler do
%{conv | status: 200, resp_body: "Bears, Lions, Tigers"}
end

def route(%Conv{method: "GET", path: "/api/bears"} = conv) do
Servy.Api.BearController.index(conv)
end

def route(%Conv{method: "GET", path: "/bears"} = conv) do
BearController.index(conv)
end
Expand Down Expand Up @@ -85,7 +89,7 @@ defmodule Servy.Handler do
def format_response(%Conv{} = conv) do
"""
HTTP/1.1 #{Conv.full_status(conv)}\r
Content-Type: text/html\r
Content-Type: #{conv.resp_content_type}\r
Content-Length: #{String.length(conv.resp_body)}\r
\r
#{conv.resp_body}
Expand Down
18 changes: 9 additions & 9 deletions lib/wildthings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ defmodule Servy.Wildthings do
def list_bears do
[
%Bear{id: 1, name: "Teddy", type: "Brown", hibernating: true},
%Bear{id: 1, name: "Smokey", type: "Black"},
%Bear{id: 1, name: "Paddington", type: "Brown"},
%Bear{id: 1, name: "Scarface", type: "Grizzly", hibernating: true},
%Bear{id: 1, name: "Snow", type: "Polar"},
%Bear{id: 1, name: "Brutus", type: "Grizzly"},
%Bear{id: 1, name: "Rosie", type: "Black", hibernating: true},
%Bear{id: 1, name: "Roscoe", type: "Panda"},
%Bear{id: 1, name: "Iceman", type: "Polar", hibernating: true},
%Bear{id: 1, name: "Kenai", type: "Grizzly"}
%Bear{id: 2, name: "Smokey", type: "Black"},
%Bear{id: 3, name: "Paddington", type: "Brown"},
%Bear{id: 4, name: "Scarface", type: "Grizzly", hibernating: true},
%Bear{id: 5, name: "Snow", type: "Polar"},
%Bear{id: 6, name: "Brutus", type: "Grizzly"},
%Bear{id: 7, name: "Rosie", type: "Black", hibernating: true},
%Bear{id: 8, name: "Roscoe", type: "Panda"},
%Bear{id: 9, name: "Iceman", type: "Polar", hibernating: true},
%Bear{id: 10, name: "Kenai", type: "Grizzly"}
]
end

Expand Down
3 changes: 1 addition & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ defmodule Servy.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
{:poison, "~> 5.0"}
]
end
end
3 changes: 3 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%{
"poison": {:hex, :poison, "5.0.0", "d2b54589ab4157bbb82ec2050757779bfed724463a544b6e20d79855a9e43b24", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "11dc6117c501b80c62a7594f941d043982a1bd05a1184280c0d9166eb4d8d3fc"},
}
62 changes: 31 additions & 31 deletions test/handler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule HandlerTest do
expected_response = """
HTTP/1.1 200 OK\r
Content-Type: text/html\r
Content-Length: 355\r
Content-Length: 377\r
\r
<h1>All The Bears!</h1>
Expand All @@ -54,7 +54,7 @@ defmodule HandlerTest do
</ul>
"""

# assert remove_whitespace(response) == remove_whitespace(expected_response)
assert remove_whitespace(response) == remove_whitespace(expected_response)
# temporary fix to tests
end

Expand Down Expand Up @@ -137,7 +137,7 @@ defmodule HandlerTest do
expected_response = """
HTTP/1.1 200 OK\r
Content-Type: text/html\r
Content-Length: 320\r
Content-Length: 324\r
\r
<h1>Clark's Wildthings Refuge</h1>
<blockquote>
Expand All @@ -148,7 +148,7 @@ defmodule HandlerTest do
</blockquote>
"""

# assert remove_whitespace(response) == remove_whitespace(expected_response)
assert remove_whitespace(response) == remove_whitespace(expected_response)
# temporary fix to tests
end

Expand All @@ -175,36 +175,36 @@ defmodule HandlerTest do
"""
end

# test "GET /api/bears" do
# request = """
# GET /api/bears HTTP/1.1\r
# Host: example.com\r
# User-Agent: ExampleBrowser/1.0\r
# Accept: */*\r
# \r
# """
test "GET /api/bears" do
request = """
GET /api/bears HTTP/1.1\r
Host: example.com\r
User-Agent: ExampleBrowser/1.0\r
Accept: */*\r
\r
"""

# response = handle(request)
response = handle(request)

# expected_response = """
# HTTP/1.1 200 OK
# Content-Type: application/json
# Content-Length: 605

# [{"type":"Brown","name":"Teddy","id":1,"hibernating":true},
# {"type":"Black","name":"Smokey","id":2,"hibernating":false},
# {"type":"Brown","name":"Paddington","id":3,"hibernating":false},
# {"type":"Grizzly","name":"Scarface","id":4,"hibernating":true},
# {"type":"Polar","name":"Snow","id":5,"hibernating":false},
# {"type":"Grizzly","name":"Brutus","id":6,"hibernating":false},
# {"type":"Black","name":"Rosie","id":7,"hibernating":true},
# {"type":"Panda","name":"Roscoe","id":8,"hibernating":false},
# {"type":"Polar","name":"Iceman","id":9,"hibernating":true},
# {"type":"Grizzly","name":"Kenai","id":10,"hibernating":false}]
# """
expected_response = """
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 605
[{"type":"Brown","name":"Teddy","id":1,"hibernating":true},
{"type":"Black","name":"Smokey","id":2,"hibernating":false},
{"type":"Brown","name":"Paddington","id":3,"hibernating":false},
{"type":"Grizzly","name":"Scarface","id":4,"hibernating":true},
{"type":"Polar","name":"Snow","id":5,"hibernating":false},
{"type":"Grizzly","name":"Brutus","id":6,"hibernating":false},
{"type":"Black","name":"Rosie","id":7,"hibernating":true},
{"type":"Panda","name":"Roscoe","id":8,"hibernating":false},
{"type":"Polar","name":"Iceman","id":9,"hibernating":true},
{"type":"Grizzly","name":"Kenai","id":10,"hibernating":false}]
"""

# assert remove_whitespace(response) == remove_whitespace(expected_response)
# end
assert remove_whitespace(response) == remove_whitespace(expected_response)
end

# test "POST /api/bears" do
# request = """
Expand Down

0 comments on commit 6f8c398

Please sign in to comment.