[Crypto] CoinMarket cap API

parent c645fd27
Pipeline #1303 failed with stages
in 1 minute and 42 seconds
<?php
namespace Onyx\Coinmarket;
use Onyx\Coinmarket\Helpers\Network\Http;
use Onyx\Coinmarket\Helpers\String\Symbols;
class Client extends Http
{
//---------------------------------------------------------------------------------
// Public Methods
//---------------------------------------------------------------------------------
/**
* @param string $name
* @return array
*/
public function getTicker(string $name): array
{
$url = sprintf(Constants::$ticket, Symbols::getTickerId($name));
try {
return $this->getJson($url, 5)[0];
} catch (\Exception $ex) {
return [];
}
}
}
\ No newline at end of file
<?php
namespace Onyx\Coinmarket;
class Constants
{
public static $ticket = 'https://api.coinmarketcap.com/v1/ticker/%s';
}
\ No newline at end of file
<?php
declare(strict_types=1);
namespace Onyx\Coinmarket\Helpers\Bot;
class MessageGenerator
{
/**
* @param array $data
* @return string
*/
public static function generateTickerMessage(array $data): string
{
$output = $data['name'] . ' (' . $data['symbol'] . ') <br />';
$output .= '<strong>$' . $data['price_usd'] . '</strong>';
return $output;
}
}
\ No newline at end of file
<?php
namespace Onyx\Coinmarket\Helpers\Network;
use Barryvdh\Debugbar\Facade as DebugBar;
use GuzzleHttp\Client as Guzzle;
class Http
{
/**
* @var \GuzzleHttp\Client
*/
protected $guzzle;
public function __construct()
{
$this->setupGuzzle();
}
private function setupGuzzle()
{
$this->guzzle = new Guzzle();
}
/**
* Request an URL expecting JSON to be returned.
*
* @param $url
* @param $cache integer
*
* @throws CoinmarketOffline
*
* @return array
*/
public function getJson($url, $cache = 5)
{
if (!$this->guzzle instanceof Guzzle) {
$this->setupGuzzle();
}
$sum = md5($url);
if ($cache != 0 && \Cache::has($sum)) {
return \Cache::get($sum);
}
DebugBar::startMeasure($sum, $url);
$response = $this->guzzle->get($url);
DebugBar::stopMeasure($sum);
if ($response->getStatusCode() != 200) {
throw new CoinmarketOffline();
}
if ($cache != 0) {
\Cache::put($sum, json_decode($response->getBody(), true), $cache);
}
return json_decode($response->getBody(), true);
}
}
class CoinmarketOffline extends \Exception
{
}
<?php
declare(strict_types=1);
namespace Onyx\Coinmarket\Helpers\String;
/**
* Class Symbols
* @package Onyx\Coinmarket\Helpers\String
*/
class Symbols
{
/**
* @param string $name
* @return string
*/
public static function getTickerId(string $name): string
{
$name = strtolower($name);
switch ($name) {
case 'btc':
return 'bitcoin';
case 'ltc':
return 'litecoin';
default:
return $name;
}
}
}
\ No newline at end of file
<?php
namespace PandaLove\Http\Controllers\Coinmarket;
use Illuminate\Http\Request as Request;
use Illuminate\Routing\Redirector as Redirect;
use Illuminate\Support\Facades\Response;
use Illuminate\View\Factory as View;
use Onyx\Coinmarket\Client;
use Onyx\Coinmarket\Helpers\Bot\MessageGenerator;
use PandaLove\Http\Controllers\Controller;
class ApiV1Controller extends Controller
{
private $view;
private $request;
private $redirect;
protected $layout = 'layouts.master';
public function __construct(View $view, Redirect $redirect, Request $request)
{
parent::__construct();
$this->view = $view;
$this->request = $request;
$this->redirect = $redirect;
}
//---------------------------------------------------------------------------------
// Destiny GET
//---------------------------------------------------------------------------------
public function getSymbol($name)
{
$client = new Client();
$data = $client->getTicker($name);
if (empty($data)) {
return $this->_error('Could not find symbol: ' . $name);
}
return Response::json([
'error' => false,
'message' => MessageGenerator::generateTickerMessage($data)
], 200);
}
//---------------------------------------------------------------------------------
// XPrivate Functions
//---------------------------------------------------------------------------------
private function _error($message)
{
return Response::json([
'error' => true,
'message' => $message,
], 200);
}
}
......@@ -40,6 +40,9 @@ Route::controller('/backstage/destiny', 'Backstage\DestinyController');
Route::controller('/backstage/halo5', 'Backstage\Halo5Controller');
Route::controller('/backstage', 'Backstage\IndexController');
//# Coinmarket
Route::controller('/coinmarket/api/v1', 'Coinmarket\ApiV1Controller');
//# Other
Route::controller('/account', 'AccountController');
Route::controller('/comment', 'CommentController');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment