Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: cb58744694
Fetching contributors…

Cannot retrieve contributors at this time

file 84 lines (71 sloc) 2.79 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
%%%-------------------------------------------------------------------
%%% @author Tristan Sloughter <>
%%% @copyright (C) 2011, Tristan Sloughter
%%% @doc
%%%
%%% @end
%%% Created : 5 Jul 2011 by Tristan Sloughter <>
%%%-------------------------------------------------------------------
-module(maru_resource_user).

%% API
-export([init/1,
         allowed_methods/2,
         content_types_provided/2,
         content_types_accepted/2,
         resource_exists/2,
         process_post/2,
         to_json/2,
         from_json/2]).

-include_lib("webmachine/include/webmachine.hrl").
-include_lib("maru_web/include/maru_web.hrl").

-define(MODEL, maru_model_users).

%%%===================================================================
%%% API
%%%===================================================================
-spec init(term()) -> {ok, record(ctx)}.
init(_) ->
   {ok, #ctx{model=?MODEL}}.

content_types_provided(ReqData, Ctx) ->
    maru_web_base:content_types_provided(ReqData, Ctx).

content_types_accepted(ReqData, Ctx) ->
    maru_web_base:content_types_accepted(ReqData, Ctx).

allowed_methods(ReqData, Ctx) ->
    maru_web_base:allowed_methods(ReqData, Ctx).

resource_exists(ReqData, Ctx) ->
    maru_web_base:resource_exists(ReqData, Ctx).

process_post(ReqData, Ctx) ->
    maru_web_base:process_post(ReqData, Ctx).

to_json(ReqData, Ctx) ->
    case wrq:path_info(type, ReqData) of
        "check" ->
            {is_taken(username, ReqData), ReqData, Ctx};
        "email_check" ->
            {is_taken(email, ReqData), ReqData, Ctx};
        _ ->
            case wrq:path_info(type, ReqData) of
                undefined ->
                    Models = [(Ctx#ctx.model):to_json(Model) || Model <- (Ctx#ctx.model):find([])],
                    {Models, ReqData, Ctx};
                Username ->
                    case maru_model_users:find({username, list_to_binary(Username)}) of
                        not_found ->
                            {mochijson2:encode(null), ReqData, Ctx};
                        [User] ->
                            NewUser = (Ctx#ctx.model):set([{password, <<"">>}], User),
                            {maru_model_users:to_json(NewUser), ReqData, Ctx}
                    end
            end
    end.

from_json(ReqData, Ctx) ->
    maru_web_base:from_json(ReqData, Ctx).

%%%===================================================================
%%% Internal functions
%%%===================================================================

is_taken(Attribute, ReqData) ->
    Value = wrq:get_qs_value(atom_to_list(Attribute), ReqData),
    case maru_model_users:find({Attribute, list_to_binary(Value)}) of
        not_found ->
            mochijson2:encode(true);
        _ ->
            mochijson2:encode(false)
    end.
Something went wrong with that request. Please try again.