Erlang/OTP Forums

Author Message

<  Erlyweb mailing list  ~  Calculating a module/function/params URL

Guest
Posted: Mon Jan 28, 2008 11:21 pm Reply with quote
Guest
make_url/4 calculates a URL with out the protocol://host:port/ prefix it
returns the form /base/module/function/params given a yaws arg record A,
a module name M, a function name F, and a list of parameters Params.
There may be errors in this code as it was copy and pasted from else
where and modififed here. It may also be be possible to get rid of one
of the reverses or make other optimisations. Consider this a first cut.

Todo: find protocol, host, and port used by client and include in url
add when-clauses to function

Jeff.
------

base(A) ->
L = length(yaws_arg:appmoddata(A)),
lists:reverse(lists:nthtail(L,lists:reverse(yaws_arg:server_path(A)))).

make_url(A, M, F, Params) ->
PStr = lists:foldl(fun(P, Acc) ->
PL = erlydb_base:field_to_iolist(P),
[["/" | PL] | Acc]
end,
[],
lists:reverse(Params)),
io:format("PStr ~p~n", [PStr]),
MStr = erlydb_base:field_to_iolist(M),
FStr = erlydb_base:field_to_iolist(F),
lists:flatten(base(A) ++ "/" ++ MStr ++ "/" ++ FStr ++ PStr).


test_make_url() ->
make_url(A, module, function, [1,2,3]).

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "erlyweb" group.
To post to this group, send email to erlyweb@googlegroups.com
To unsubscribe from this group, send email to erlyweb-unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Post recived from mailinglist
Guest
Posted: Tue Jan 29, 2008 8:04 pm Reply with quote
Guest
I like this functionality. I tend to hardcode my urls as strings in
the view templates, but this is a better solution because it lets you
change your URL scheme later without modifying a bunch of templates.
This could be very useful in making your URLs work both in and out of
Facebook, which annoyingly prepends all relative urls with
/[app-name]/.

A comment: if you create the url so it can be sent through a socket as
an iolist, this line is inefficient:

lists:flatten(base(A) ++ "/" ++ MStr ++ "/" ++ FStr ++ PStr).

I would rewrite it as

[base(A), $/, MStr, $/, Fstr, Pstr].

Yariv

On Jan 28, 2008 3:22 PM, jm <jeffm@ghostgun.com> wrote:
>
>
> make_url/4 calculates a URL with out the protocol://host:port/ prefix it
> returns the form /base/module/function/params given a yaws arg record A,
> a module name M, a function name F, and a list of parameters Params.
> There may be errors in this code as it was copy and pasted from else
> where and modififed here. It may also be be possible to get rid of one
> of the reverses or make other optimisations. Consider this a first cut.
>
> Todo: find protocol, host, and port used by client and include in url
> add when-clauses to function
>
> Jeff.
> ------
>
> base(A) ->
> L = length(yaws_arg:appmoddata(A)),
> lists:reverse(lists:nthtail(L,lists:reverse(yaws_arg:server_path(A)))).
>
> make_url(A, M, F, Params) ->
> PStr = lists:foldl(fun(P, Acc) ->
> PL = erlydb_base:field_to_iolist(P),
> [["/" | PL] | Acc]
> end,
> [],
> lists:reverse(Params)),
> io:format("PStr ~p~n", [PStr]),
> MStr = erlydb_base:field_to_iolist(M),
> FStr = erlydb_base:field_to_iolist(F),
> lists:flatten(base(A) ++ "/" ++ MStr ++ "/" ++ FStr ++ PStr).
>
>
> test_make_url() ->
> make_url(A, module, function, [1,2,3]).
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "erlyweb" group.
To post to this group, send email to erlyweb@googlegroups.com
To unsubscribe from this group, send email to erlyweb-unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Post recived from mailinglist
dmitriid
Posted: Wed Jan 30, 2008 7:34 am Reply with quote
User Joined: 17 Aug 2006 Posts: 213
I would also like a function without A, because A is not always available (especially in the views). Most of the time (especially in the production environment) a "/module/function/params" url is more than enough

Quote:
I like this functionality. I tend to hardcode my urls as strings in the view templates, but this is a better solution because it lets you change your URL scheme later without modifying a bunch of templates. This could be very useful in making your URLs work both in and out of Facebook, which annoyingly prepends all relative urls with /[app-name]/. A comment: if you create the url so it can be sent through a socket as an iolist, this line is inefficient: lists:flatten(base(A) ++ "/" ++ MStr ++ "/" ++ FStr ++ PStr). I would rewrite it as [base(A), $/, MStr, $/, Fstr, Pstr]. Yariv On Jan 28, 2008 3:22 PM, jm <jeffm@ghostgun.com> (jeffm@ghostgun.com) wrote: [/code]
Quote:
make_url/4 calculates a URL with out the protocol://host:port/ prefix it returns the form /base/module/function/params given a yaws arg record A, a module name M, a function name F, and a list of parameters Params. There may be errors in this code as it was copy and pasted from else where and modififed here. It may also be be possible to get rid of one of the reverses or make other optimisations. Consider this a first cut. Todo: find protocol, host, and port used by client and include in url add when-clauses to function Jeff. ------ base(A) -> L = length(yaws_arg:appmoddata(A)), lists:reverse(lists:nthtail(L,lists:reverse(yaws_arg:server_path(A)))). make_url(A, M, F, Params) -> PStr = lists:foldl(fun(P, Acc) -> PL = erlydb_base:field_to_iolist(P), [["/" | PL] | Acc] end, [], lists:reverse(Params)), io:format("PStr ~p~n", [PStr]), MStr = erlydb_base:field_to_iolist(M), FStr = erlydb_base:field_to_iolist(F), lists:flatten(base(A) ++ "/" ++ MStr ++ "/" ++ FStr ++ PStr). test_make_url() -> make_url(A, module, function, [1,2,3]). [/code]
[/code]


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "erlyweb" group.
To post to this group, send email to erlyweb@googlegroups.com
To unsubscribe from this group, send email to erlyweb-unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Post recived from mailinglist
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum