PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/models/m_req.erl

http://github.com/zotonic/zotonic
Erlang | 91 lines | 49 code | 16 blank | 26 comment | 1 complexity | 87c4ffa3a086f0fc07786e602157fae1 MD5 | raw file
 1%% @author Marc Worrell <marc@worrell.nl>
 2%% @copyright 2010 Marc Worrell
 3%% Date: 2010-07-22
 4%%
 5%% @doc Model for the accessing the request fields. Exposes Webmachine's wrq.erl
 6
 7%% Copyright 2010 Marc Worrell
 8%%
 9%% Licensed under the Apache License, Version 2.0 (the "License");
10%% you may not use this file except in compliance with the License.
11%% You may obtain a copy of the License at
12%% 
13%%     http://www.apache.org/licenses/LICENSE-2.0
14%% 
15%% Unless required by applicable law or agreed to in writing, software
16%% distributed under the License is distributed on an "AS IS" BASIS,
17%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18%% See the License for the specific language governing permissions and
19%% limitations under the License.
20
21-module(m_req).
22-author("Marc Worrell <marc@worrell.nl").
23
24-behaviour(gen_model).
25
26%% interface functions
27-export([
28    m_find_value/3,
29    m_to_list/2,
30    m_value/2,
31    
32    get/2
33]).
34
35-include_lib("zotonic.hrl").
36-include_lib("webzmachine/include/webmachine_logger.hrl").
37
38%% @doc Fetch the value for the key from a model source
39%% @spec m_find_value(Key, Source, Context) -> term()
40m_find_value(Key, #m{value=undefined}, Context) ->
41    get(Key, Context).
42
43%% @doc Transform a m_config value to a list, used for template loops
44%% @spec m_to_list(Source, Context) -> List
45m_to_list(#m{value=undefined}, Context) ->
46    values(Context).
47
48%% @doc Transform a model value so that it can be formatted or piped through filters
49%% @spec m_value(Source, Context) -> term()
50m_value(#m{value=undefined}, Context) ->
51    values(Context).
52
53
54%% @doc Fetch the field from the wrq interface.
55-spec get(atom(), #context{} | #wm_reqdata{} | undefined) -> any(). 
56get(undefined, _) -> undefined;
57get(_, undefined) -> undefined;
58get(site, #context{} = Context) -> z_context:site(Context);
59get(timezone, #context{} = Context) -> z_context:tz(Context);
60get(language, #context{} = Context) -> z_context:language(Context);
61get(is_crawler, #context{} = Context) -> z_user_agent:is_crawler(Context);
62get(What, #context{} = Context) -> get_req(What, z_context:get_reqdata(Context));
63get(What, #wm_reqdata{} = RD) -> get_req(What, RD).
64
65get_req(_What, undefined) -> undefined;
66get_req(method, RD) -> wrq:method(RD);
67get_req(version, RD) -> wrq:version(RD);
68get_req(peer, RD) -> wrq:peer(RD);
69get_req(is_ssl, RD) -> wrq:is_ssl(RD);
70get_req(host, RD) -> wrq:get_req_header_lc("host", RD);
71get_req(raw_path, RD) -> wrq:raw_path(RD);
72get_req(path, RD) -> wrq:path(RD);
73get_req(qs, RD) -> wrq:req_qs(RD);
74get_req(headers, RD) -> wrq:req_headers(RD);
75get_req(user_agent, RD) -> wrq:get_req_header_lc("user-agent", RD);
76get_req(referer, RD) -> wrq:get_req_header_lc("referer", RD);
77get_req(referrer, RD) -> wrq:get_req_header_lc("referer", RD);
78get_req(req_id, #wm_reqdata{log_data=#wm_log_data{req_id=ReqId}}) -> ReqId;
79get_req(is_crawler, RD) -> z_user_agent:is_crawler(RD);
80get_req(_Key, _RD) -> undefined.
81
82
83-spec values(#context{}) -> list({atom(), any()}).
84values(Context) ->
85    [ {K, get(K, Context)} || K <- [
86            method, version, peer, is_ssl, host, raw_path, path, qs, referrer, user_agent, is_crawler,
87            req_id, headers, timezone, language
88        ]
89    ].
90
91