Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++

RAD Studio 10.1 Berlin, REST/JSON and using a fun site: Name2Gender.com

I was thinking about how I would do a gender based analysis of our customer database without surveying our customers. The Embarcadero Community profile does not ask whether you are male or female. At the recent Evans Data Developer Relations Conference, there was a presentation about the number of developers in the world (currently around 19 million going up to 25 million in 2020). Evans Data puts the global number of female developers near 20%. In a November 2015 report, IDC said that “24% of North American developers are female”. I am always looking around for new REST based sites to test our REST Client Library Components. Wondering what our Delphi, C++Builder and RAD Studio developer gender profile looks like, I started to see how I might analyze the first names of our customers. I looked at several sources of first name analysis including the United States Social Security name list. As part of my search, I found a site that has a REST/JSON API to search for the likely gender by first name. The site is Name2Gender.com.

Using the RAD Studio 10.1 Berlin REST Debugger and REST Client Library components, I quickly tested Name2Gender’s REST API and built Delphi and C++ multi-device projects that run on Windows, OS X, iOS and Android.  Here are some screen shots and the source code. You can find a zip file with the two projects on CodeCentral at http://cc.embarcadero.com/item/30524.

The Application UI at Design and Runtime

The main application form is very simple. The form contains the following components: TButton, TEdit, TStringGrid, TMemo, TCircle (2), TLabel (5) TFDMemTable, TRESTClient, TRESTRequest, TRESTResponse, TRESTResponseDataSetAdapter, TBindSourceDB and TBindingsList. I used the REST Debugger’s “CopyToClipboard” to add the REST Client Library components to my application with the settings from my test of the Name2Gender API.

 

The following are several screen grabs showing my app in action on Windows (I also have created form views for OS X, iOS and Android). I tested likely male and female names. I also tried less likely names and one case where I did not enter a name. If you look at the source code, you’ll see where I use the returned “accuracy” percentage to set the colors for the two TCircle indicators.

 

 

 

 

 

The Project Group in the Project Manager

The project group contains C++Builder and Delphi Multi-Device FireMonkey projects. For each form, I created Windows, OS X, iOS and Android form views. You will find the different FMX form inheritance files (for Windows, OS X, iPhone 4.7 inch, and Android 4 inch form factors) in the source code zip file on CodeCentral.

 

The C++ source code

Here is the C++ source code for the button OnClick handler. I also set the starting color for each TCircle in the OnFormCreate event handler. Notice, that I arbitrarily used the accuracy result to set the second TCircle to Green, Yellow or Red.

