Sinatra - how do I debug it when it's online? - ruby

I wrote a teeny tiny Sinatra app that runs fine, locally, but for some reason as soon as I put it online, all I get is 'Internal Server Error'.
How do I get the logging output?
I'm running on Dreamhost with passenger, using the instructions from the Sinatra book.
So I added to more handlers:
get '/hello/:name' do
"Hello, #{params[:name]}!"
end
get '/nokogiri-test/' do
doc = Nokogiri::HTML(open('http://www.google.co.il/search?q='+params[:query]))
res = ''
doc.xpath('//li//h3//a').each do |li|
res+= li.content + '<br />'
end
res
end
The first one works fine, the second throws an error.
I'm not interested in why there's an error. I'm interested in how to get feedback for it and resolve errors in the future.

Ah! The answer comes from here.
Never would have thought to look in there, but I got desperate. The solution is to set the environment to :development:
set :environment, :development
I stuck that into my configuration files and it produced all the error output.
Still doesn't solve my problem if I ever want to discover what's causing an error in a PRODUCTION app...
So how would I solve that?

If you're seeing apache's Internal Server Error message, you should be able to check the apache error log to find out what's going on. I think on dreamhost the log file is stored in /home/your_user_name/logs/yourdomain.com/http/error.log

Related

can't get ruby omniauth-ebay-oauth gem example code working

