sinatra interview questions
Top sinatra frequently asked interview questions
In my main Sinatra controller, I want to debug the params hash after it is POSTed from a form.
I have added:
puts params.inspect
and
set :logging, :true
The params.inspect
works if everything goes well. But if an error happens before the controller is executed I'm not getting any information about the error like I would in Rails by default.
What's the best way to get useful debug info?
This example did not work at all (the app wouldn't even start after I added this code):
configure do
Log = Logger.new("sinatra.log")
Log.level = Logger::INFO
end
followed by:
Log.info "#{@users.inspect}"
Source: (StackOverflow)
I have a small web-server that I wrote with Sinatra. I want to be able to log messages to a log file. I've read through http://www.sinatrarb.com/api/index.html and www.sinatrarb.com/intro.html, and I see that Rack has something called Rack::CommonLogger, but I can't find any examples of how it can be accessed and used to log messages. My app is simple so I wrote it as a top-level DSL, but I can switch to subclassing it from SinatraBase if that's part of what's required.
Source: (StackOverflow)
I am using
# my_app.rb
load 'index.rb'
and start the sever like this
ruby my_app.rb
but it never reload any changes I made in index page.
Did I miss anything here?
Source: (StackOverflow)
I'm wondering how one can use Bundler with Sinatra. The idea is to use the gems that Bundler downloads inside the .gems folder.
Source: (StackOverflow)
I have one page website only using HTML, CSS and JavaScript. I want to deploy the app to Heroku, but I cannot find a way to do it. I am now trying to make the app working with Sinatra.
.
|-- application.css
|-- application.js
|-- index.html
|-- jquery.js
`-- myapp.rb
And the following is the content of myapp.rb
.
require 'rubygems'
require 'sinatra'
get "/" do
# What should I write here to point to the `index.html`
end
Source: (StackOverflow)
I like the sinatra framework, but might have to work in python. A quick web search has uncovered a few python equivalents including itty, flask and juno.
I'd like to know people's experience of these, or other sinatra equivalents. Which would you recommend?
Source: (StackOverflow)
I'm trying to pass parameters to an erb view using Ruby and Sinatra.
For example, I can do:
get '/hello/:name' do
"Hello #{params[:name]}!"
end
How do I pass :name
to the view?
get '/hello/:name' do
erb :hello
end
And how do I read the parameters inside view/hello.erb?
Thanks!
Source: (StackOverflow)
I am writing my first Sinatra application and would like to use Pry to inspect/debug some things going on in the application. I haven't used Pry before either, but I would like to try it out. How would I get started using Pry with my Sinatra application?
Source: (StackOverflow)
I'm new to all three, and I'm trying to write a simple contact form for a website. The code I have come up with is below, but I know there are some fundamental problems with it (due to my inexperience with sinatra). Any help at getting this working would be appreciated, I can't seem to figure out/find the documentation for this sort of thing.
haml code from the contact page:
%form{:name => "email", :id => "email", :action => "/contact", :method => "post", :enctype => "text/plain"}
%fieldset
%ol
%li
%label{:for => "message[name]"} Name:
%input{:type => "text", :name => "message[name]", :class => "text"}
%li
%label{:for => "message[mail]"} Mail:
%input{:type => "text", :name => "message[mail]", :class => "text"}
%li
%label{:for => "message[body]"} Message:
%textarea{:name => "message[body]"}
%input{:type => "submit", :value => "Send", :class => "button"}
And here is my code in sinatra's app.rb:
require 'rubygems'
require 'sinatra'
require 'haml'
require 'pony'
get '/' do
haml :index
end
get '/contact' do
haml :contact
end
post '/contact' do
name = #{params[:name]}
mail = #{params[:mail]}
body = #{params[:body]}
Pony.mail(:to => '*emailaddress*', :from => mail, :subject => 'art inquiry from' + name, :body => body)
end
Source: (StackOverflow)
I've just made the switch from rvm to rbenv and I'm trying to use bundler for gem management. After running bundle install
and trying to run a simple sinatra app (ruby app.rb
), I get this:
Could not find haml-3.1.4 in any of the sources
Run `bundle install` to install missing gems.
Running bundle install
again had no effect. Also tried bundle update
as suggested by another question response.
This is my Gemfile:
source "http://rubygems.org"
gem "sinatra"
gem "haml"
This is what bundle show
produces
* bundler (1.0.22)
* haml (3.1.4)
* rack (1.4.1)
* rack-protection (1.2.0)
* sinatra (1.3.2)
* tilt (1.3.3)
This is what my app requires:
require "rubygems"
require "bundler/setup"
require 'sinatra'
I'm convince that this is some kind of path issue where bundler and rbenv aren't playing along. I've tried looking through rbenv's documentation but was not able to find to anything.
Note: In a different sinatra app I get the following when trying to run it:
Could not find addressable-2.2.7 in any of the sources
Run `bundle install` to install missing gems.
Supplemental info
echo $PATH
/Users/uri/.rbenv/shims:/Users/uri/.rbenv/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
which bundle
/Users/uri/.rbenv/shims/bundle
Update
If I do bundle install --system
it works. Although I can't see this as being a viable solution.
Source: (StackOverflow)
Is Sinatra multi-threaded? I read else where that "sinatra is multi-threaded by default", what does that imply?
Consider this example
get "/multithread" do
t1 = Thread.new{
puts "sleeping for 10 sec"
sleep 10
# Actually make a call to Third party API using HTTP NET or whatever.
}
t1.join
"multi thread"
end
get "/dummy" do
"dummy"
end
If I access "/multithread" and "/dummy" subsequently in another tab or browser then nothing can be served(in this case for 10 seconds) till "/multithread" request is completed. In case activity freezes application becomes unresponsive.
How can we work around this without spawning another instance of the application?
Source: (StackOverflow)
My ssh keys are definitely set up correctly, as I'm never prompted for the password when using ssh. But capistrano still asks for a password when deploying with cap deploy
. It doesn't ask for the password when I setup with cap deploy:setup
though, strangely enough. It would make the deployment cycle so much smoother without a password prompt.
Specifics: I'm deploying a Sinatra app to a Dreamhost shared account (which uses Passenger). I had followed a tutorial for doing so long back, which worked perfectly back then. Something broke since. I'm using capistrano (2.5.9) and git version 1.6.1.1. Here's my Capfile:
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
set :user, 'ehsanul'
set :domain, 'jellly.com'
default_run_options[:pty] = true
# the rest should be good
set :repository, "ehsanul@jellly.com:git/jellly.git"
set :deploy_to, "/home/ehsanul/jellly.com"
set :deploy_via, :remote_cache
set :scm, 'git'
set :branch, 'deploy'
set :git_shallow_clone, 1
set :scm_verbose, true
set :use_sudo, false
server domain, :app, :web
namespace :deploy do
task :migrate do
run "cd #{current_path}; /usr/bin/rake migrate environment=production"
end
task :restart do
run "touch #{current_path}/tmp/restart.txt"
end
end
after "deploy", "deploy:migrate"
And here's the output of what happens when I cap deploy
, upto the password prompt:
$ cap deploy
* executing `deploy'
* executing `deploy:update'
** transaction: start
* executing `deploy:update_code'
updating the cached checkout on all servers
executing locally: "git ls-remote ehsanul@jellly.com:git/jellly.git deploy"
/usr/local/bin/git
* executing "if [ -d /home/ehsanul/jellly.com/shared/cached-copy ]; then cd /home/ehsanul/jellly.com/shared/cached-copy && git fetch origin && git reset --hard ea744c77b0b939d5355ba2dc50ef1ec85f918d66 && git clean -d -x -f; else git clone --depth 1 ehsanul@jellly.com:git/jellly.git /home/ehsanul/jellly.com/shared/cached-copy && cd /home/ehsanul/jellly.com/shared/cached-copy && git checkout -b deploy ea744c77b0b939d5355ba2dc50ef1ec85f918d66; fi"
servers: ["jellly.com"]
[jellly.com] executing command
** [jellly.com :: out] ehsanul@jellly.com's password:
Password:
** [jellly.com :: out]
** [jellly.com :: out] remote: Counting objects: 7, done.
remote: Compressing objects: 100% (4/4), done.
What could be broken?
Source: (StackOverflow)
I have a simple Sinatra server that I run through textmate but I can't access from another computer on the same network.
I'm running Ruby 1.9.3p327 and Sinatra 1.4.1 on a Mac OS 10.8.3. Firewall is disabled.
I tested the same scenario on different networks and computers.
The computer responds to simple pings but when I try to telnet port 4567 I can't establish a connection.
Source: (StackOverflow)
In Sinatra, is it possible to make content_type 'application/json'
the default? cause I'm writing an api.
Source: (StackOverflow)
On server-side using Sinatra with a stream
block.
get '/stream', :provides => 'text/event-stream' do
stream :keep_open do |out|
connections << out
out.callback { connections.delete(out) }
end
end
On client side:
var es = new EventSource('/stream');
es.onmessage = function(e) { $('#chat').append(e.data + "\n") };
When i using app directly, via http://localhost:9292/
, everything works perfect. The connection is persistent and all messages are passed to all clients.
However when it goes through Nginx, http://chat.dev
, the connection are dropped and a reconnection fires every second or so.
Nginx setup looks ok to me:
upstream chat_dev_upstream {
server 127.0.0.1:9292;
}
server {
listen 80;
server_name chat.dev;
location / {
proxy_pass http://chat_dev_upstream;
proxy_buffering off;
proxy_cache off;
proxy_set_header Host $host;
}
}
Tried keepalive 1024
in upstream
section as well as proxy_set_header Connection keep-alive;
in location
.
Nothing helps :(
No persistent connections and messages not passed to any clients.
Source: (StackOverflow)