Commits

jer...@jeremylatt.com  committed 813ba46

more xhr impl

  • Participants
  • Parent commits fea416e

Comments (0)

Files changed (1)

 -module(xhr).
+
+%% ready states
+-define(UNSENT, 0).
+-define(OPENED, 1).
+-define(HEADERS_RECEIVED, 2).
+-define(LOADING, 3).
+-define(DONE, 4).
+
 -record(xhr, {
-		  ready_state = 0,
+		  ready_state = ?UNSENT,
 		  status = 0,
 		  method = get,
 		  url,
 		  request_id
 		 }).
 -define(is_xhr(X), is_record(X, xhr)).
+
 -export([new/0,
 		 open/3,
 		 send/1]).
 	#xhr{}.
 
 
-open(XHR, Method, URL) when (?is_xhr(XHR) andalso
-							 is_list(Method) andalso
-							 is_list(URL)) ->
-	{ok, XHR#xhr{method=method_string_to_atom(Method),
+open(XHR=#xhr{ready_state=?UNSENT}, Method, URL) 
+  when is_list(Method) andalso
+	   is_list(URL) ->
+	{ok, XHR#xhr{ready_state=?OPENED,
+				 method=method_string_to_atom(Method),
 				 url=URL}}.
 
 
-send(XHR=#xhr{ready_state=0, method=Method, http_options=HTTPOptions, options=Options}) ->
+send(XHR=#xhr{ready_state=?OPENED, method=Method, http_options=HTTPOptions, options=Options}) ->
 	case http:request(Method, request(XHR), HTTPOptions, Options) of
 		{ok, RequestId} ->
-			{ok, XHR#xhr{ready_state=1, request_id=RequestId}};
+			{ok, XHR#xhr{request_id=RequestId}};
 		{error, Reason} ->
 			{error, Reason}
 	end.
 
 
-request(#xhr{method=M, url=URL, headers=Headers}) when ((M == get) orelse
-														(M == options) orelse
-														(M == trace)) ->
+request(#xhr{method=M, url=URL, headers=Headers}) 
+  when (M == get) orelse
+	   (M == options) orelse
+	   (M == head) orelse
+	   (M == trace) orelse
+	   (M == delete) ->
 	{URL, Headers};
-request(#xhr{url=URL, headers=Headers, body=Body}) ->
+request(#xhr{method=M, url=URL, headers=Headers, body=Body})
+  when (M == put) orelse
+	   (M == post) ->
 	{URL, Headers, content_type(Headers), Body}.
 
 
 	put;
 method_ustring_to_atom("POST") ->
 	post;
+method_ustring_to_atom("HEAD") ->
+	head;
 method_ustring_to_atom("DELETE") ->
 	delete;
 method_ustring_to_atom("TRACE") ->