Skip to content

Commit

Permalink
Merge pull request #203 from 2600hz/KAZOO-1770
Browse files Browse the repository at this point in the history
Kazoo 1770
  • Loading branch information
k-anderson committed Jan 16, 2014
2 parents c90ee25 + baac519 commit ccbbd5d
Show file tree
Hide file tree
Showing 32 changed files with 2,570 additions and 594 deletions.
10 changes: 6 additions & 4 deletions applications/crossbar/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ all: compile

MODULES = $(shell ls src/*.erl | sed 's/src\///;s/\.erl/,/' | sed '$$s/.$$//')
CB_MODULES = $(shell ls src/modules/*.erl | sed 's/src\/modules\///;s/\.erl/,/' | sed '$$s/.$$//')
CB_MODULES_V1 = $(shell ls src/modules_v1/*.erl | sed 's/src\/modules_v1\///;s/\.erl/,/' | sed '$$s/.$$//')
CB_MODULES_V2 = $(shell ls src/modules_v2/*.erl | sed 's/src\/modules_v2\///;s/\.erl/,/' | sed '$$s/.$$//')

compile: ebin/$(PROJECT).app
@cat src/$(PROJECT).app.src \
| sed 's/{modules, \[\]}/{modules, \[$(MODULES),$(CB_MODULES)\]}/' \
| sed 's/{modules, \[\]}/{modules, \[$(MODULES),$(CB_MODULES),$(CB_MODULES_V1),$(CB_MODULES_V2)\]}/' \
> ebin/$(PROJECT).app
-@$(MAKE) ebin/$(PROJECT).app

ebin/$(PROJECT).app: src/*.erl src/modules/*.erl
ebin/$(PROJECT).app: src/*.erl src/*/*.erl
@mkdir -p ebin/
ERL_LIBS=$(ERL_LIBS) erlc -v $(ERLC_OPTS) -o ebin/ -pa ebin/ $?

compile-test: test/$(PROJECT).app
@cat src/$(PROJECT).app.src \
| sed 's/{modules, \[\]}/{modules, \[$(MODULES),$(CB_MODULES)\]}/' \
| sed 's/{modules, \[\]}/{modules, \[$(MODULES),$(CB_MODULES,$(CB_MODULES_V1),$(CB_MODULES_V2))\]}/' \
> test/$(PROJECT).app
-@$(MAKE) test/$(PROJECT).app

Expand All @@ -49,7 +51,7 @@ clean:
test: clean compile-test eunit

eunit: compile-test
erl -noshell -pa test -eval "eunit:test([$(MODULES),$(CB_MODULES)], [verbose])" -s init stop
erl -noshell -pa test -eval "eunit:test([$(MODULES),$(CB_MODULES,$(CB_MODULES_V1),$(CB_MODULES_V2))], [verbose])" -s init stop

dialyze:
@$(DIALYZER) $(foreach DIR,$(DIRS),$(DIR)/ebin) \
Expand Down
4 changes: 4 additions & 0 deletions applications/crossbar/priv/couchdb/account/services.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"devices": {
"map": "function(doc) { if (doc.pvt_type != 'device' || doc.pvt_deleted || doc.enabled == false) return; emit(doc.device_type || 'sip_device', 1); }",
"reduce": "_sum"
},
"users": {
"map": "function(doc) { if (doc.pvt_type != 'user' || doc.pvt_deleted || doc.enabled == false) return; emit(doc.priv_level || 'user', 1); }",
"reduce": "_sum"
}
}
}
2 changes: 1 addition & 1 deletion applications/crossbar/src/api_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ known_methods(Req, Context) ->
allowed_methods(Req0, Context) ->
Methods = cb_context:allowed_methods(Context),
{Tokens, Req1} = cowboy_req:path_info(Req0),
case api_util:parse_path_tokens(Tokens) of
case api_util:parse_path_tokens(Context, Tokens) of
[_|_] = Nouns ->
%% Because we allow tunneling of verbs through the request,
%% we have to check and see if we need to override the actual
Expand Down
39 changes: 25 additions & 14 deletions applications/crossbar/src/api_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
,is_cors_request/1
,add_cors_headers/2
,allow_methods/4
,parse_path_tokens/1
,parse_path_tokens/2
,get_req_data/2
,get_http_verb/2
,is_authentic/2
Expand Down Expand Up @@ -468,34 +468,45 @@ get_http_verb(Method, Context) ->

-type cb_mod_with_tokens() :: {ne_binary(), path_tokens()}.
-type cb_mods_with_tokens() :: [cb_mod_with_tokens(),...] | [].
-spec parse_path_tokens(path_tokens()) -> cb_mods_with_tokens().
parse_path_tokens(Tokens) ->
parse_path_tokens(Tokens, []).
-spec parse_path_tokens(cb_context:context(), path_tokens()) -> cb_mods_with_tokens().
parse_path_tokens(Context, Tokens) ->
parse_path_tokens(Context, Tokens, []).

