Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for fixer.io #130

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions config/currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

/*
|--------------------------------------------------------------------------
| API Key for OpenExchangeRates.org
| API Keys for different services
|--------------------------------------------------------------------------
|
| Only required if you with to use the Open Exchange Rates api. You can
| always just use Yahoo, the current default.
|
*/

'api_key' => '',
'api_key' => [
'openexchangerates' => '', // OpenExchangeRates.org
'fixer' => '', // fixer.io
],

/*
|--------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ class CreateCurrencyTable extends Migration
* @var string
*/
protected $table_name;
protected $table_connection;

/**
* Create a new migration instance.
*/
public function __construct()
{
$this->table_connection = config('currency.drivers.database.connection');
$this->table_name = config('currency.drivers.database.table');
}

Expand All @@ -26,7 +28,7 @@ public function __construct()
*/
public function up()
{
Schema::create($this->table_name, function ($table) {
Schema::connection($this->table_connection)->create($this->table_name, function ($table) {
$table->increments('id')->unsigned();
$table->string('name');
$table->string('code', 10)->index();
Expand Down
51 changes: 46 additions & 5 deletions src/Console/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class Update extends Command
*/
protected $signature = 'currency:update
{--o|openexchangerates : Get rates from OpenExchangeRates.org}
{--g|google : Get rates from Google Finance}';
{--g|google : Get rates from Google Finance}
{--f|fixer : Get rates from fixer.io}';

/**
* The console command description.
Expand Down Expand Up @@ -67,7 +68,7 @@ public function handle()
}

if ($this->input->getOption('openexchangerates')) {
if (!$api = $this->currency->config('api_key')) {
if (!$api = $this->currency->config('api_key.openexchangerates')) {
$this->error('An API key is needed from OpenExchangeRates.org to continue.');

return;
Expand All @@ -76,6 +77,17 @@ public function handle()
// Get rates from OpenExchangeRates
return $this->updateFromOpenExchangeRates($defaultCurrency, $api);
}

if ($this->input->getOption('fixer')) {
if (!$api = $this->currency->config('api_key.fixer')) {
$this->error('An API key is needed from fixer.io to continue.');

return;
}

// Get rates
$this->updateFromFixerIO($defaultCurrency, $api);
}
}

/**
Expand Down Expand Up @@ -133,18 +145,47 @@ private function updateFromGoogle($defaultCurrency)
if (Str::contains($response, 'bld>')) {
$data = explode('bld>', $response);
$rate = explode($code, $data[1])[0];

$this->currency->getDriver()->update($code, [
'exchange_rate' => $rate,
]);
}
else {
} else {
$this->warn('Can\'t update rate for ' . $code);
continue;
}
}
}

private function updateFromFixerIO($defaultCurrency, $api)
{
$this->info('Updating currency exchange rates from fixer.io...');

// Make request
$content = json_decode($this->request("http://data.fixer.io/api/latest?access_key={$api}&format=1&base={$defaultCurrency}"));

// Error getting content?
if (isset($content->error)) {
$this->error($content->error->code . ' ' . $content->error->type);

return;
}

// Parse timestamp for DB
$timestamp = (new DateTime())->setTimestamp($content->timestamp);

// Update each rate
foreach ($content->rates as $code => $value) {
$this->currency->getDriver()->update($code, [
'exchange_rate' => $value,
'updated_at' => $timestamp,
]);
}

$this->currency->clearCache();

$this->info('Update!');
}

/**
* Make the request to the sever.
*
Expand Down