void __fastcall TForm3::Button1Click(TObject *Sender)
{
  Memo1->Lines->Clear();
  StatusCircle->Fill->Color = TAlphaColors::Lightgray;
  AccuracyCircle->Fill->Color = TAlphaColors::Lightgray;
  RESTRequest1->AddParameter("Name",UpperCase(Edit1->Text));
  RESTRequest1->Execute();
  RESTStatusLabel->Text = IntToStr(RESTResponse1->StatusCode)+"/"+RESTResponse1->StatusText;
  Memo1->Lines->Add(RESTResponse1->JSONText);
  if (RESTResponse1->StatusCode != 200) {
	StatusCircle->Fill->Color = TAlphaColors::Red;
	switch (RESTResponse1->StatusCode) {
	  case 403:
		RESTRequestErrorLabel->Text = "Forbidden access to Private section";
		break;
	  case 460:
		RESTRequestErrorLabel->Text = "User already registered";
		break;
	  case 461:
		RESTRequestErrorLabel->Text = "Not Found by required parameters";
		break;
	  case 462:
		RESTRequestErrorLabel->Text = "Invalid user session id";
		break;
	  case 463:
		RESTRequestErrorLabel->Text = "Name cannot be empty";
		break;
	  case 464:
		RESTRequestErrorLabel->Text = "Ip and Key cannot be null at the same time";
		break;
	  case 465:
		RESTRequestErrorLabel->Text = "Current available calls for ApiKey is 0";
		break;
	  case 466:
		RESTRequestErrorLabel->Text = "Ip request day limit";
		break;
	  case 500:
		RESTRequestErrorLabel->Text = "Internal server exception";
		break;
	default:
	  RESTRequestErrorLabel->Text = "Unknown Error";
	};
  }
  else {
	StatusCircle->Fill->Color = TAlphaColors::Green;
	RESTRequestErrorLabel->Text = "";
  };
  if (RESTResponse1->StatusCode == 200) {
	AccuracyResultLabel->Text = FDMemTable1accuracy->AsString;
	if (FDMemTable1accuracy->AsFloat >= 90.0) {
	  AccuracyCircle->Fill->Color = TAlphaColors::Green;
	}
	else if (FDMemTable1accuracy->AsFloat >= 70.0) {
	  AccuracyCircle->Fill->Color = TAlphaColors::Yellow;
	}
	else {
	  AccuracyCircle->Fill->Color = TAlphaColors::Red;
	}
  }
  else {
	AccuracyResultLabel->Text = "No Result";
	AccuracyCircle->Fill->Color = TAlphaColors::Red;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm3::FormCreate(TObject *Sender)
{
  StatusCircle->Fill->Color = TAlphaColors::Lightgray;
  AccuracyCircle->Fill->Color = TAlphaColors::Lightgray;
}

 

The Delphi source code

Here is the Delphi source code for the button OnClick handler. I also set the starting color for each TCircle in the OnFormCreate event handler. Notice, that I arbitrarily used the accuracy result to set the second TCircle to Green, Yellow or Red.

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Clear;
  StatusCircle.Fill.Color := TAlphaColors.Lightgray;
  AccuracyCircle.Fill.Color := TAlphaColors.Lightgray;
  RESTRequest1.Params[0].Value := UpperCase(Edit1.Text);
  RESTRequest1.Execute();
  RESTStatusLabel.Text := IntToStr(RESTResponse1.StatusCode)+'/'+RESTResponse1.StatusText;
  Memo1.Lines.Add(RESTResponse1.JSONText);
  if RESTResponse1.StatusCode <> 200 then begin
    StatusCircle.Fill.Color := TAlphaColors.Red;
    case RESTResponse1.StatusCode of
      403: RESTRequestErrorLabel.Text := 'Forbidden access to Private section';
      460: RESTRequestErrorLabel.Text := 'User already registered';
      461: RESTRequestErrorLabel.Text := 'Not Found by required parameters';
      462: RESTRequestErrorLabel.Text := 'Invalid user session id';
      463: RESTRequestErrorLabel.Text := 'Name cannot be empty';
      464: RESTRequestErrorLabel.Text := 'Ip and Key cannot be null at the same time';
      465: RESTRequestErrorLabel.Text := 'Current available calls for ApiKey is 0';
      466: RESTRequestErrorLabel.Text := 'Ip request day limit';
      500: RESTRequestErrorLabel.Text := 'Internal server exception';
    else
      RESTRequestErrorLabel.Text := 'Unknown Error'
    end;
  end
  else begin
    StatusCircle.Fill.Color := TAlphaColors.Green;
    RESTRequestErrorLabel.Text := '';
  end;
  if RESTResponse1.StatusCode = 200 then begin
    AccuracyResultLabel.Text := FDMemTable1accuracy.AsString;
    if FDMemTable1accuracy.AsFloat >= 90.0 then
      AccuracyCircle.Fill.Color := TAlphaColors.Green
    else if FDMemTable1accuracy.AsFloat >= 70.0 then
      AccuracyCircle.Fill.Color := TAlphaColors.Yellow
    else
      AccuracyCircle.Fill.Color := TAlphaColors.Red;
  end
  else begin
    AccuracyResultLabel.Text := 'No Result';
    AccuracyCircle.Fill.Color := TAlphaColors.Red;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  StatusCircle.Fill.Color := TAlphaColors.Lightgray;
  AccuracyCircle.Fill.Color := TAlphaColors.Lightgray;
end;

 

Project Group Source Code on CodeCentral

You can find the complete source code to both projects in CodeCentral at http://cc.embarcadero.com/item/30524

A Final Note

I have not done the analysis of our Developer Network members, but I will use the REST component settings from these projects, to read through our customer database and see what I find. The Name2Gender site has several levels of pricing depending on how many REST calls you make.

 

[DownloadButton Product=’RAD’ Caption=’RAD Studio 10.1 Berlin – Try for Free’]

[BuyButton Product=’RAD’ Caption=’RAD Studio 10.1 Berlin – Now Available for Purchase’]


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES