Created
October 17, 2011 04:51
-
-
Save kevinburke/1291949 to your computer and use it in GitHub Desktop.
Help me refactor this Clojure code.
This file contains 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
(defn add-phone | |
"Add a phone number to a given service. There are a few steps we have to take: | |
- Validate the service name and token | |
- Validate the phone number | |
- Send a text message to the number | |
- Store the number in the database | |
Arguments: | |
request - an authorized HTTP request | |
Returns: | |
201 Resource Created - phone number added to a service | |
400 Bad Request - invalid phone number or bad request" | |
[request auth-type] | |
(do | |
(println "Url requested is " (:request-method request) " " (:uri request)) | |
(println request) | |
(def auth-header ((:headers request) "authorization")) | |
(def auth-map (utils/get-auth-header auth-type auth-header)) | |
(if (model/invalid-user-and-pass auth-map auth-type) | |
{:status 401, | |
:headers {"WWW-Authenticate" "Basic realm=\"smsauth.herokuapp.com\""}} | |
(do | |
(def raw-phone-no (:phone_number (:params request))) | |
(def service-name (:service-name auth-map)) | |
(if raw-phone-no | |
(do | |
(def valid-phone-no (utils/parse-phone-no raw-phone-no)) | |
; XXX get the service id some other way. | |
(def service-map (model/service-in-db service-name)) | |
(def service-id (service-map :id)) | |
; XXX check that service doesn't already have phone number | |
(model/store-phone-no :numbers service-id valid-phone-no) | |
(twilio/send-sms-verify twilio/sms-url valid-phone-no service-name) | |
; XXX jsonify this, send more information, document it. | |
{:status 201, | |
:body (json/json-str { | |
:service_name service-name, | |
:phone_number valid-phone-no, | |
:status "active", | |
:uri (:uri request) | |
; should have some kind of unique id for the phone number | |
; here. | |
; also return the date | |
})}) | |
{:status 400, | |
:body (json/json-str {:error "Please include a phone_number."})}))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment