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

Add reserve model #40

Merged
merged 6 commits into from
Feb 1, 2015
Merged
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
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,41 @@ $transaction = $card->createTransaction('foo@bar.com', '2.0', 'BTC');
$transaction->commit();
```

### Get all public transactions
```php
require_once 'vendor/autoload.php';

use \Bitreserve\BitreserveClient as Client;

// Initialize the client. In this case, we don't need an
// AUTHORIZATION_TOKEN because the Ticker endpoint is public.
$client = new Client();

// Get all public transactions.
$transactions = $client->getReserve()->getTransactions();
```

Or you could get a specific public transaction:

```php
// Get one public transaction.
$transaction = $client->getReserve()->getTransactionById('a97bb994-6e24-4a89-b653-e0a6d0bcf634');
```

### Get reserve status
```php
require_once 'vendor/autoload.php';

use \Bitreserve\BitreserveClient as Client;

// Initialize the client. In this case, we don't need an
// AUTHORIZATION_TOKEN because the Ticker endpoint is public.
$client = new Client();

// Get the reserve summary of all the obligations and assets within it.
$statistics = $client->getReserve()->getStatistics();
```

## Contributing & Development

#### Contributing
Expand Down
14 changes: 14 additions & 0 deletions examples/Reserve.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

require_once 'vendor/autoload.php';

use \Bitreserve\BitreserveClient as Client;

// Initialize the client. In this case, we don't need an
// AUTHORIZATION_TOKEN because the Ticker endpoint is public.
$client = new Client();

// Get the reserve summary of all the obligations and assets within it.
$statistics = $client->getReserve()->getStatistics();

print_r($statistics);
38 changes: 29 additions & 9 deletions lib/Bitreserve/BitreserveClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Bitreserve\HttpClient\HttpClient;
use Bitreserve\HttpClient\HttpClientInterface;
use Bitreserve\HttpClient\Message\ResponseMediator;
use Bitreserve\Model\Reserve;
use Bitreserve\Model\Ticker;
use Bitreserve\Model\Token;
use Bitreserve\Model\Transaction;
Expand All @@ -22,6 +23,13 @@ class BitreserveClient
*/
private $httpClient;

/**
* Current Reserve object.
*
* @var Reserve
*/
private $reserve;

/**
* @var array
*/
Expand Down Expand Up @@ -176,30 +184,42 @@ public function getToken()

/**
* Return the public view of any transaction.
* Be advised that this method has the potential to return a great deal of data.
*
* @return Transaction The transaction identified by a given id.
*
* @deprecated Method deprecated in Release 1.2.0
*/
public function getTransactionById($id)
{
$data = $this->get(sprintf('/reserve/transactions/%s', $id));

return new Transaction($this, $data);
return $this->getReserve()->getTransactionById($id);
}

/**
* Return the public view of all transactions from the beginning of time.
* Be advised that this method has the potential to return a great deal of data.
*
* @return array The list all public transactions.
*
* @deprecated Method deprecated in Release 1.2.0
*/
public function getTransactions()
{
$data = $this->get('/reserve/transactions');
return $this->getReserve()->getTransactions();
}

/**
* Get a reserve object or create a new one.
*
* @return Reserve The reserve object.
*/
public function getReserve()
{
if ($this->reserve) {
return $this->reserve;
}

return array_map(function($transaction) {
return new Transaction($this, $transaction);
}, $data);
$this->reserve = new Reserve($this);

return $this->reserve;
}

/**
Expand Down
51 changes: 51 additions & 0 deletions lib/Bitreserve/Model/Reserve.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Bitreserve\Model;

use Bitreserve\BitreserveClient;

/**
* Reserve Model.
*/
class Reserve extends BaseModel implements ReserveInterface
{
/**
* Constructor.
*
* @param BitreserveClient $client Bitreserve client
*/
public function __construct(BitreserveClient $client)
{
$this->client = $client;;
}

/**
* {@inheritdoc}
*/
public function getStatistics()
{
return $this->client->get('/reserve/statistics');
}

/**
* {@inheritdoc}
*/
public function getTransactionById($id)
{
$data = $this->client->get(sprintf('/reserve/transactions/%s', $id));

return new Transaction($this->client, $data);
}

/**
* {@inheritdoc}
*/
public function getTransactions()
{
$data = $this->client->get('/reserve/transactions');

return array_map(function($transaction) {
return new Transaction($this->client, $transaction);
}, $data);
}
}
30 changes: 30 additions & 0 deletions lib/Bitreserve/Model/ReserveInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Bitreserve\Model;

/**
* ReserveInterface.
*/
interface ReserveInterface
{
/**
* Return the public view of any transaction.
*
* @return Transaction The transaction identified by a given id.
*/
public function getTransactionById($id);

/**
* Return the public view of all transactions from the beginning of time.
*
* @return array The list all public transactions.
*/
public function getTransactions();

/**
* Get the reserve summary of all the obligations and assets within it.
*
* @return array The list of each holdings in all available currencies.
*/
public function getStatistics();
}
10 changes: 10 additions & 0 deletions test/Bitreserve/Tests/BitreserveClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,16 @@ public function shouldReturnOneTransaction()
$this->assertEquals($expectedTransactionId, $transaction->getId());
}

/**
* @test
*/
public function shouldReturnReserve()
{
$client = new BitreserveClient();

$this->assertInstanceOf('Bitreserve\Model\Reserve', $client->getReserve());
}

/**
* @test
*/
Expand Down
96 changes: 96 additions & 0 deletions test/Bitreserve/Tests/Model/ReserveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Bitreserve\Tests\Model;

use Bitreserve\Model\Reserve;

class ReserveTest extends TestCase
{
/**
* @test
*/
public function shouldReturnInstanceOfReserve()
{
$client = $this->getBitreserveClientMock();

$reserve = new Reserve($client);

$this->assertInstanceOf('Bitreserve\BitreserveClient', $reserve->getClient());
$this->assertInstanceOf('Bitreserve\Model\Reserve', $reserve);
}

/**
* @test
*/
public function shouldReturnStatistics()
{
$data = array('foo' => 'bar');

$client = $this->getBitreserveClientMock();
$client->expects($this->once())
->method('get')
->with('/reserve/statistics')
->will($this->returnValue($data));

$reserve = new Reserve($client);

$this->assertEquals($data, $reserve->getStatistics());
}

/**
* @test
*/
public function shouldReturnTransactions()
{
$data = array(array(
'id' => 'a97bb994-6e24-4a89-b653-e0a6d0bcf634',
'status' => 'completed',
), array(
'id' => '63dc7ccb-0e57-400d-8ea7-7d903753801c',
'status' => 'pending',
));

$client = $this->getBitreserveClientMock();
$client->expects($this->once())
->method('get')
->with('/reserve/transactions')
->will($this->returnValue($data));

$reserve = new Reserve($client);

$transactions = $reserve->getTransactions();

foreach ($transactions as $key => $transaction) {
$this->assertInstanceOf('Bitreserve\Model\Transaction', $transaction);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing data assert.

$this->assertEquals($data[$key]['id'], $transaction->getId());
$this->assertEquals($data[$key]['status'], $transaction->getStatus());
}
}

public function shouldReturnOneTransaction()
{
$data = array(
'id' => 'a97bb994-6e24-4a89-b653-e0a6d0bcf634',
'status' => 'completed',
);

$client = $this->getBitreserveClientMock();
$client->expects($this->once())
->method('get')
->with(sprintf('/reserve/transactions/%s', $data['id']))
->will($this->returnValue($data));

$reserve = new Reserve($client);

$transaction = $reserve->getTransactionById($data['id']);

$this->assertInstanceOf('Bitreserve\Model\Transaction', $transaction);
$this->assertEquals($data['id'], $transaction->getId());
$this->assertEquals($data['status'], $transaction->getStatus());
}

protected function getModelClass()
{
return 'Bitreserve\Model\Reserve';
}
}