I am trying to setup and use omniauth-ebay-oauth (https://github.com/evilmartians/omniauth-ebay-oauth) gem to use eBay rest APIs in my app without success.
I set up the required environment variables and run the example code but get a message saying "Sinatra doesn’t know this ditty.". It does not recognise the '/auth/ebay' route, not sure if I have to declare that route myself nor what to put in it if I do. I'm new to ruby and Sinatra so do apologise if this is just something silly and obvious that I'm missing.
require 'omniauth-ebay-oauth'
use Rack::Session::Cookie
use OmniAuth::Builder do
provider :ebay_oauth, ENV['EBAY_CLIENT_ID'], ENV['EBAY_CLIENT_SECRET'],
callback_url: ENV['EBAY_RU_NAME'], name: 'ebay'
end
get '/' do
redirect '/auth/ebay'
end
get '/auth/ebay/callback' do
"Hello, #{request.env['omniauth.auth'].dig('info', 'name')}"
end
I appreciate any help and insight in getting this working. I've googled everywhere and asking here as my last resort.
I opened an issue on the GitHub repository and the gem creator replied in 3 hours. Totally life saver. I will post the solution here to help others.
It is because of security settings of OmniAuth 2.x.
Add the following line at the top, after requires:
OmniAuth.config.allowed_request_methods += %i[get]
This worked like a charm and I can now move forwards with the project.

I am getting an RPC error message when running my cucumber tests against a password protected https url

So what am I using:
I am using the following gems on Ruby 1.9.3:
capybara, commander, cucumber, cucumber-rails, fakeweb, factory_girl_rails, flexmock, gherkin, parallel, parallel_tests, poltergeist, rspec, rspec-rails, sauce, sauce-connect, sauce-cucumber, selenium-webdriver'
For my config file I am using yaml. so config.yml
Now to access the homepage of the site I am testing in my config I have
base_url: https://<username>:<password>#the.url.com
When running my cucumber tests (using poltergeist) the following message is shown several times while the tests are running:
Invalid rpc message origin. https://username#the.url.com vs https://the.url.com
It does not cause the tests to fail but is incredibly untidy and I would really like to get rid of it.
I have been, and still am, investigating a solution to this but if someone gets there first that would be amazing.
Some things that I have tried/know what is happening:-
I know my tests are working as I have run them using the browser (firefox) and it is fine with none of these messages.
Also if I remove the s from https the message dissapears. But alas that will not work for the site as require https.
Putting the url in double quotes does not solve the problem.
I have pinpointed the issue directly to the config and specifying the UN and PW in the url.
It looks like your username and password have gone AWOL:
Invalid rpc message origin. https://#the.url.com vs https://the.url.com
The first part should be https://username:password#the.url.com. Could it be because your config is lacking the double slashes?
base_url: https:<username>:<password>#the.url.com
should probably be
base_url: https://<username>:<password>#the.url.com

Twitter handler (sensu and ruby) troubleshooting

I'm trying to get Sensu twitter-handler working on my environment. The issue is that I'm not getting any errors on screen or logs when I cat a .json event into the twitter-handler and, the tweets are not being shown on the linked account.
Here're are my config files:
https://gist.github.com/Mariano-gon/8648427
https://gist.github.com/Mariano-gon/8648455
https://gist.github.com/Mariano-gon/8648489
This is the output I get:
https://gist.github.com/Mariano-gon/8648480
One important note is that in sensu-api.log the request are being recieved:
https://gist.github.com/Mariano-gon/8673758
So, my question is: is there a way to troubleshoot this issue? Any way to debug the handler.rb?
Thanks!
Finally got it working! Thing was the new 'twitter' gem (in my case v5.6) won't work with the code as it was written. I needed to follow this great answer and this thread too. Was just a matter of sintaxis (as usual).
Thanks!

Facebook Sinatra app won't login

I am trying to develop a Facebook Application using the Ruby Heroku pack provided. I have experimented with this before but just created a new app to develop. I am having an issue where I am unable to log in with the app. I click on a button to log in, but after the redirects, I find that nothing has happened. Once in every 20 or so attempts though, it will log me in correctly.
For those who've never used the Heroku hosting for Facebook, here is the template that gets installed. The only changes I have made were to modularize the app.
The relevant routing is as follows:
get "/auth/facebook" do
session[:access_token] = nil
redirect authenticator.url_for_oauth_code(:permissions => FACEBOOK_SCOPE)
end
get '/auth/facebook/callback' do
session[:access_token] = authenticator.get_access_token(params[:code])
redirect '/'
end
Has anyone ever experienced this before? If one would like to try themselves, this is the location of the app. I have a similar one running that doesn't exhibit this problem, so I really can't figure it out.
EDIT: I think I found the problem. I had set Unicorn preload_app to false, following this piece of information from New Relic. Setting preload_app to true has since fixed the problem. Unfortunately, I cannot remember why I set it to false in the first place (I have adapted some configuration stuff from a previous app). Maybe someone has better knowledge than myself.

Recieving a 404 HTTPError on a working page in Ruby Script

This is my first time asking a question, please be gentle!
I have a Rails application that handles content for a whole bunch of domains (over 100 so far). Each domain either points to where my app is hosted (Heroku, if you're interested), or the original place it was hosted. Every time a domain is ready, it needs to point to the heroku servers, so that my app can serve content for it.
To check to see if a domain has successfully been changed over from its original location to my application, I'm writing a script that looks for a special hidden tag I included in them. If it finds the tag, then the domain is pointing to my app. If not, it hasn't been changed, which I record.
The problem is that, at least for one domain so far, I'm getting a 404 OpenURI::HTTPError exception for my script. Which is strange, because I can visit the site just fine and I can even get it via curl. Does anyone know why a working site would get an error like this? Here's the important snippet:
require 'rubygems'
require 'open-uri'
require 'hpricot'
...
url = "http://www.#{domainname}.com"
doc = Hpricot(open(url)) #<---- Problem right here.
...
Thanks for all of your help!
Welcome to SO!
Here would be my debugging method:
See if you can replicate in irb with open-uri alone, no Hpricot:
$ irb -rubygems -ropen-uri
>> open('http://www.somedomain.com')
Look in your Heroku log to see if it even touches the server.
Look in your original server's log for the same.
Throw open something like Wireshark to see the HTTP transaction, and see if a 404 is indeed coming back.
Start with that, and come back with your results.

Resources