I am trying to get the stock price streaming to work using TradeKing API
https://developers.tradeking.com/documentation/ruby-streaming
or the copied and pasted codes below
require 'em-http'
require 'em-http/middleware/oauth'
credentials = {
:consumer_key => "<CONSUMER_KEY>",
:consumer_secret => "<CONSUMER_SECRET>",
:access_token => "<ACCESS_TOKEN>",
:access_token_secret => "<ACCESS_TOKEN_SECRET>"
}
EM.run do
conn = EventMachine::HttpRequest.new('https://stream.tradeking.com/v1/market/quotes.json?symbols=F')
conn.use EventMachine::Middleware::OAuth, credentials
http = conn.get
http.stream { |chunk| puts chunk }
http.errback do
EM.stop
end
trap("INT") { http.close; EM.stop }
trap("TERM") { http.close; EM.stop }
end
After getting key, secret, and token, I built a simple Ruby app to play with the codes but I get an error that says it cannot load
require 'em-http/middleware/oauth'
If I disable this, the code EventMachine::Middleware::OAuth
will not work.
Here is the error message:
c:/tools/ruby215/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/
kernel_require.rb:54:in `require': cannot load such file --
simple_oauth (LoadError)
from c:/tools/ruby215/lib/ruby/site_ruby/2.1.0/rubyg
ems/core_ext/kernel_require.rb:54:in `require'
from c:/tools/ruby215/lib/ruby/gems/2.1.0/gems/em-ht
tp-request-1.1.2/lib/em-http/middleware/oauth.rb:1:in `<top
(required)>'
from c:/tools/ruby215/lib/ruby/site_ruby/2.1.0/rubyg
ems/core_ext/kernel_require.rb:54:in `require'
from c:/tools/ruby215/lib/ruby/site_ruby/2.1.0/rubyg
ems/core_ext/kernel_require.rb:54:in `require'
from app.rb:2:in `<main>'
I am new to event machine and em-http gem. I looked at their documentation but couldn't find information about this error. Can someone help me figure out why the file cannot load?
OK, so I solved this myself. I haven't worked with vanilla Ruby in a while so I got a bit rusty with reading the error messages. The error says that it cannot load the file and points to line 1 of the the
oauth.rb
file which is in theem-http-request
gem. I looked into the file and the first line saysrequire simple_oauth
which I haven't installed it yet. I would think installingem-http-request
gem would installsimple_oauth
as a dependency, but I guess not (well the em-http-request hasn't been updated for several years).The fix to this problem is to install
simple_oauth
gem.gem install simple_oauth
and run those codes again and it should work.
https://rubygems.org/gems/simple_oauth/versions/0.3.1
https://github.com/laserlemon/simple_oauth
I hope this helps anyone who is having the same problem (since TradeKing API doc isn't as clear).