sinatra

Accessing session in Sinatra Middleware

≯℡__Kan透↙ 提交于 2019-12-06 04:21:51
问题 I am working on a Sinatra Project and have set some variables in the session for later usage. The scenario for which I need help for is that I want to access the session object in a middleware class. I am using warden for authentication. I want to do something similar below in the Middleware class: class MyMiddleware def initialize(app, options={}) @app = app end def call(env) puts "#{session.inspect}" end end Is there a possibility for doing that? Thoughts? 回答1: You can't use Sinatra's

How do I exclude a path from requiring basic auth in Sinatra

无人久伴 提交于 2019-12-06 03:59:15
问题 I'm writing a smallish web service in Ruby using Sinatra. Access to pretty much everything is controlled using http basic auth (over https in production). There is one particular directory that I want to exclude from requiring authorization. Is there an easy way to do this? 回答1: require 'sinatra' helpers do def protected! unless authorized? response['WWW-Authenticate'] = %(Basic realm="Testing HTTP Auth") throw(:halt, [401, "Not authorized\n"]) end end def authorized? @auth ||= Rack::Auth:

Can't access Docker's exposed port in Ubuntu

老子叫甜甜 提交于 2019-12-06 03:45:35
The Sinatra web app I created works inside the container and I am able to access it at 9393 within the container. Following is my Dockerfile (which uses the image specified by the Dockerfile : jikkujose/red ): FROM jikkujose/red MAINTAINER Jikku Jose <jikkujose@gmail.com> COPY . /banana_app WORKDIR /banana_app RUN bundle install EXPOSE 9393 ENTRYPOINT ["bundle", "exec", "shotgun"] I launched the built image by, docker run -itdP hey When I do, docker ps -a : CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a815e2852c68 hey "bundle exec shotgun 13 minutes ago Up 13 minutes 0.0.0.0:32783-

How do I get Twitter Bootstrap's Less files to work with Sinatra AssetPack?

喜欢而已 提交于 2019-12-06 02:48:06
I am trying to get Bootstrap's Less files working with Sinatra AssetPack , but I am getting Less parser errors. These errors lead me believe that the less files being imported through bootstrap.less are not aware of each other. I have an app.rb file: require 'sinatra/base' require 'sinatra/assetpack' class App < Sinatra::Base set :root, File.dirname(__FILE__) register Sinatra::AssetPack assets do css :bootstrap, [ '/css/bootstrap.css' ] end get '/' do erb :index end # start the server if ruby file executed directly run! if app_file == $0 end I've copied all of the Bootstrap less files into the

omniauth behind proxy

不问归期 提交于 2019-12-06 02:36:34
I have a small app using omniauth and I'm testing it with LinkedIn provider (omniauth-linkedin). Unfortunately my test environment is behind a proxy. So I need to access linkedin through a proxy. How do I set the proxy address so the request to linkedin will succeed? It does not seem to be obeying the http_proxy settings. I figured it out, I think.. ue OmniAuth::Builder do provider :linkedin, "KEY", "SECRET", {:client_options => { :proxy =>\ ENV["HTTP_PROXY"] || ENV["http_proxy"] }} end 来源: https://stackoverflow.com/questions/10326416/omniauth-behind-proxy

How to know what exceptions to rescue?

人盡茶涼 提交于 2019-12-06 02:26:46
I often find myself without knowing what exceptions to rescue when using a specific library of code in Ruby. For instance, I often use HTTParty for any HTTP requests my rails/sinatra app would make. I dug around the code for HTTParty and found a file containing the defined exceptions used. Great! I'll just rescue them when making a request. To test it out, I put in a bogus domain name for the request, but instead of HTTParty::ResponseError exception I expected, I instead get got a SocketError exception. What is the best way to deal with this? I'm aware that HTTParty is a wrapper for Ruby's

Looking for sinatra ajax example

喜夏-厌秋 提交于 2019-12-06 02:16:01
问题 Sorry if this has been covered. Looking for a example of AJAX with Sinatra, specifically to get a partial and apply it to a tag in the DOM, from a javascript button handler. thx 回答1: Check out these examples: https://github.com/fcantelmi/sinatra_ajax/ (tiny example) https://github.com/crguezl/sinatra-jquery-ajax/ http://ididitmyway.heroku.com/past/2011/2/27/ajax_in_sinatra/ 来源: https://stackoverflow.com/questions/9120972/looking-for-sinatra-ajax-example

Run background process in Sinatra

折月煮酒 提交于 2019-12-06 02:05:13
I have got Sinatra/Rails app and an action which starts some long process. Ordinary I make a queue for background jobs. But this case is too simple and background process starts very rarely, so queue is an overhead. So how could I run background process without queue? get "/build_logs/:project" do LogBuilder.new(params[:project]).generate "done" end I've tried to make it as a new Thread or Process fork, but it didn't help. I have had success with this (simplified) in Sinatra: get '/start_process' @@pid = Process.spawn('external_command_to_run') end This returns the Process ID, which you can

If Statement inside Sinatra template

℡╲_俬逩灬. 提交于 2019-12-06 01:25:03
I'd like to to show a message only if on a specific route/page. Essentially, if on /route display a message. I tried going through the Sinatra Docs, but I can't find a specific way to do it. Is there a Ruby method that will make this work? EDIT: Here's an example of what I'd like to do. get '/' do erb :index end get '/page1' do erb :page1 end get '/page2' do erb :page2 end ******************* <!-- Layout File --> <html> <head> <title></title> </head> <body> <% if this page is 'page1' do something %> <% else do something else %> <% end %> <%= yield %> </body> </html> No idea what how to target

Rails + Sinatra apps sharing sessions

江枫思渺然 提交于 2019-12-06 01:13:58
I haven't found a good answer to this yet. How can I get my Rails app and Sinatra app (mounted in my Rails app's config.ru) to share a session successfully? If I visit my Sinatra app first, then the Rails app, I get an error like undefined method sweep for {}:Hash , presumably because Rails uses a custom subclass of Hash for storing session info, and Rack::Session::Cookie doesn't. My code so far: config.ru map "/" do run MyRailsApp::Application end map "/sinatra" do use Rack::Session::Cookie, key: "_app_session", secret: "<SECRET_KEY>" run MySinatraApp end config/initializers/session_store.rb