Skip to content

Commit

Permalink
Added documentation for helper methods and moved success closer to er…
Browse files Browse the repository at this point in the history
…ror, will refactor shortly.
  • Loading branch information
baphled committed Jan 12, 2009
1 parent 8e7e583 commit d5242e9
Showing 1 changed file with 41 additions and 21 deletions.
62 changes: 41 additions & 21 deletions lib/chatterl/src/chatterl_gateway.erl
Expand Up @@ -179,6 +179,19 @@ clean_path(Path) ->
string:substr(Path, 1, string:len(Path) - (N + 1))
end.

%%--------------------------------------------------------------------
%% @doc
%%
%% Handles our successful responses.
%%
%% Will eventually be JSON and XML based
%% @spec success(Req,Body) -> tuple()
%%
%% @end
%%--------------------------------------------------------------------
success(Req, {ContentType,Body}) when is_list(Body) ->
Req:respond({200, [{"Content-Type", ContentType}], list_to_binary(Body)}).

%%--------------------------------------------------------------------
%% @doc
%%
Expand All @@ -190,47 +203,54 @@ clean_path(Path) ->
%% @end
%%--------------------------------------------------------------------
error(Req, {ContentType,Record}) when is_list(ContentType) ->
Response =
case ContentType of
"text/json" ->
to_json(Record);
"text/xml" ->
xml_message(Record)
end,
Response = get_response_body(ContentType,Record),
Code = get_response_code(Record),
%% If we cant construct the code or have an illegal content type, we need to
%% send an illegal method message back to the client.
Req:respond({Code, [{"Content-Type", ContentType}], list_to_binary(Response)}).

handle_response(ContentType) ->
%%--------------------------------------------------------------------
%% @doc
%%
%% Gets the actual response body to return to the client.
%%
%% Responses are either in JSON or XML, if the wrong content type is passed
%% the client will recieve an error in XML format.
%% @spec get_response_body(ContentType,Record) -> [ResponseBody]
%%
%% @end
%%--------------------------------------------------------------------
get_response_body(ContentType,Record) ->
case ContentType of
"text/json" ->
to_json(Record);
"text/xml" ->
xml_message(Record)
xml_message(Record);
_ -> xml_message(#carrier{ type="error", message="Illegal method"})
end.

%%--------------------------------------------------------------------
%% @doc
%%
%% Gets our response code depending on the type of message passed by
%% the carrier record.
%%
%%
%% @spec get_response_code(Record) -> integer()
%%
%% @end
%%--------------------------------------------------------------------
get_response_code(Record) ->
case Record of
{carrier,Type,_Message} ->
case Type of
"fail" -> 500;
"success" -> 200;
"error" -> 200;
_ -> 500
end;
_-> io:format("Illegal Code set ~s~n",[Record])
end.
%%--------------------------------------------------------------------
%% @doc
%%
%% Handles our successful responses.
%%
%% Will eventually be JSON and XML based
%% @spec success(Req,Body) -> tuple()
%%
%% @end
%%--------------------------------------------------------------------
success(Req, {ContentType,Body}) when is_list(Body) ->
Req:respond({200, [{"Content-Type", ContentType}], list_to_binary(Body)}).

%%--------------------------------------------------------------------
%% @doc
Expand Down

0 comments on commit d5242e9

Please sign in to comment.