Skip to content

Commit

Permalink
Merge pull request #11 from lukasyelle/master
Browse files Browse the repository at this point in the history
Quote Endpoint
  • Loading branch information
richdynamix authored Jun 27, 2020
2 parents e380241 + e8f3e8c commit 0fe072d
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 1 deletion.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

All notable changes to `iex-cloud-sdk` will be documented in this file

## v0.1.1 - 2020-06-24

### Added

##### Stocks

- Quote

### Changed

- Docs - Updated docs to include new endpoints

## v0.1.0 - 2019-10-15

### Added
Expand Down
2 changes: 1 addition & 1 deletion docs/dev-status.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Below is a list of endpoints to be implemented. (53/97)
- [x] Previous Day Prices
- [x] Price
- [x] Price Target
- [ ] Quote
- [x] Quote
- [ ] Recommendation Trends
- [ ] Sector Performance
- [ ] Splits
Expand Down
93 changes: 93 additions & 0 deletions docs/stocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,96 @@ Collection {#275 ▼
]
}
```

## Quote

**Example 1**

```php
use \Digitonic\IexCloudSdk\Stocks\Quote;

$endpoint = new Quote($client);
$response = $endpoint->setSymbol('aapl')->get();

// Laravel
use \Digitonic\IexCloudSdk\Facades\Stocks\Quote;

$response = Price::setSymbol('aapl')->get();
```

**Response 1**

```php
Collection {#277 ▼
#items: array:1 [▼
"symbol" => "AAPL",
"companyName" => "Apple Inc.",
"calculationPrice" => "tops",
"open" => 154,
"openTime" => 1506605400394,
"close" => 153.28,
"closeTime" => 1506605400394,
"high" => 154.80,
"low" => 153.25,
"latestPrice" => 158.73,
"latestSource" => "Previous close",
"latestTime" => "September 19, 2017",
"latestUpdate" => 1505779200000,
"latestVolume" => 20567140,
"volume" => 20567140,
"iexRealtimePrice" => 158.71,
"iexRealtimeSize" => 100,
"iexLastUpdated" => 1505851198059,
"delayedPrice" => 158.71,
"delayedPriceTime" => 1505854782437,
"oddLotDelayedPrice" => 158.70,
"oddLotDelayedPriceTime" => 1505854782436,
"extendedPrice" => 159.21,
"extendedChange" => -1.68,
"extendedChangePercent" => -0.0125,
"extendedPriceTime" => 1527082200361,
"previousClose" => 158.73,
"previousVolume" => 22268140,
"change" => -1.67,
"changePercent" => -0.01158,
"iexMarketPercent" => 0.00948,
"iexVolume" => 82451,
"avgTotalVolume" => 29623234,
"iexBidPrice" => 153.01,
"iexBidSize" => 100,
"iexAskPrice" => 158.66,
"iexAskSize" => 100,
"marketCap" => 751627174400,
"week52High" => 159.65,
"week52Low" => 93.63,
"ytdChange" => 0.3665,
"peRatio" => 17.18,
"lastTradeTime" => 1505779200000,
"isUSMarketOpen" => false
]
}
```

**Example 2**

```php
use \Digitonic\IexCloudSdk\Stocks\Quote;

$endpoint = new Quote($client);
$response = $endpoint->setSymbol('aapl')->only('latestPrice')->get();

// Laravel
use \Digitonic\IexCloudSdk\Facades\Stocks\Quote;

