Forex

 view release on metacpan or  search on metacpan

Forex.pm  view on Meta::CPAN


=back

 my $oxe = new Forex( OXR_HOME => 'https://openexchangerates.org',
                      API_HOOK => 'api',
                      APP_ID   => 'temp-e091fc14b3884a516d6cc2c299a',
                      BASE     => 'AUD');

=cut

sub new {
	my $class = shift;
	my $self = {@_};
	bless $self, $class;
	$self->{ 'OXR_HOME' } = 'http://openexchangerates.org'     unless defined ($self->{'OXR_HOME'});
	$self->{ 'API_HOOK' } = 'api'                              unless defined ($self->{'API_HOOK'});
	$self->{ 'APP_ID'   } = 'temp-e091fc14b3884a516d6cc2c299a' unless defined ($self->{APP_ID});
	$self->{ 'BASE'     } = 'USD'                              unless defined ($self->{'BASE'});
	return $self;
}

=pod

=head3 get_rate_of( $currency, <$date> )

This method returns forex rate for C<$currency> on $date in BASE currency, $date should be in C<yyyy-mm-dd> format.

my $AUD = $oxr->get_rate_of( 'AUD' , '2012-09-10' );

=cut

sub get_rate_of {
	my ($self, $currency, $date ) = @_;

	if (!$date)
		{ my $_d = DateTime->now();
		  $date = join "-",($_d->year, $_d->month, $_d->day); }
		  
	if ($date !~ m/-/)
		{ $LASTERROR = 1;
		  $self->{ERROR} = 'date parameter not is in yyyy-mm-dd format';
		  return undef;                                               }

	if( !$self->{ 'CURRENCIES'} || !$self->{'CURRENCIES'}->{$date})
	  { $self->get_rates( $date ); }
		  
	return $self->{ 'CURRENCIES' }->{ $currency }->{ $date };
}

sub _fetch_data {
	my ($self) = @_;
	my ($response);
		   
	if ( !$self->{'LWP_OBJ'} )
		{  $self->{ 'LWP_OBJ' } = new LWP::UserAgent();
		   $self->{ 'LWP_OBJ' }->timeout( $self->{'TIMEOUT'} );  }

	      $response = $self->{'LWP_OBJ'}->get( $self->{ 'REQUEST_URL' } );

	if ( $response->is_success )
	   { $self->{ 'CONTENT' } = decode_json $response->decoded_content; $LASTERROR = undef; }
	else
		{ $self->{ 'ERROR'} = decode_json $response->decoded_content;
		  $LASTERROR = 1; }
		
	delete $self->{ 'REQUEST_URL' };

	1;
}

=pod

=head3 get_rates_from ( $from_date , $to_date ) 

downloads and fills CURRENCIES hash for $from_date to $to_date
both dates should be in C<yyyy-mm-dd> formate

=cut

sub get_rates_from {
	my ($self, $from , $to) = @_;
	my ( $from_dt, $to_dt );
	
	if ( $from !~ m/-/ && $to !~ m/-/ )
		{ $self->{ERROR} = "to date or from date not in yyyy-mm-dd formate";
		  $LASTERROR = 1; 
		  return undef;                                                    }
		
	   ( $y, $m , $d ) = split '-' , $from;
	     $from_dt = new DateTime( year => $y, month => $m , day => $d );
	   ( $y, $m , $d ) = split '-' , $to;
	     $to_dt   = new DateTime( year => $y, month => $m , day => $d );
	
	while ( DateTime->compare( $from_dt , $to_dt ) )
	{ $self->get_rates( $from_dt->ymd('-') );
	  $from_dt->add( days => 1 );	              }
	
	1;
}

=pod

=head3 get_rates ( <$day> ) 

downloads and fills in values for all currencies in the CURRENCIES hash for given C<$day>
$day should be in C<yyyy-mm-dd> formate if $day is skipped , it uses C<todays date>,

=cut

sub get_rates {
	my ($self, $day) = @_;
	my ($request_day, $response_day );

	if ( !$day )
	{ $request_day = DateTime->now();
	  $request_day = new DateTime ( year  => $request_day->year,
				        month => $request_day->month,
			                day   => $request_day->day );     	 }
	else
	   { my ( $y, $m , $d ) = split '-' , $day;
	     $request_day = new DateTime( year => $y, month => $m , day => $d ); }
	
	if ( !$request_day )



( run in 1.793 second using v1.01-cache-2.11-cpan-39bf76dae61 )