event machine with em http gem cannot load middleware oauth - ruby

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 the em-http-request gem. I looked into the file and the first line says require simple_oauth which I haven't installed it yet. I would think installing em-http-request gem would install simple_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).

Related

How can I make AWS lambda find the grpc dependency in my function?

GRPC is giving me fits here:
{
"errorMessage": "Could not find 'grpc' (~> 1.24) among 281 total gem(s)\nChecked in 'GEM_PATH=/var/task/vendor/bundle/ruby/2.5.0:/opt/ruby/gems/2.5.0:/var/runtime', execute `gem env` for more information",
"errorType": "Init<Gem::MissingSpecError>",
"stackTrace": [
"/var/lang/lib/ruby/site_ruby/2.5.0/rubygems/dependency.rb:311:in `to_specs'",
"/var/lang/lib/ruby/site_ruby/2.5.0/rubygems/specification.rb:1402:in `block in activate_dependencies'",
"/var/lang/lib/ruby/site_ruby/2.5.0/rubygems/specification.rb:1391:in `each'",
"/var/lang/lib/ruby/site_ruby/2.5.0/rubygems/specification.rb:1391:in `activate_dependencies'",
"/var/lang/lib/ruby/site_ruby/2.5.0/rubygems/specification.rb:1373:in `activate'",
"/var/lang/lib/ruby/site_ruby/2.5.0/rubygems.rb:215:in `rescue in try_activate'",
"/var/lang/lib/ruby/site_ruby/2.5.0/rubygems.rb:208:in `try_activate'",
"/var/lang/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:161:in `rescue in require'",
"/var/lang/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:35:in `require'",
"/var/task/vendor/bundle/ruby/2.5.0/gems/google-cloud-dialogflow-0.14.0/lib/google/cloud/dialogflow.rb:16:in `<top (required)>'",
"/var/task/google_dialog.rb:3:in `require_relative'",
"/var/task/google_dialog.rb:3:in `<top (required)>'",
"/var/lang/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:168:in `require'",
"/var/lang/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:168:in `rescue in require'",
"/var/lang/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:35:in `require'"
]
}
I see it at vendor/bundle/ruby/2.5.0/gems/grpc-1.27.0-universal-darwin/src/ruby/lib/grpc.rb
I'm writing a simple Ruby class to hook into Dialogflow, and the google gem is fairly heavy with a lot of dependencies. I've written some tests that all pass locally and in the context of the Rails application from where I'm extracting this bit of code. I'll need Nokogiri, but it appears that Google is the big user of the GRPC stuff. And it just won't find that file when when I specify is manually. Any help is greatly appreciated.
In the case of the above issue - order of operations are everything I learned. Ruby is parsing top-down instead of loading everything then parsing, which I suppose I should have known, but Rails does a lot of magical things for you.
So when I was seeing the issue above, I had the following require statements at the top of my file:
require_relative 'vendor/bundle/ruby/2.5.0/gems/nokogiri-1.10.9/lib/nokogiri.rb'
require_relative 'vendor/bundle/ruby/2.5.0/gems/google-cloud-dialogflow-0.14.0/lib/google/cloud/dialogflow.rb'
require_relative 'vendor/bundle/ruby/2.5.0/gems/google-cloud-dialogflow-0.14.0/lib/google/cloud/dialogflow/v2.rb'
require_relative 'vendor/bundle/ruby/2.5.0/gems/grpc-1.24.0-universal-darwin/src/ruby/lib/grpc.rb'
And due to the require for grpc coming after the google cloud ruby
require_relative 'vendor/bundle/ruby/2.5.0/gems/grpc-1.24.0-universal-darwin/src/ruby/lib/grpc.rb'
require_relative 'vendor/bundle/ruby/2.5.0/gems/nokogiri-1.10.9/lib/nokogiri.rb'
require_relative 'vendor/bundle/ruby/2.5.0/gems/google-cloud-dialogflow-0.14.0/lib/google/cloud/dialogflow.rb'
require_relative 'vendor/bundle/ruby/2.5.0/gems/google-cloud-dialogflow-0.14.0/lib/google/cloud/dialogflow/v2.rb'
Simply putting grpc before dialogflow resolved that issue.
Lesson - always go back to basics.