$response = Price::setSymbol('aapl')->only('latestPrice')->get();
```

**Response 2**

```php
Collection {#277 ▼
#items: array:1 [▼
"latestPrice" => 158.73
]
}
```
16 changes: 16 additions & 0 deletions src/Facades/Stocks/Quote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Digitonic\IexCloudSdk\Facades\Stocks;

use Illuminate\Support\Facades\Facade;

class Quote extends Facade
{
/**
* @return string
*/
protected static function getFacadeAccessor()
{
return \Digitonic\IexCloudSdk\Stocks\Quote::class;
}
}
66 changes: 66 additions & 0 deletions src/Stocks/Quote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Digitonic\IexCloudSdk\Stocks;

use Digitonic\IexCloudSdk\Contracts\IEXCloud;
use Digitonic\IexCloudSdk\Exceptions\WrongData;
use Digitonic\IexCloudSdk\Requests\BaseRequest;

class Quote extends BaseRequest
{
const ENDPOINT = 'stock/{symbol}/quote';

/**
* IEX Cloud Documentation provides for the optional field to be added to
* the end of the endpoint uri in order to retrieve a specific field.
* This property allows that functionality to be used in this SDK.
*/
public $field;

/**
* Create constructor.
*
* @param IEXCloud $api
*/
public function __construct(IEXCloud $api)
{
parent::__construct($api);
}

/**
* If the field property is set, add it to the end of the endpoint string.
*
* @return string
*/
protected function getFullEndpoint(): string
{
$endpoint = str_replace('{symbol}', $this->symbol, self::ENDPOINT);

return $this->field ? "$endpoint/$this->field" : $endpoint;
}

/**
* @return bool|void
* @throws WrongData
*/
protected function validateParams(): void
{
if (empty($this->symbol)) {
throw WrongData::invalidValuesProvided('Please provide a symbol to query!');
}
}

/**
* Setter for field property
*
* @param string $field
*
* @return Quote
*/
public function only(string $field): self
{
$this->field = $field;

return $this;
}
}
102 changes: 102 additions & 0 deletions tests/Stocks/QuoteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace Digitonic\IexCloudSdk\Tests\Stocks;

use Digitonic\IexCloudSdk\Exceptions\WrongData;
use Digitonic\IexCloudSdk\Facades\Stocks\Quote;
use Digitonic\IexCloudSdk\Tests\BaseTestCase;
use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Collection;

class QuoteTest extends BaseTestCase
{
/**
* Setup the test environment.
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();

$this->response = new Response(200, [], '{
"symbol": "AAPL",
"companyName": "Apple Inc.",
"calculationPrice": "tops",
"open": 154,
"openTime": 1506605400394,
"close": 153.28,
"closeTime": 1506605400394,
"high": 154.80,
"low": 153.25,
"latestPrice": 158.73,
"latestSource": "Previous close",
"latestTime": "September 19, 2017",
"latestUpdate": 1505779200000,
"latestVolume": 20567140,
"volume": 20567140,
"iexRealtimePrice": 158.71,
"iexRealtimeSize": 100,
"iexLastUpdated": 1505851198059,
"delayedPrice": 158.71,
"delayedPriceTime": 1505854782437,
"oddLotDelayedPrice": 158.70,
"oddLotDelayedPriceTime": 1505854782436,
"extendedPrice": 159.21,
"extendedChange": -1.68,
"extendedChangePercent": -0.0125,
"extendedPriceTime": 1527082200361,
"previousClose": 158.73,
"previousVolume": 22268140,
"change": -1.67,
"changePercent": -0.01158,
"iexMarketPercent": 0.00948,
"iexVolume": 82451,
"avgTotalVolume": 29623234,
"iexBidPrice": 153.01,
"iexBidSize": 100,
"iexAskPrice": 158.66,
"iexAskSize": 100,
"marketCap": 751627174400,
"week52High": 159.65,
"week52Low": 93.63,
"ytdChange": 0.3665,
"peRatio": 17.18,
"lastTradeTime": 1505779200000,
"isUSMarketOpen": false}');

$this->client = $this->setupMockedClient($this->response);
}

/** @test */
public function it_should_fail_without_a_symbol()
{
$quote = new \Digitonic\IexCloudSdk\Stocks\Quote($this->client);

$this->expectException(WrongData::class);

$quote->get();
}

/** @test */
public function it_can_query_the_quote_endpoint()
{
$quote = new \Digitonic\IexCloudSdk\Stocks\Quote($this->client);

$response = $quote->setSymbol('aapl')->get();
$this->assertInstanceOf(Collection::class, $response);
$this->assertCount(44, $response);
}

/** @test */
public function it_can_call_the_facade()
{
$this->setConfig();

Quote::shouldReceive('setSymbol')
->once()
->andReturnSelf();

Quote::setSymbol('aapl');
}
}

0 comments on commit 0fe072d

Please sign in to comment.