Created
March 1, 2010 16:40
-
-
Save leandrosilva/318531 to your computer and use it in GitHub Desktop.
Sample code from Restfulierl client-side
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| %% | |
| %% Restfulierl, a member of Restfulie initiative. | |
| %% | |
| %% @author Leandro Silva <leandrodoze@gmail.com> | |
| %% @copyright 2009 Leandro Silva. | |
| %% | |
| %% See more about Restfulie initiative on http://restfulie.caelum.com.br. | |
| %% | |
| %% @doc start point to use Restfuilerl. | |
| -module(restfulierl). | |
| -author('Leandro Silva <leandrodoze@gmail.com>'). | |
| %% operation & maintenance api | |
| -export([start/0, stop/0]). | |
| %% external api | |
| -export([get_resource/1, post_resource/1, post_resource/2, put_resource/1, delete_resource/1]). | |
| -include("restfulierl.hrl"). | |
| %% | |
| %% Operation & Maintenance API | |
| %% | |
| %% @spec start() -> ok | |
| %% @doc Start the Restfulierl application and dependencies. | |
| start() -> | |
| restfulierl_deps:ensure(), | |
| ensure_started(inets), | |
| ensure_started(xmerl), | |
| ok. | |
| %% @spec stop() -> ok | |
| %% @doc Stop the Restfulierl application and dependencies (take care!). | |
| stop() -> | |
| Res = application:stop(restfulierl), | |
| application:stop(xmerl), | |
| application:stop(inets), | |
| Res. | |
| %% | |
| %% External API | |
| %% | |
| %% @spec get_resource(Uri) -> Resource | |
| %% @doc Get a resource from specified Uri. | |
| get_resource(Uri) -> | |
| HttpResponse = http:request(Uri), | |
| _Resource = restfulierl_resource:from_http_response(Uri, HttpResponse). | |
| %% @spec post_resource(Resource) -> Response | |
| %% @doc Post a resource to Resource#resource.uri. | |
| post_resource(Resource) -> | |
| post_resource(Resource, Resource#resource.uri). | |
| %% @spec post_resource(Resource, Transition) -> Response | |
| %% @doc Post a resource to specified Transition. | |
| post_resource(_Resource, _Transition) when is_atom(_Transition) -> | |
| yet_not_implemented; | |
| %% @spec post_resource(Resource, Uri) -> Response | |
| %% @doc Post a resource to specified Uri. | |
| post_resource(_Resource, Uri) when is_list(Uri) -> | |
| Headers = [], | |
| ContentType = "application/xml", | |
| Body = "<order></order>", %restfulierl_xml_marshaler:to_xml(Resource), | |
| HttpOptions = [], | |
| Options = [{body_format, string}], | |
| _HttpResponse = http:request(post, | |
| {Uri, Headers, ContentType, Body}, HttpOptions, Options). | |
| %% @spec put_resource(Resource) -> Response | |
| %% @doc Put a resource to Resource#resource.uri. | |
| put_resource(_Resource) -> | |
| yet_not_implemented. | |
| %% @spec delete_resource(Resource) -> Response | |
| %% @doc Delete a resource from Resource#resource.uri. | |
| delete_resource(_Resource) -> | |
| yet_not_implemented. | |
| %% | |
| %% Internal API | |
| %% | |
| %% ensure that required application is started | |
| ensure_started(App) -> | |
| case application:start(App) of | |
| ok -> | |
| ok; | |
| {error, {already_started, App}} -> | |
| ok | |
| end. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| %% | |
| %% Restfulierl, a member of Restfulie initiative. | |
| %% | |
| %% @author Leandro Silva <leandrodoze@gmail.com> | |
| %% @copyright 2009 Leandro Silva. | |
| %% | |
| %% See more about Restfulie initiative on http://restfulie.caelum.com.br. | |
| %% | |
| %% @doc test module for Restfuilerl. | |
| -module(restfulierl_test). | |
| -author('Leandro Silva <leandrodoze@gmail.com>'). | |
| -include("restfulierl.hrl"). | |
| -include_lib("eunit/include/eunit.hrl"). | |
| %% | |
| %% Setup | |
| %% | |
| restfulierl_test_() -> | |
| {setup, fun setup/0, []}. | |
| setup() -> | |
| restfulierl:start(). | |
| %% | |
| %% Tests | |
| %% | |
| -define(RESOURCE_URI, "http://restfulie-test.heroku.com/orders/11.xml"). | |
| get_resource_test() -> | |
| Resource = restfulierl:get_resource(?RESOURCE_URI), | |
| % {resource,"http://restfulie-test.heroku.com/orders/11.xml", | |
| % {order,[], | |
| % [{'created-at',[],["2010-01-01T05:15:45Z"]}, | |
| % {'customer-name',[],["Leandro Silva"]}, | |
| % {id,[],["11"]}, | |
| % {status,[],["unpaid"]}, | |
| % {'updated-at',[],["2010-01-01T05:15:45Z"]}, | |
| % "\n"]}, | |
| % [{transition,latest, | |
| % "http://restfulie-test.heroku.com/orders/11"}, | |
| % {transition,pay, | |
| % "http://restfulie-test.heroku.com/orders/11/pay"}, | |
| % {transition,cancel, | |
| % "http://restfulie-test.heroku.com/orders/11"}]}} | |
| % matching uri | |
| ?assertEqual(Resource#resource.uri, ?RESOURCE_URI), | |
| % matching state | |
| OrderState = Resource#resource.state, | |
| {order, [], OrderAttributes} = OrderState, | |
| [CreatedAt | NextOrderAttributes1] = OrderAttributes, | |
| ?assertMatch({'created-at',[],["2010-01-01T05:15:45Z"]}, CreatedAt), | |
| [CustomerName | NextOrderAttributes2] = NextOrderAttributes1, | |
| ?assertMatch({'customer-name',[],["Leandro Silva"]}, CustomerName), | |
| [Id | NextOrderAttributes3] = NextOrderAttributes2, | |
| ?assertMatch({id,[],["11"]}, Id), | |
| [Status | NextOrderAttributes4] = NextOrderAttributes3, | |
| ?assertMatch({status,[],["unpaid"]}, Status), | |
| [UpdateAt | _] = NextOrderAttributes4, | |
| ?assertMatch({'updated-at',[],["2010-01-01T05:15:45Z"]}, UpdateAt), | |
| % matching transitions | |
| OrderTransitions = Resource#resource.transitions, | |
| [Latest | NextOrderTransitions1] = OrderTransitions, | |
| ?assertMatch({transition, latest, "http://restfulie-test.heroku.com/orders/11"}, Latest), | |
| [Pay | NextOrderTransitions2] = NextOrderTransitions1, | |
| ?assertMatch({transition, pay, "http://restfulie-test.heroku.com/orders/11/pay"}, Pay), | |
| [Cancel | _] = NextOrderTransitions2, | |
| ?assertMatch({transition, cancel, "http://restfulie-test.heroku.com/orders/11"}, Cancel). | |
| post_new_resource_test() -> | |
| Resource = #resource{ | |
| uri = ?RESOURCE_URI, | |
| state = {order, [], []}}, | |
| Response = restfulierl:post_resource(Resource), | |
| ?assertMatch({ok, _}, Response). | |
| post_new_resource_to_transition_test() -> | |
| Resource = #resource{state = {order, [], []}}, | |
| Response = restfulierl:post_resource(Resource, pay), | |
| ?assertMatch(yet_not_implemented, Response). | |
| post_new_resource_to_uri_test() -> | |
| Resource = #resource{state = {order, [], []}}, | |
| Response = restfulierl:post_resource(Resource, ?RESOURCE_URI), | |
| ?assertMatch({ok, _}, Response). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment