Skip to content

Instantly share code, notes, and snippets.

@drgarcia1986
Created February 13, 2014 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drgarcia1986/8985799 to your computer and use it in GitHub Desktop.
Save drgarcia1986/8985799 to your computer and use it in GitHub Desktop.
Classe feita em Delphi para recuperar a cotação de algumas moedas estrangeiras baseada nas versões brasileiras do site CashCash.cc
program TesteCotacao;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
Utils.Cotacao in 'Utils.Cotacao.pas';
var
oCotacao : TCotacao;
begin
oCotacao := TCotacao.Create;
try
Writeln('Hoje estamos com as seguintes contações de moedas estrangeiras');
Writeln(Format('Dolar: R$%f',[oCotacao.Dolar()]));
Writeln(Format('Euro: R$%f',[oCotacao.Euro()]));
Writeln(Format('Libra: R$%f',[oCotacao.Libra()]));
finally
oCotacao.Free;
end;
write('Pressione ENTER para sair...');
Readln;
end.
unit Utils.Cotacao;
interface
uses
IdHTTP;
type
TCotacao = class
strict private
FHTTP : TIdHTTP;
function GetCotacao(const AUrl : string; ARegEx : string = 'nacional"[ ]+value="([0-9,]+)'):Real;
public
constructor Create;
destructor Free;
function Dolar():Real;
function Euro():Real;
function Libra():Real;
end;
implementation
uses
RegularExpressions,
Classes, System.SysUtils;
{ TCotacao }
constructor TCotacao.Create;
begin
Self.FHTTP := TIdHTTP.Create(nil);
end;
function TCotacao.GetCotacao(const AUrl: string; ARegEx: string = 'nacional"[ ]+value="([0-9,]+)'): Real;
var
ssResponse : TStringStream;
rMatch : TMatch;
begin
Result := 0;
ssResponse := TStringStream.Create();
try
Self.FHTTP.Get(AUrl,ssResponse);
rMatch := TRegEx.Match(ssResponse.DataString,ARegEx,[roMultiLine]);
if rMatch.Success then
Result := StrToFloat(rMatch.Groups.Item[1].Value);
finally
ssResponse.Free;
end;
end;
function TCotacao.Libra: Real;
begin
Result := Self.GetCotacao('http://librahoje.com/');
end;
function TCotacao.Dolar: Real;
begin
Result := Self.GetCotacao('http://dolarhoje.com/');
end;
function TCotacao.Euro: Real;
begin
Result := Self.GetCotacao('http://eurohoje.com/');
end;
destructor TCotacao.Free;
begin
Self.FHTTP.Free;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment