Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### 1.5.1 (Next)
* Your contribution here.
* [#108](https://github.com/dblock/iex-ruby-client/pull/108): Added support for quote streaming endpoint - [@bguban](https://github.com/bguban).

### 1.5.0 (2021/08/15)
* [#105](https://github.com/dblock/iex-ruby-client/pull/105): Added support for fetching latest foreign exchange rates - [@mathu97](https://github.com/mathu97).
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ A Ruby client for the [The IEX Cloud API](https://iexcloud.io/docs/api/).
- [Configure](#configure)
- [Get a Single Price](#get-a-single-price)
- [Get a Quote](#get-a-quote)
- [Stream quotes](#stream-quotes)
- [Get a OHLC (Open, High, Low, Close) price](#get-a-ohlc-open-high-low-close-price)
- [Get a Market OHLC (Open, High, Low, Close) prices](#get-a-market-ohlc-open-high-low-close-prices)
- [Get Historical Prices](#get-historical-prices)
Expand Down Expand Up @@ -107,6 +108,21 @@ quote.change_percent_s # '+0.42%'

See [#quote](https://iexcloud.io/docs/api/#quote) for detailed documentation or [quote.rb](lib/iex/resources/quote.rb) for returned fields.

### Stream quotes

Streams quotes in real-time.

`interval` option lets you limit amount of updates. Possible values: `1Second 5Second 1Minute`
Copy link
Owner

Choose a reason for hiding this comment

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

The interval ... so it's a complete sentence.

Possible values are ... and quote each value. End with a period.


```ruby
client.stream_quote(['SPY', 'MSFT'], interval: '5Second') do |quote|
quote.latest_price # 90.165
quote.symbol # 'MSFT'
end
```

See [#streaming-data](https://iexcloud.io/docs/api/#streaming-data) for detailed documentation or [quote.rb](lib/iex/resources/quote.rb) for returned fields.

### Get a OHLC (Open, High, Low, Close) price

Fetches a single stock OHLC price. Open and Close prices contain timestamp.
Expand Down
2 changes: 1 addition & 1 deletion iex-ruby-client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |s|
s.add_dependency 'faraday', '>= 0.17'
s.add_dependency 'faraday_middleware'
s.add_dependency 'hashie'
s.add_dependency 'money_helper'
s.add_dependency 'money_helper', '~>1'
s.add_development_dependency 'rake', '~> 10'
s.add_development_dependency 'rspec'
s.add_development_dependency 'rubocop', '0.72.0'
Expand Down
24 changes: 22 additions & 2 deletions lib/iex/cloud/request.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
module IEX
module Cloud
module Request
STREAM_EVENT_DELIMITER = "\r\n\r\n".freeze

def get(path, options = {})
request(:get, path, options)
end

def get_stream(path, options = {})
buffer = ''
Copy link
Owner

Choose a reason for hiding this comment

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

use StringIO

event_parser = proc do |chunk|
events = (buffer + chunk).lines(STREAM_EVENT_DELIMITER)
buffer = events.last.end_with?(STREAM_EVENT_DELIMITER) ? '' : events.delete_at(-1)
events.each do |event|
yield JSON.parse(event.gsub(/\Adata: /, ''))
end
end

options = {
endpoint: endpoint.gsub('://cloud.', '://cloud-sse.'),
Copy link
Owner

Choose a reason for hiding this comment

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

remove this

request: { on_data: event_parser }
}.merge(options)
request(:get, path, options)
end

def post(path, options = {})
request(:post, path, options)
end
Expand All @@ -20,16 +39,17 @@ def delete(path, options = {})
private

def request(method, path, options)
path = [endpoint, path].join('/')
options = options.dup
path = [options.delete(:endpoint) || endpoint, path].join('/')
response = connection.send(method) do |request|
request.options.merge!(options.delete(:request)) if options.key?(:request)
case method
when :get, :delete
request.url(path, options)
when :post, :put
request.path = path
request.body = options.to_json unless options.empty?
end
request.options.merge!(options.delete(:request)) if options.key?(:request)
end
response.body
end
Expand Down
13 changes: 13 additions & 0 deletions lib/iex/endpoints/quote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ def quote(symbol, options = {})
rescue Faraday::ResourceNotFound => e
raise IEX::Errors::SymbolNotFoundError.new(symbol, e.response[:body])
end

# @param symbols - a list of symbols
# @param options[:interval] sets intervals such as 1Second, 5Second, or 1Minute
def stream_quote(symbols, options = {})
options[:symbols] = Array(symbols).join(',')
interval = options.delete(:interval)

get_stream("stocksUS#{interval}", { token: secret_token }.merge(options)) do |payload|
payload.each do |quote|
yield IEX::Resources::Quote.new(quote)
end
end
end
end
end
end
61 changes: 61 additions & 0 deletions spec/fixtures/iex/stream_quote/spy.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 49 additions & 27 deletions spec/iex/endpoints/quote_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,61 @@

describe IEX::Resources::Quote do
include_context 'client'
context 'known symbol', vcr: { cassette_name: 'quote/msft' } do
subject do
client.quote('MSFT')
end
it 'retrieves a quote' do
expect(subject.symbol).to eq 'MSFT'
expect(subject.company_name).to eq 'Microsoft Corp.'
expect(subject.market_cap).to eq 915_754_985_600
end
it 'coerces numbers' do
expect(subject.latest_price).to eq 119.36
expect(subject.change).to eq(-0.61)
expect(subject.week_52_high).to eq 120.82
expect(subject.week_52_low).to eq 87.73
expect(subject.change_percent).to eq(-0.00508)
expect(subject.change_percent_s).to eq '-0.51%'
expect(subject.extended_change_percent).to eq(-0.00008)
expect(subject.extended_change_percent_s).to eq '-0.01%'

describe '#quote' do
context 'known symbol', vcr: { cassette_name: 'quote/msft' } do
subject do
client.quote('MSFT')
end
it 'retrieves a quote' do
expect(subject.symbol).to eq 'MSFT'
expect(subject.company_name).to eq 'Microsoft Corp.'
expect(subject.market_cap).to eq 915_754_985_600
end
it 'coerces numbers' do
expect(subject.latest_price).to eq 119.36
expect(subject.change).to eq(-0.61)
expect(subject.week_52_high).to eq 120.82
expect(subject.week_52_low).to eq 87.73
expect(subject.change_percent).to eq(-0.00508)
expect(subject.change_percent_s).to eq '-0.51%'
expect(subject.extended_change_percent).to eq(-0.00008)
expect(subject.extended_change_percent_s).to eq '-0.01%'
end
it 'coerces times' do
expect(subject.latest_update).to eq 1_554_408_000_193
expect(subject.latest_update_t).to eq Time.at(1_554_408_000)
expect(subject.iex_last_updated).to eq 1_554_407_999_529
expect(subject.iex_last_updated_t).to eq Time.at(1_554_407_999)
end
end
it 'coerces times' do
expect(subject.latest_update).to eq 1_554_408_000_193
expect(subject.latest_update_t).to eq Time.at(1_554_408_000)
expect(subject.iex_last_updated).to eq 1_554_407_999_529
expect(subject.iex_last_updated_t).to eq Time.at(1_554_407_999)

context 'invalid symbol', vcr: { cassette_name: 'quote/invalid' } do
subject do
client.quote('INVALID')
end
it 'fails with SymbolNotFoundError' do
expect { subject }.to raise_error IEX::Errors::SymbolNotFoundError, 'Symbol INVALID Not Found'
end
end
end

context 'invalid symbol', vcr: { cassette_name: 'quote/invalid' } do
describe '#stream_quote' do
subject do
client.quote('INVALID')
quotes = []
client.stream_quote('SPY', interval: '5Second') do |quote|
quotes << quote
end

quotes
end
it 'fails with SymbolNotFoundError' do
expect { subject }.to raise_error IEX::Errors::SymbolNotFoundError, 'Symbol INVALID Not Found'

let(:quote) { subject.last }

it 'retrieves a quote', vcr: { cassette_name: 'stream_quote/spy' } do
expect(subject.size).to eq(2)
expect(quote.symbol).to eq('SPY')
expect(quote.close).to eq(433.63)
end
end
end