Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #1 from voluntas/master

少しリファクタリングしてみました
  • Loading branch information...
commit 6c4e6539c9a131fd1194b31e56c11d158e3c9f2d 2 parents cea5343 + 68f1992
@hiroeorz authored
View
12 Makefile
@@ -1,3 +1,5 @@
+.PHONY: test
+
ERL=erl
BEAMDIR=./deps/*/ebin ./ebin
REBAR=./rebar
@@ -16,14 +18,12 @@ compile:
xref:
@$(REBAR) xref skip_deps=true
-clean:
+clean:
@ $(REBAR) clean
-check:
- @rm -rf .eunit
- @mkdir -p .eunit
+test:
@ export ERL_MAX_ETS_TABLES=$MAX_ETS_COUNT
- @$(REBAR) skip_deps=true eunit
+ @$(REBAR) skip_deps=true eunit
edoc:
- @$(REBAR) skip_deps=true doc
+ @$(REBAR) skip_deps=true doc
View
5 rebar.config
@@ -23,6 +23,11 @@
{git, "git://github.com/davisp/jiffy.git",
{branch, "master"}}},
+ {mochiweb,
+ "2.3.1",
+ {git, "git://github.com/mochi/mochiweb.git",
+ {tag, "v2.3.1"}}},
+
{cowboy,
"0.*",
{git, "git://github.com/extend/cowboy.git",
View
47 src/hs_session.erl
@@ -24,7 +24,7 @@
terminate/2, code_change/3]).
-define(SERVER, ?MODULE).
--define(SESSION_KEY_HEADER, "_hs_session_").
+-define(SESSION_KEY_HEADER, <<"_hs_session_">>).
-record(state, {}).
@@ -110,7 +110,6 @@ check_loggedin(UsrId, SessionKey) when is_integer(UsrId) ->
check_loggedin(UsrId, SessionKey) when is_binary(UsrId) ->
case get_value(SessionKey, <<"usr_id">>) of
- undefined -> false;
{error, not_found} -> false;
{ok, UsrIdFromSession} ->
case UsrIdFromSession of
@@ -173,8 +172,7 @@ init([]) ->
%% @end
%%--------------------------------------------------------------------
handle_call({create, Usr}, _From, State) ->
- SessionKey = create_session_key(Usr),
-
+ SessionKey = create_session_key(),
Reply = case get_value(SessionKey, <<"usr_id">>) of
{ok, _} -> {error, already_exist};
{error, not_found} ->
@@ -247,25 +245,22 @@ code_change(_OldVsn, State, _Extra) ->
%%% Internal functions
%%%===================================================================
--spec create_session_key(Usr) -> SessionKey when
- Usr :: #usr{},
- SessionKey :: binary().
-create_session_key(Usr) ->
- {{Year, Month, Day}, {Hour, Min, Sec}} = Usr#usr.created_at,
- {{YearNow, MonthNow, DayNow}, {HourNow, MinNow, SecNow}} = {date(), time()},
-
- TimeStr = integer_to_list(Year) ++ integer_to_list(Month) ++
- integer_to_list(Day) ++ integer_to_list(Hour) ++
- integer_to_list(Min) ++ float_to_list(Sec),
-
- TimeNowStr = integer_to_list(YearNow) ++ integer_to_list(MonthNow) ++
- integer_to_list(DayNow) ++ integer_to_list(HourNow) ++
- integer_to_list(MinNow) ++ integer_to_list(SecNow),
-
- SessionKeyBin = crypto:sha([TimeStr, TimeNowStr,
- binary_to_list(Usr#usr.name),
- binary_to_list(Usr#usr.email)]),
-
- S = lists:flatten(lists:map(fun(X) -> io_lib:format("~.16X", [X, ""]) end,
- binary_to_list(SessionKeyBin))),
- list_to_binary(?SESSION_KEY_HEADER ++ string:substr(S, 1, 32)).
+-type session_key() :: binary().
+
+-spec create_session_key() -> session_key().
+create_session_key() ->
+ list_to_binary([?SESSION_KEY_HEADER,
+ mochihex:to_hex(<<(calendar:datetime_to_gregorian_seconds(calendar:now_to_local_time(now()))):32,
+ (crypto:rand_bytes(32))/binary>>)]).
+
+-ifdef(TEST).
+
+create_session_key_test_() ->
+ [
+ {"session key byte size 84",
+ ?_assertEqual(84, byte_size(create_session_key()))},
+ {"session key is not duplicate",
+ ?_assertNot(create_session_key() =:= create_session_key())}
+ ].
+
+-endif.
View
27 src/hs_util.erl
@@ -125,28 +125,13 @@ acc_multipart(Req, [{Headers, BodyAcc}|Acc], end_of_part) ->
acc_multipart(Req, Acc, eof) ->
{lists:reverse(Acc), Req}.
-create_datetime_string(DateTime) when is_tuple(DateTime) ->
- {{Y, M, D}, {H, Min, S}} =
- case DateTime of
- {{Y1, M1, D1}, {H1, Min1, S1}} -> {{Y1, M1, D1}, {H1, Min1, S1}};
- {{Y1, M1, D1}, {H1, Min1, S1, _}} -> {{Y1, M1, D1}, {H1, Min1, S1}}
- end,
-
- Y2 = integer_to_list(Y),
- M2 = string:right(integer_to_list(M), 2, $0),
- D2 = string:right(integer_to_list(D), 2, $0),
- H2 = string:right(integer_to_list(H), 2, $0),
- Min2 = string:right(integer_to_list(Min), 2, $0),
- S2 = case S of
- S3 when is_float(S3) ->
- string:right(integer_to_list(trunc(S3)), 2, $0);
- S3 when is_integer(S3) ->
- string:right(integer_to_list(S), 2, $0)
- end,
-
+-spec create_datetime_string(calendar:datetime() | tuple()) -> binary().
+create_datetime_string({Date, {Hour, Minute, Second, _}}) ->
+ create_datetime_string({Date, {Hour, Minute, Second}});
+create_datetime_string({{Year, Month, Day}, {Hour, Minute, Second}}) ->
list_to_binary(
- lists:flatten(
- io_lib:format("~s-~s-~s ~s:~s:~s", [Y2, M2, D2, H2, Min2, S2]))).
+ io_lib:format("~B-~2.10.0B-~2.10.0B ~2.10.0B:~2.10.0B:~2.10.0B",
+ [Year, Month, Day, Hour, Minute, Second])).
%%%===================================================================
%%% Internal functions
View
15 test/hs_util_tests.erl
@@ -0,0 +1,15 @@
+-module(hs_util_tests).
+
+-include_lib("eunit/include/eunit.hrl").
+
+-import(hs_util, [create_datetime_string/1]).
+
+create_datetime_string_test_() ->
+ [
+ {"default",
+ ?_assertEqual(<<"2000-01-01 00:00:00">>,
+ create_datetime_string({{2000,1,1},{0,0,0}}))},
+ {"default with milli-sec ?",
+ ?_assertEqual(<<"2000-01-01 00:00:00">>,
+ create_datetime_string({{2000,1,1},{0,0,0,0}}))}
+ ].
Please sign in to comment.
Something went wrong with that request. Please try again.