/* Counts the number of A's in a phrase. */ func (self *Phrase) Count() body.Body { content := body.Json() input := self.Params.GetString("text") counter := strings.Count(strings.ToLower(input), "a") response := sugar.Tuple{ "data": sugar.Tuple{ "input": input, "output": counter, }, "time": time.Now(), } if counter > 0 { response["success"] = fmt.Sprintf("Has %d \"A\"s.", counter) } else { response["error"] = "Does not contain any \"A\"." } content.Set(response) return content }
func (self *Country) Find(code string) body.Body { response := body.Json() found := self.Collection.Find( db.Cond{ "code": strings.ToUpper(code), }, ) if found == nil { response.Set( sugar.Tuple{ "error": "Not found", }, ) } else { response.Set( sugar.Tuple{ "success": "Found", "data": found, }, ) } return response }
func (self *Country) Find(code string) body.Body { response := body.Json() country := &CountryValue{} err := self.Collection.Find( bson.M{ "code": strings.ToUpper(code), }, ).One(country) if err != nil { response.Set( sugar.Tuple{ "error": "Not found", }, ) } else { response.Set( sugar.Tuple{ "success": "Found", "data": sugar.Tuple{ "code": country.Code, "name": country.Name, }, }, ) } return response }
func (self *Data) Required() body.Body { response := body.Json() content := sugar.Tuple{} params := self.Params var isValid bool var messages map[string][]string isValid, messages = params.Require("email", "name", "last_name") if isValid == false { content["error"] = "Missing required data." content["data"] = messages } isValid, messages = self.Rules.Validate(params) if isValid == true { content["success"] = "Thank you." } else { content["error"] = "Some errors were found." content["data"] = messages } response.Set(content) return response }
func (self *Country) Find(code string) body.Body { response := body.Json() country := CountryValue{} row := self.Database.QueryRow( "SELECT code, name FROM countries WHERE code = ?", code, ) err := row.Scan(&country.Code, &country.Name) if err != nil { response.Set( sugar.Tuple{ "error": "Not found", }, ) } else { response.Set( sugar.Tuple{ "success": "Found", "data": sugar.Tuple{ "code": country.Code, "name": country.Name, }, }, ) } return response }
/* Changes every character in a phrase to uppercase. */ func (self *Phrase) Upper() body.Body { content := body.Json() input := self.Params.GetString("text") content.Set( sugar.Tuple{ "success": "OK", "data": sugar.Tuple{ "input": input, "output": strings.ToUpper(input), }, "time": time.Now(), }, ) return content }
func (self *Data) Validate() body.Body { response := body.Json() content := sugar.Tuple{} params := self.Params isValid, messages := self.Rules.Validate(params) if isValid == true { content["success"] = "Thank you." } else { content["error"] = "Some errors were found." content["data"] = messages } response.Set(content) return response }