diff --git a/lib/api/bear_controller.ex b/lib/api/bear_controller.ex new file mode 100644 index 0000000..d837d6f --- /dev/null +++ b/lib/api/bear_controller.ex @@ -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 diff --git a/lib/conv.ex b/lib/conv.ex index 2f9df06..088c8a6 100644 --- a/lib/conv.ex +++ b/lib/conv.ex @@ -3,6 +3,7 @@ defmodule Servy.Conv do path: "", params: %{}, headers: %{}, + resp_content_type: "text/html", resp_body: "", status: nil diff --git a/lib/handler.ex b/lib/handler.ex index f9ab217..8731808 100644 --- a/lib/handler.ex +++ b/lib/handler.ex @@ -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 @@ -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} diff --git a/lib/wildthings.ex b/lib/wildthings.ex index 20debb1..61b4afe 100644 --- a/lib/wildthings.ex +++ b/lib/wildthings.ex @@ -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 diff --git a/mix.exs b/mix.exs index ac0b5a3..ce19873 100644 --- a/mix.exs +++ b/mix.exs @@ -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 diff --git a/mix.lock b/mix.lock new file mode 100644 index 0000000..e09e666 --- /dev/null +++ b/mix.lock @@ -0,0 +1,3 @@ +%{ + "poison": {:hex, :poison, "5.0.0", "d2b54589ab4157bbb82ec2050757779bfed724463a544b6e20d79855a9e43b24", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "11dc6117c501b80c62a7594f941d043982a1bd05a1184280c0d9166eb4d8d3fc"}, +} diff --git a/test/handler_test.exs b/test/handler_test.exs index 8f8f772..db968ed 100644 --- a/test/handler_test.exs +++ b/test/handler_test.exs @@ -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

All The Bears!

@@ -54,7 +54,7 @@ defmodule HandlerTest do """ - # assert remove_whitespace(response) == remove_whitespace(expected_response) + assert remove_whitespace(response) == remove_whitespace(expected_response) # temporary fix to tests end @@ -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

Clark's Wildthings Refuge

@@ -148,7 +148,7 @@ defmodule HandlerTest do
""" - # assert remove_whitespace(response) == remove_whitespace(expected_response) + assert remove_whitespace(response) == remove_whitespace(expected_response) # temporary fix to tests end @@ -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 = """