Using tweetstream and timeout

Ruby version: 2.0.0-p0
Mac - Mountain Lion
Following is my code (tw_stream_track.rb):
require 'tweetstream'
TweetStream.configure do |config|
config.consumer_key = 'xxxxxxxxxxxxxxx'
config.consumer_secret = 'xxxxxxxxxxxxxxx'
config.oauth_token = 'xxxxxxxxxxxxxxx-xxxxxxxxxxxxxxx'
config.oauth_token_secret = 'xxxxxxxxxxxxxxx'
config.auth_method = :oauth
end
client = TweetStream::Client.new
client.on_error do |message|
puts message
end
client.track('apple', 'microsoft', 'samsung') do |status|
puts "#{status.text}"
end
When I run it from a terminal:
ruby tw_stream_track.rb
I get the following:
/Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/tweetstream-2.5.0/lib/tweetstream/client.rb:436:in `block in connect': Failed to reconnect after 11 tries. (TweetStream::ReconnectError)
from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/em-twitter-0.2.2/lib/em-twitter/connection.rb:296:in `call'
from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/em-twitter-0.2.2/lib/em-twitter/connection.rb:296:in `invoke_callback'
from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/em-twitter-0.2.2/lib/em-twitter/connection.rb:268:in `rescue in schedule_reconnect'
from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/em-twitter-0.2.2/lib/em-twitter/connection.rb:263:in `schedule_reconnect'
from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/em-twitter-0.2.2/lib/em-twitter/connection.rb:93:in `unbind'
from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/eventmachine-1.0.3/lib/eventmachine.rb:1440:in `event_callback'
from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run_machine'
from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run'
from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/tweetstream-2.5.0/lib/tweetstream/client.rb:388:in `start'
from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/tweetstream-2.5.0/lib/tweetstream/client.rb:131:in `filter'
from /Users/mine/.rvm/gems/ruby-2.0.0-p0/gems/tweetstream-2.5.0/lib/tweetstream/client.rb:98:in `track'
This is my first time using tweetstream, and from the documentation, it seems as if this is what I'm supposed to be doing. What am I missing?
I heard TweetStream had some problems with ruby 2.0.0, did you try with ruby 1.9.3 ?
UPDATE
I just had a try and your code is working fine with ruby 1.9.3 (installed with rvm).
The only pb that I have is that the stream seems to stop retrieving new messages after some time.
In my case this error appeared because I was using same keys in 2 rails app instances(on staging and production servers) + 1 local(development) instance.
The 3rd instance was unable to connect to tweets stream. It is cause because of the twitter limitations
So I resolved this by creating dedicated twitter applications(with separate keys) for each Rails application instance.

Weird LoadError on custom ruby gem

