From b4a9dd649af294877c56b64951ab3c6d8b6575ef Mon Sep 17 00:00:00 2001 From: Nuno Rafael Rocha Date: Fri, 30 Jan 2015 16:15:14 +0000 Subject: [PATCH 1/6] Add reserve model * Add get reserve transactions method. * Add get reserve transaction by id method. * Add get reserve statistics method. --- lib/Bitreserve/Model/Reserve.php | 51 +++++++++++++++++++++++ lib/Bitreserve/Model/ReserveInterface.php | 30 +++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 lib/Bitreserve/Model/Reserve.php create mode 100644 lib/Bitreserve/Model/ReserveInterface.php diff --git a/lib/Bitreserve/Model/Reserve.php b/lib/Bitreserve/Model/Reserve.php new file mode 100644 index 0000000..17b742d --- /dev/null +++ b/lib/Bitreserve/Model/Reserve.php @@ -0,0 +1,51 @@ +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); + } +} diff --git a/lib/Bitreserve/Model/ReserveInterface.php b/lib/Bitreserve/Model/ReserveInterface.php new file mode 100644 index 0000000..f944faf --- /dev/null +++ b/lib/Bitreserve/Model/ReserveInterface.php @@ -0,0 +1,30 @@ + Date: Fri, 30 Jan 2015 16:29:12 +0000 Subject: [PATCH 2/6] Add reserve model tests --- test/Bitreserve/Tests/Model/ReserveTest.php | 96 +++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 test/Bitreserve/Tests/Model/ReserveTest.php diff --git a/test/Bitreserve/Tests/Model/ReserveTest.php b/test/Bitreserve/Tests/Model/ReserveTest.php new file mode 100644 index 0000000..d549a51 --- /dev/null +++ b/test/Bitreserve/Tests/Model/ReserveTest.php @@ -0,0 +1,96 @@ +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); + $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'; + } +} From 8bb6536bc1405d375abd36508fdecfd32dc66a2c Mon Sep 17 00:00:00 2001 From: Nuno Rafael Rocha Date: Fri, 30 Jan 2015 16:56:07 +0000 Subject: [PATCH 3/6] Add reserve to bitreserve client Add method `getReserve` that returns an instance of a Reserve model. --- lib/Bitreserve/BitreserveClient.php | 24 +++++++++++++++++++ .../Bitreserve/Tests/BitreserveClientTest.php | 10 ++++++++ 2 files changed, 34 insertions(+) diff --git a/lib/Bitreserve/BitreserveClient.php b/lib/Bitreserve/BitreserveClient.php index 2ca6189..bcdf90b 100644 --- a/lib/Bitreserve/BitreserveClient.php +++ b/lib/Bitreserve/BitreserveClient.php @@ -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; @@ -22,6 +23,13 @@ class BitreserveClient */ private $httpClient; + /** + * Current Reserve object. + * + * @var Reserve + */ + private $reserve; + /** * @var array */ @@ -202,6 +210,22 @@ public function getTransactions() }, $data); } + /** + * Get a reserve object or create a new one. + * + * @return Reserve The reserve object. + */ + public function getReserve() + { + if ($this->reserve) { + return $this->reserve; + } + + $this->reserve = new Reserve($this); + + return $this->reserve; + } + /** * Get current user. * diff --git a/test/Bitreserve/Tests/BitreserveClientTest.php b/test/Bitreserve/Tests/BitreserveClientTest.php index 39a49bf..541af42 100644 --- a/test/Bitreserve/Tests/BitreserveClientTest.php +++ b/test/Bitreserve/Tests/BitreserveClientTest.php @@ -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 */ From 0ac4fd00587d3a65348788b03de76f4aec061275 Mon Sep 17 00:00:00 2001 From: Nuno Rafael Rocha Date: Fri, 30 Jan 2015 20:26:48 +0000 Subject: [PATCH 4/6] Change bitreserve client transactions methods * Add `deprecated` annotation to `getTransaction` method. The current method for this feature is `getReserve()->getTransaction()`. * Add `deprecated` annotation to `getTransactionById` method. The current method for this feature is `getReserve()->getTrasactionId()`. --- lib/Bitreserve/BitreserveClient.php | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/Bitreserve/BitreserveClient.php b/lib/Bitreserve/BitreserveClient.php index bcdf90b..cdbcff1 100644 --- a/lib/Bitreserve/BitreserveClient.php +++ b/lib/Bitreserve/BitreserveClient.php @@ -184,30 +184,26 @@ 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 array_map(function($transaction) { - return new Transaction($this, $transaction); - }, $data); + return $this->getReserve()->getTransactions(); } /** From 063c6fc15b4bf9c7291cb90e8eb57ce7b7dc6233 Mon Sep 17 00:00:00 2001 From: Nuno Rafael Rocha Date: Sun, 1 Feb 2015 10:07:31 +0000 Subject: [PATCH 5/6] Add reserve example --- examples/Reserve.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 examples/Reserve.php diff --git a/examples/Reserve.php b/examples/Reserve.php new file mode 100644 index 0000000..f5db6e4 --- /dev/null +++ b/examples/Reserve.php @@ -0,0 +1,14 @@ +getReserve()->getStatistics(); + +print_r($statistics); From 607758ede479fe7cd6b9a303deac370096191cfe Mon Sep 17 00:00:00 2001 From: Nuno Rafael Rocha Date: Sun, 1 Feb 2015 10:08:16 +0000 Subject: [PATCH 6/6] Update readme with reserve examples --- README.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/README.md b/README.md index 0534547..571404e 100644 --- a/README.md +++ b/README.md @@ -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