Skip to content

Commit

Permalink
[#24] Fixed pair review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
davecaos committed Jul 1, 2015
1 parent 0185b35 commit f99bebb
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 28 deletions.
2 changes: 1 addition & 1 deletion example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ curl -v -H "Content-Type: text/plain" -X PUT http://localhost:8080/message/yahoo
< content-type: text/plain
<
* Connection #0 to host localhost left intact
You put a echo! yahooooo!⏎
You put an echo! yahooooo!⏎
```

Description example
Expand Down
2 changes: 1 addition & 1 deletion example/priv/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<h2>This is a Cowboy Trails example!</h2>
<h3><a href="http://localhost:8080/description">Cowboy Trails Descriptions link</a></h3>
<h3><a href="http://localhost:8080/description">Cowboy Trails Descriptions</a></h3>
<br>
<a href="https://en.wikipedia.org/wiki/Cowboy_Trail">The Cowboy Trail is a rail trail in northern Nebraska</a>
<br>
Expand Down
14 changes: 6 additions & 8 deletions example/src/example.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
%% application
%% @doc Starts the application
start() ->
% {ok, _Started}
Resp = application:ensure_all_started(example).
application:ensure_all_started(example).

%% @doc Stops the application
stop() ->
Expand All @@ -19,8 +18,7 @@ stop() ->
%% behaviour
%% @private
start(_StartType, _StartArgs) ->
{ok, Pid} = example_sup:start_link(),
{ok, Pid}.
example_sup:start_link().

%% @private
stop(_State) ->
Expand All @@ -32,10 +30,10 @@ start_phase(start_trails_http, _StartType, []) ->
{ok, Port} = application:get_env(example, http_port),
{ok, ListenerCount} = application:get_env(example, http_listener_count),
DescriptionTrail =
trails:trail(<<"/description">>
, example_description_handler
, []
, #{get => #{desc => "Retrives trails's server description"}}),
trails:trail(<<"/description">>
, example_description_handler
, []
, #{get => #{desc => "Retrives trails's server description"}}),

StaticTrail =
trails:trail(<<"/[...]">>
Expand Down
14 changes: 3 additions & 11 deletions example/src/example_default.erl
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,13 @@ rest_init(Req, _Opts) ->
{ok, Req, #{}}.

content_types_accepted(Req, State) ->
{Method, Req1} = cowboy_req:method(Req),
HandleMethod = case Method of
<<"PUT">> -> handle_put
%% Add here another handlers methods
end,
{[{<<"text/plain">>, HandleMethod}], Req1, State}.
{[{<<"text/plain">>, handle_put}], Req, State}.

content_types_provided(Req, State) ->
{ [{<<"text/plain">>, handle_get}]
, Req
, State}.
{[{<<"text/plain">>, handle_get}], Req, State}.

forbidden(Req, State) ->
{false, Req, State}.

resource_exists(Req, State) ->
{Method, Req1} = cowboy_req:method(Req),
{Method =/= <<"POST">>, Req1, State}.
{true, Req, State}.
13 changes: 6 additions & 7 deletions example/src/example_echo_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,28 @@

trails() ->
MsgTrailsMetadata =
#{ get => #{ desc => "Gets echo var from the server"
#{ get => #{ desc => "Gets echo var from the server"
, 'content-type' => "text/plain"}
, put => #{desc => "Sets echo var in the server"
, put => #{desc => "Sets echo var in the server"
, 'content-type' => "text/plain"}
},
},
[
trails:trail("/message/[:echo]", example_echo_handler, [], MsgTrailsMetadata)
].

%% cowboy
allowed_methods(Req, State) ->
io:format("allowed_methods() -> Req ~p ~n", [Req]),
{[<<"GET">>, <<"PUT">>, <<"HEAD">>], Req, State}.
{[<<"GET">>, <<"PUT">>, <<"HEAD">>], Req, State}.

%% internal
handle_get(Req, State) ->
Echo = application:get_env(example, echo, ""),
Body = [<<"You Get a echo! ">> , Echo ],
Body = [<<"You Get an echo! ">> , Echo ],
{Body, Req, State}.

handle_put(Req, State) ->
{Echo, Req1} = cowboy_req:binding(echo, Req, ""),
application:set_env(example, echo, Echo),
Body = [<<"You put a echo! ">> , Echo ],
Body = [<<"You put an echo! ">> , Echo ],
Req2 = cowboy_req:set_resp_body(Body, Req1),
{true, Req2, State}.

0 comments on commit f99bebb

Please sign in to comment.