I have a custom gem and am encountering a really weird LoadError when I install it as a gem and attempt to require it in irb.
Everything works fine with my rspec tests inside the project folder. This only occurs when using it as an actual gem in irb.
The file it throws a LoadError exception at (/lib/mws/api/order_response.rb) does in fact exist. I've tried renaming the file and updating the file that requires it (/lib/mws.rb). I've tried recreating the file thinking maybe there was a permissions issue. Nothing works.
If I comment out the require line for that specific file, everything works. There's nothing special about the file. There's 4 other files nearly identical to it (*_response.rb).
I feel like I'm taking crazy pills. I must being overlooking something but I sure don't see it.
Trace:
chris#Samus:~$ irb
1.9.3p194 :001 > require 'mws'
LoadError: cannot load such file -- mws/api/order_response
from /Users/chris/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/chris/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/chris/.rvm/gems/ruby-1.9.3-p194/gems/mws-0.1.18/lib/mws.rb:14:in `<top (required)>'
from /Users/chris/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `require'
from /Users/chris/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:60:in `rescue in require'
from /Users/chris/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from (irb):1
from /Users/chris/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `<main>'
File with the requires (/lib/mws.rb)
require 'mws/base'
require 'mws/connection'
require 'mws/utility'
require 'mws/api/seller'
require 'mws/api/product'
require 'mws/api/order'
require 'mws/api/report'
require 'mws/api/general_response'
require 'mws/api/product_response'
require 'mws/api/report_response'
require 'mws/api/seller_response'
require 'mws/api/order_response' # <--- the offending line
module MWS
# #see Base#initialize MWS::Base for instantiation details.
# #return [Base] returns MWS::Base object.
def self.new(merchant_id, access_key, secret_key)
MWS::Base.new(merchant_id, access_key, secret_key)
end
end
# The below is for documentation generation purposes.
# MWS is a wrapper for the Amazon Marketplace Web Service (MWS) API.
module MWS
# API handles all the Amazon MWS API specific stuff.
module API
end
# Utilities contains various functions needed throughout MWS. Utilities is a mixin to multiple classes.
module Utilities
end
end
File I'm requiring (/lib/mws/api/order_response.rb):
module MWS
module API
# Class for parsing Amazon's XML responses into managable objects.
class OrderResponse
# Include GeneralResponse instance methods as class methods
extend GeneralResponse
end
end
end
And my file structure
For anyone interested, I was using Jeweler to handle building this gem. As it turns out, Jeweler uses your Git repository when building a gemspec.
If you haven't added all required files to your git repository, Jeweler's gemspec rake task will not include them when generating a new gemspec file.
Can should check in /Users/chris/.rvm/gems/ruby-1.9.3-p194/gems/mws-0.1.18/lib/mws/api if the file lies there (and doesn't have obscure permissions).
If that's not the case, you probably forgot to add it in your gemspec.
If it is there, please try requiring/loading it with the absolute path (for debugging purpose).

padrino && websockets

i'm looking for a way to open and use websockets from within a Padrino application. i know Padrino works with a single thread but i'm looking for a way to open websockets and share variables between its "onopen" "onclose" "onmessage" methods and Padrino controllers.
any idea how it's done ?
links i looked into:
Examples of Eventmachine usage with Padrino and Sinatra (only Sinatra worked for me)
em-websocket on GitHub
UPDATE 1:
this is my main.rb:
require 'rubygems' # <-- Added this require
require 'em-websocket'
require 'padrino-core'
require 'thin'
require File.expand_path("../config/boot.rb", __FILE__)
SOCKETS = []
EventMachine.run do # <-- Changed EM to EventMachine
# class App < Sinatra::Base
# get '/' do
# SOCKETS.each {|s| s.send "fooooo"}
# return "foo"
# end
# end
EventMachine::WebSocket.start(:host => '0.0.0.0', :port => 8080) do |ws| # <-- Added |ws|
# Websocket code here
ws.onopen {
ws.send "connected!!!!"
SOCKETS << ws
}
ws.onmessage { |msg|
puts "got message #{msg}"
ws.send "ECHO: #{msg}"
}
ws.onclose {
ws.send "WebSocket closed"
SOCKETS.delete ws
}
end
# You could also use Rainbows! instead of Thin.
# Any EM based Rack handler should do.
#App.run!({:port => 3000}) # <-- Changed this line from Thin.start to App.run!
Thin::Server.start Padrino.application, '0.0.0.0', 3000
end
i'm getting this exception:
/home/cstore/.rvm/gems/ruby-1.9.2-p290#runtime/gems/thin-1.2.11/lib/thin/daemonizing.rb:2:in `require': no such file to load -- daemons (LoadError)
from /home/cstore/.rvm/gems/ruby-1.9.2-p290#runtime/gems/thin-1.2.11/lib/thin/daemonizing.rb:2:in `<top (required)>'
from /home/cstore/.rvm/gems/ruby-1.9.2-p290#runtime/gems/thin-1.2.11/lib/thin/server.rb:50:in `<class:Server>'
from /home/cstore/.rvm/gems/ruby-1.9.2-p290#runtime/gems/thin-1.2.11/lib/thin/server.rb:48:in `<module:Thin>'
from /home/cstore/.rvm/gems/ruby-1.9.2-p290#runtime/gems/thin-1.2.11/lib/thin/server.rb:1:in `<top (required)>'
from main.rb:39:in `block in <main>'
from /home/cstore/.rvm/gems/ruby-1.9.2-p290#runtime/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `call'
from /home/cstore/.rvm/gems/ruby-1.9.2-p290#runtime/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run_machine'
from /home/cstore/.rvm/gems/ruby-1.9.2-p290#runtime/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in `run'
from main.rb:9:in `<main>'
UPDATE 2:
Resolved thanks to Nathan !
I just added 'daemons' to Gemfile and reloaded my application.
Maybe you need to install daemons:
Edit your Gemfile:
# Adding this
gem 'daemons'
Install missing gems:
$ bundle install
What in particular from this example: https://github.com/igrigorik/em-websocket and Any success with Sinatra working together with EventMachine WebSockets? didn't work with Padrino but did with Sinatra? Can you explain the errors you got and why those examples failed (stacktraces)? Maybe we can help investigate.
I ran across this post and it helped me a bit, but I wanted to offer an alternative solution to anyone else who might stumble upon it. I chose to just directly modify the config.ru and mount a websocket-rack application.
Here's my config.ru where WSApp is a subclass of Rack::WebSocket::Application and is placed in the lib/ directory (therefore being automatically loaded by Padrino):
#!/usr/bin/env rackup
# encoding: utf-8
# This file can be used to start Padrino,
# just execute it from the command line.
require File.expand_path("../config/boot.rb", __FILE__)
# Setup routes
map '/' do
run Padrino.application
end
map '/ws' do
run WSApp.new
end
Since this is the top hit in Google right now, I'd like to link it to padrino-websockets, a clean DSL for writing websockets applications in Padrino.

ruby-openid: #socket not set while performing discovery

I'm having a bit of truble with omniauth/openid.
When trying to authenticate, I found this in my logs:
OpenID::FetchingError: Error fetching https://www.google.com/accounts/o8/.well-known/host-meta?hd=profiles.google.com%2Fmy_username: undefined method `io' for nil:NilClass
The important thing there is undefined method io' for nil:NilClass which comes from openid/fetchers.rb, in the following snippet:
module Net
class HTTP
def post_connection_check(hostname)
check_common_name = true
cert = #socket.io.peer_cert
cert.extensions.each { |ext|
next if ext.oid != "subjectAltName"
ext.value.split(/,\s+/).each{ |general_name|
if /\ADNS:(.*)/ =~ general_name
check_common_name = false
...
That error is generated by #socket.io.peer_cert, #socket is not defined.
Have any of you encountered this before? Not quite sure what the cause is.
Versions I'm running:
ruby 1.9.3dev (2010-08-17 trunk 29020) [x86_64-darwin10.4.0]
ruby-openid (2.1.8)
ruby-openid-apps-discovery (1.2.0)
omniauth 0.2.0
We had this same problem and it was a direct result of Net::HTTP#connect never being invoked. Turns out we had the fakeweb gem scoped into the environment that was throwing the error (development, in our case).
Narrowing fakeweb's scope allows for normal processing of #connect and #socket is once again happy.
group :test do
gem 'fakeweb'
end
We came across the same / very similar problem with both fakeweb and webmock (when using the VCR gem). Switching from fakeweb to typhoeus seemed to have solved this problem for us.

Resources