-spec parse_path_tokens(wh_json:json_strings(), cb_mods_with_tokens()) ->
-spec parse_path_tokens(cb_context:context(), wh_json:json_strings(), cb_mods_with_tokens()) ->
cb_mods_with_tokens().
parse_path_tokens([], Events) -> Events;
parse_path_tokens([<<"schemas">>=Mod|T], Events) ->
parse_path_tokens(_, [], Events) -> Events;
parse_path_tokens(_, [<<"schemas">>=Mod|T], Events) ->
[{Mod, T} | Events];
parse_path_tokens([<<"braintree">>=Mod|T], Events) ->
parse_path_tokens(_, [<<"braintree">>=Mod|T], Events) ->
[{Mod, T} | Events];
parse_path_tokens([Mod|T], Events) ->
case is_cb_module(Mod) of
parse_path_tokens(Context, [Mod|T], Events) ->
case is_cb_module(Context, Mod) of
'false' -> [];
'true' ->
{Params, List2} = lists:splitwith(fun(Elem) -> not is_cb_module(Elem) end, T),
parse_path_tokens(List2, [{Mod, Params} | Events])
{Params, List2} = lists:splitwith(fun(Elem) -> not is_cb_module(Context, Elem) end, T),
parse_path_tokens(Context, List2, [{Mod, Params} | Events])
end.

-spec is_cb_module(ne_binary()) -> boolean().
is_cb_module(Elem) ->
-spec is_cb_module(cb_context:context(), ne_binary()) -> boolean().
is_cb_module(Context, Elem) ->
try (wh_util:to_atom(<<"cb_", Elem/binary>>)):module_info('imports') of
_ -> 'true'
catch
'error':'badarg' -> 'false'; %% atom didn't exist already
_E:_R -> is_cb_module_version(Context, Elem)
end.

-spec is_cb_module_version(cb_context:context(), ne_binary()) -> boolean().
is_cb_module_version(#cb_context{api_version=ApiVersion}, Elem) ->
try (wh_util:to_atom(<<"cb_", Elem/binary, "_", ApiVersion/binary>>)):module_info('imports') of
_ -> 'true'
catch
'error':'badarg' -> 'false'; %% atom didn't exist already
_E:_R -> 'false'
end.



%%--------------------------------------------------------------------
%% @private
%% @doc
Expand Down
2 changes: 2 additions & 0 deletions applications/crossbar/src/crossbar.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
-define(APP_NAME, <<"crossbar">>).
-define(APP_VERSION, <<"0.8.0">>).

-define(VERSION_SUPPORTED, [<<"v1">>, <<"v2">>]).

-define(CACHE_TTL, whapps_config:get_integer(<<"crossbar">>, <<"cache_ttl">>, 300)).

-define(CROSSBAR_DEFAULT_CONTENT_TYPE, {<<"application">>, <<"json">>, []}).
Expand Down
17 changes: 16 additions & 1 deletion applications/crossbar/src/crossbar_bindings.erl
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,24 @@ maybe_init_mod(ModBin) ->
_ -> 'ok'
catch
_E:_R ->
lager:warning("failed to initialize ~s: ~p, ~p", [ModBin, _E, _R])
lager:warning("failed to initialize ~s: ~p, ~p. Trying other versions...", [ModBin, _E, _R]),
maybe_init_mod_versions(?VERSION_SUPPORTED, ModBin)
end.

maybe_init_mod_versions([], _) -> 'ok';
maybe_init_mod_versions([Version|Versions], ModBin) ->
Module = <<ModBin/binary, "_", Version/binary>>,
try (wh_util:to_atom(Module, 'true')):init() of
_ -> maybe_init_mod_versions(Versions, ModBin)
catch
_E:_R ->
lager:warning("failed to initialize ~s: ~p, ~p", [Module, _E, _R]),
maybe_init_mod_versions(Versions, ModBin)
end.




%%--------------------------------------------------------------------
%% @private
%% @doc
Expand Down
6 changes: 6 additions & 0 deletions applications/crossbar/src/crossbar_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
,response_redirect/3, response_redirect/4
]).
-export([response_202/2]).
-export([response_402/2]).
-export([response_faulty_request/1]).
-export([response_bad_identifier/2]).
-export([response_conflicting_docs/1]).
Expand Down Expand Up @@ -51,6 +52,11 @@ response(JTerm, Context) ->
response_202(Msg, Context) ->
create_response('success', Msg, 202, Msg, Context).

-spec response_402(cb_context:context(), wh_json:json_string()) ->
cb_context:context().
response_402(Data, Context) ->
create_response('error', <<"accept charges">>, 402, Data, Context).

%%--------------------------------------------------------------------
%% @public
%% @doc
Expand Down
84 changes: 0 additions & 84 deletions applications/crossbar/src/modules/cb_phone_numbers_v1.erl

This file was deleted.

Loading

0 comments on commit ccbbd5d

Please sign in to comment.