2011-01-30 08:28:56 -06:00
|
|
|
-module(ts_json).
|
|
|
|
-export([encode_record/1, record_to_ejson/1, ejson_to_record/2]).
|
|
|
|
|
|
|
|
-include("ts_db_records.hrl").
|
|
|
|
|
|
|
|
encode_record(Record) -> lists:flatten(json:encode(record_to_ejson(Record))).
|
|
|
|
|
2011-02-03 17:23:40 -06:00
|
|
|
record_to_ejson(Record=#ts_user{}) ->
|
|
|
|
{struct, [
|
|
|
|
{username, atom_to_list(Record#ts_user.username)},
|
|
|
|
{name, Record#ts_user.name},
|
|
|
|
{email, Record#ts_user.email},
|
|
|
|
{join_date, encode_datetime(Record#ts_user.join_date)}]};
|
|
|
|
|
2011-01-30 08:28:56 -06:00
|
|
|
record_to_ejson(Record=#ts_timeline{}) ->
|
2011-02-03 17:23:40 -06:00
|
|
|
% pull out the username and timeline id
|
|
|
|
{Username, TimelineId} = Record#ts_timeline.ref,
|
2011-01-30 08:28:56 -06:00
|
|
|
|
|
|
|
% create the EJSON struct
|
|
|
|
{struct, [
|
2011-02-03 17:23:40 -06:00
|
|
|
{username, atom_to_list(Username)},
|
|
|
|
{timeline_id, atom_to_list(TimelineId)},
|
2011-01-30 08:28:56 -06:00
|
|
|
{created, encode_datetime(Record#ts_timeline.created)},
|
2011-02-02 16:57:58 -06:00
|
|
|
{description, Record#ts_timeline.desc}]};
|
2011-01-30 08:28:56 -06:00
|
|
|
|
|
|
|
record_to_ejson(Record=#ts_entry{}) ->
|
2011-02-03 17:23:40 -06:00
|
|
|
% pull out the username, timeline id, and entry id
|
|
|
|
{Username, TimelineId, EntryId} = Record#ts_entry.ref,
|
2011-01-30 08:28:56 -06:00
|
|
|
|
|
|
|
% convert the timestamp to a date-time
|
|
|
|
DateTime = calendar:gregorian_seconds_to_datetime(Record#ts_entry.timestamp),
|
|
|
|
|
|
|
|
% create the EJSON struct
|
|
|
|
{struct, [
|
2011-03-03 17:05:30 -06:00
|
|
|
{username, atom_to_list(Username)},
|
|
|
|
{timeline_id, atom_to_list(TimelineId)},
|
2011-02-03 17:23:40 -06:00
|
|
|
{entry_id, EntryId},
|
2011-01-30 08:28:56 -06:00
|
|
|
{timestamp, encode_datetime(DateTime)},
|
|
|
|
{mark, Record#ts_entry.mark},
|
|
|
|
{notes, Record#ts_entry.notes}]}.
|
|
|
|
|
|
|
|
encode_datetime({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
2011-03-07 16:43:40 -06:00
|
|
|
lists:flatten(io_lib:format("~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~2.10.0B~3.10.0BZ",
|
|
|
|
[Year, Month, Day, Hour, Minute, Second, 000])).
|
2011-01-30 08:28:56 -06:00
|
|
|
|
2011-02-03 17:23:40 -06:00
|
|
|
ejson_to_record(_Empty=#ts_timeline{}, EJSON) ->
|
2011-01-30 08:28:56 -06:00
|
|
|
{struct, Fields} = EJSON,
|
|
|
|
|
|
|
|
#ts_timeline{
|
2011-02-14 17:16:37 -06:00
|
|
|
ref = {undefined, undefined},
|
2011-02-02 16:57:58 -06:00
|
|
|
created = decode_datetime(element(2, lists:keyfind(created, 1, Fields))),
|
|
|
|
desc = element(2, lists:keyfind(description, 1, Fields))};
|
2011-01-30 08:28:56 -06:00
|
|
|
|
2011-02-03 17:23:40 -06:00
|
|
|
ejson_to_record(_Empty=#ts_entry{}, EJSON) ->
|
2011-01-30 08:28:56 -06:00
|
|
|
{struct, Fields} = EJSON,
|
|
|
|
|
|
|
|
#ts_entry{
|
2011-02-14 17:16:37 -06:00
|
|
|
ref = {undefined, undefined, undefined},
|
2011-01-30 08:28:56 -06:00
|
|
|
timestamp = calendar:datetime_to_gregorian_seconds(decode_datetime(
|
2011-02-02 16:57:58 -06:00
|
|
|
element(2, lists:keyfind(timestamp, 1, Fields)))),
|
|
|
|
mark = element(2, lists:keyfind(mark, 1, Fields)),
|
|
|
|
notes = element(2, lists:keyfind(notes, 1, Fields))}.
|
2011-01-30 08:28:56 -06:00
|
|
|
|
|
|
|
decode_datetime(DateTimeString) ->
|
|
|
|
% TODO: catch badmatch and badarg on whole function
|
|
|
|
|
|
|
|
[DateString, TimeString] = re:split(DateTimeString, "[TZ]",
|
|
|
|
[{return, list}, trim]),
|
|
|
|
|
|
|
|
[YearString, MonthString, DayString] =
|
|
|
|
re:split(DateString, "-", [{return, list}]),
|
|
|
|
|
2011-03-07 16:43:40 -06:00
|
|
|
[HourString, MinuteString, SecondString] =
|
|
|
|
case re:split(TimeString, "[:\\.]", [{return, list}]) of
|
|
|
|
[HS, MS, SS, _MSS] -> [HS, MS, SS];
|
|
|
|
[HS, MS, SS] -> [HS, MS, SS]
|
|
|
|
end,
|
2011-01-30 08:28:56 -06:00
|
|
|
|
|
|
|
Date = {list_to_integer(YearString), list_to_integer(MonthString),
|
2011-02-02 16:57:58 -06:00
|
|
|
list_to_integer(DayString)},
|
2011-01-30 08:28:56 -06:00
|
|
|
|
|
|
|
Time = {list_to_integer(HourString), list_to_integer(MinuteString),
|
2011-02-02 16:57:58 -06:00
|
|
|
list_to_integer(SecondString)},
|
2011-01-30 08:28:56 -06:00
|
|
|
|
|
|
|
{Date, Time}.
|