The app I’m working on in my spare time (BeerDrinkin) requires a modest amount of user data to in the future provide the best possible suggestions for new beers the user might like to try. Part of building a great model of my users is knowing thier gender.
When a user authenticates within BeerDrinkin using Facebook, I can simply parse the returned information and add the users gender to my database. Unfortunately, not all social authentication providers were born equal. With the recent addition of Google Auth, which I added to allow my father (who isn’t even sure what Facebook is) to use my app, I was unable to get the users gender.
Its for this reason that I’ve created a library that allows me to query Genderize. Genderize is a restful service which allows me to determine the gender of a user based on only a first name. It offers a free tier which includes the ability to query 1000 names a day which is more than enough for my requirements. If I find myself hitting the limit I’ll firstly buy a bottle of champagne to celebrate high user adoption and to drink away the thoughts of my Azure bill. On a serious note, Genderize provide the option to upgrade the account plan for a nominal fee to 100,000 users a month.
Given that BeerDrinkin does some sneaky UX to make a Facebook auth more likely (I delay showing the Google sign-in button for a few seconds so the user intially is confronted with only 1 option. Its very subtle but seems to help push Facebook as the prefered option), the free teir should be perfect for me.
Creating a PCL to interact with Genderize
To get started I headed over to the Genderize’s documentation to see what response I should expect back when querying the service. It’s actually increbily easy to use this service so much so that the entire PCL consists of no more than 100 lines of code.
Sample JSON response
{ “name”:“peter”, “gender”:“male”, “probability”:“0.99”, “count”:796}
Corresponding C# model
public class Response{ private string gender { get; set; } [JsonProperty("name")] public string Name { get; set; } [JsonConverter(typeof(StringEnumConverter))] public Gender Gender { get; set; } [JsonProperty("probability")] public string Probability { get; set; } [JsonProperty("count")] public int Count { get; set; }}public enum Gender{ Male, Female, None,}
One thing to note is that I’m using Json.NET’s JsonConverter to deal with converting from a string gender to an enum. This is just one of the many featuress of Json.NET that make it a pleasure to use.
Genderize Client Code
public class Client{ public Client() { } public async Task GenderizeSingleName(string name) { if (_client == null) _client = new HttpClient(); Response model; var url = string.Format("http://api.genderize.io/?name={0}", name); var response = await _client.GetAsync(url); var jsonString = response.Content.ReadAsStringAsync(); jsonString.Wait(); model = JsonConvert.DeserializeObject(jsonString.Result); return model; } HttpClient _client;}
As always, its open source
If you want to use the library, you can go ahead and grab a copy from my GitHub page. Once I get home from Australia I’ll add more features and publish to Nuget.