sinatra

What is a very simple authentication scheme for Sinatra/Rack

不羁岁月 提交于 2019-12-04 07:25:06
问题 I am busy porting a very small web app from ASP.NET MVC 2 to Ruby/Sinatra. In the MVC app, FormsAuthentication.SetAuthCookie was being used to set a persistent cookie when the users login was validated correctly against the database. I was wondering what the equivalent of Forms Authentication would be in Sinatra? All the authentication frameworks seem very bulky and not really what I'm looking for. 回答1: Here is a very simple authentication scheme for Sinatra. I’ll explain how it works below.

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

你。 提交于 2019-12-04 07:14:25
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? 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::Basic::Request.new(request.env) @auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == [

generating css file from new sass format (scss) with sinatra and haml

瘦欲@ 提交于 2019-12-04 06:56:24
I am writing a sinatra app with haml and sass. When I try to link in the stylesheet with a scss extension located in my views folder I get the following error: NoMethodError at /nav.css undefined method `scss' Here is my get method get '/nav.css' do content_type 'text/css', :charset => 'utf-8' scss :nav end I have only gotten this to work when I switch to the older sass syntax. I also have to change the nav.scss to nav.sass and the get method to sass :nav I have also tried just having sass :nav with nav.scss and sass :nav with nav.sass but still scss syntax Excerpt from Sinatra README ## You

Looking for sinatra ajax example

天大地大妈咪最大 提交于 2019-12-04 06:27:10
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 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

Mongo find by regex: return only matching string

家住魔仙堡 提交于 2019-12-04 04:33:58
问题 My application has the following stack: Sinatra on Ruby -> MongoMapper -> MongoDB The application puts several entries in the database. In order to crosslink to other pages, I've added some sort of syntax. e.g.: Coffee is a black, caffeinated liquid made from beans. {Tea} is made from leaves. Both drinks are sometimes enjoyed with {milk} In this example {Tea} will link to another DB entry about tea. I'm trying to query my mongoDB about all 'linked terms'. Usually in ruby I would do something

Rack Session Cookie and Sinatra - setting and accessing data

谁说我不能喝 提交于 2019-12-04 03:43:14
I was using Rack Session Pool, however my users would get kicked off one webserver thread onto another making the session data expire. I started toying around with just enable :sessions in Sinatra, however I am unable to use that because I have mutliple apps using Sinatra (same key it appears to be using - not sure if this is because its the same host or not) So since my apps would break each other, I now am trying Rack Session Cookie and setting the variables (same thing as enable :sessions, but you can set the variables) Great so that works! But now I cannot access the session data the way I

Sinatra Variable Scope

北城余情 提交于 2019-12-04 02:40:20
Take the following code: ### Dependencies require 'rubygems' require 'sinatra' require 'datamapper' ### Configuration config = YAML::load(File.read('config.yml')) name = config['config']['name'] description = config['config']['description'] username = config['config']['username'] password = config['config']['password'] theme = config['config']['theme'] set :public, 'views/themes/#{theme}/static' ### Models DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/marvin.db") class Post include DataMapper::Resource property :id, Serial property :name, String property :body, Text property :created_at,

Stubbing RestClient response in RSpec

こ雲淡風輕ζ 提交于 2019-12-04 02:30:22
I have the following spec... describe "successful POST on /user/create" do it "should redirect to dashboard" do post '/user/create', { :name => "dave", :email => "dave@dave.com", :password => "another_pass" } last_response.should be_redirect follow_redirect! last_request.url.should == 'http://example.org/dave/dashboard' end end The post method on the Sinatra application makes a call to an external service using rest-client. I need to somehow stub the rest client call to send back canned responses so I don't have to invoke an actual HTTP call. My application code is... post '/user/create' do

How do I use .html.erb as a file extension for my views with Sinatra?

一个人想着一个人 提交于 2019-12-04 02:09:36
If I have the following Sinatra code: get '/hi' do erb :hello end This works great if I have a file called views/hello.erb . However if I have a file called views/hello.html.erb Sinatra can't find the file and gives me an error. How do I tell Sinatra I want it to look for .html.erb as a valid .erb extension? Sinatra uses Tilt to render its templates, and to associate extensions with them. All you have to do is tell Tilt it should use ERB to render that extension: Tilt.register Tilt::ERBTemplate, 'html.erb' get '/hi' do erb :hello end Edit to answer follow-up question. There's no #unregister

Loading Stylesheets in Sinatra

混江龙づ霸主 提交于 2019-12-04 02:08:17
I'm using Sinatra, and I've been trying to load in some stylesheets. I've tried just the normal html link tag in my erb, but that hasn't worked. ive tried <head> <link href="style.css" rel="stylesheet" type="text/css" /> </head> It's not an issue with the url i'm using, is there some special way of achieving this? When you use href="style.css" , you’re specifying a relative link to the stylesheet. The actual path that your browser will request will depend on the url of the current page, so for example if you have a route like: get '/things/:id' do #look up thing with id = :id erb :my_view end