sinatra

How to silently start Sinatra + Thin?

拟墨画扇 提交于 2019-12-22 04:55:07
问题 I have a Sinatra::Base webservice which I want to start from a command line Ruby program, so I have this: # command line program file require 'mymodule/server' puts "Running on 0.0.0.0:4567, debugging to STDOUT..." MyModule::Server.run! bind: '0.0.0.0', port: 4567, environment: :production This works as expected but it throws out: $ myscript Running on 0.0.0.0:4567, debugging to STDOUT... == Sinatra/1.3.1 has taken the stage on 4567 for production with backup from Thin >> Thin web server (v1

Creating a route with Sinatra to only accept a certain Content-type

我的梦境 提交于 2019-12-22 04:37:21
问题 I'm trying to create a route with Sinatra that only accepts POST with an Content-type: application/json without success. My approach is as follows: post '/dogs', :provides => :json do # returns here a json response end Testing with curl, I have seen that :provides => :json configures the route to respond with an Content-Type: application/json . That's right because I want also to respond with a JSON message to the POST request but I really need that this route only respond to POST requests

Transactions in Ruby Sequel module: how to get DB object?

跟風遠走 提交于 2019-12-22 00:26:59
问题 I'm working in a Sinatra application using Sequel. I want to make a transaction, according to the manual I have to use the DB object, how can I get this object from any part of my code? 回答1: You can define it in your base app.rb (or equivalent) or include a separate file where you configure the DB object if you wish. For example, in one of my Sinatra apps, I have an app.rb that includes a class App < Sinatra::Application #lots of stuff here... end require_relative 'models/init' In my models

Rack Sessions getting lost in Chrome

会有一股神秘感。 提交于 2019-12-21 17:34:37
问题 I have an pretty simple app hosted on EC2 built with Sinatra, served with thin behind nginx. The problem is that with Chrome, the session variables get 'lost' in Sinatra. It does not happen in Firefox. This is using Rack::Session::Cookie. This is similar to this issue: Sinatra not persisting session with redirect on Chrome Any insights in how to solve this issues in Chrome would be appreciated. 回答1: Make sure you are setting the following: configure :development do set(:session_secret, 'a

How to make a POST request inside Sinatra code?

走远了吗. 提交于 2019-12-21 12:37:08
问题 Clicking a button in a form will send a POST request to be handled by the following piece of code. post '/register' do #send post request to http://www.randomsite.com #parse response #do something with it @user = User.first(:name => params['regUsername']) if @user == nil @user = User.create( :name => params['regUsername'], :pass => Password.create(params['regPassword']), :email => params['regEmail'], :created_date => Time.now ) redirect '/' else "User already exists." end end How can I send

database connection pooling in ruby

懵懂的女人 提交于 2019-12-21 11:08:10
问题 I just started with Ruby and I am playing with Sinatra, but could not find a way to share database connections between requests. I came from Java web developement and one of the basic things you have to do is to pool the database connections, so I am sure that something similar exists in Ruby, but I just can't find it. ActiveRecord and DataMapper offer this feature but I don't need ORM and just want to make regular SQL queries. Is there some specific approach for Sinatra or there are general

Using Cucumber With Modular Sinatra Apps

 ̄綄美尐妖づ 提交于 2019-12-21 05:32:14
问题 I'm building out a medium-sized application using Sinatra and all was well when I had a single app.rb file and I followed Aslak's guidance up on Github: https://github.com/cucumber/cucumber/wiki/Sinatra As the app grew a bit larger and the app.rb file started to bulge, I refactored out a lot of of the bits into "middleware" style modules using Sinatra::Base, mapping things using a rack-up file (config.ru) etc. The app works nicely - but my specs blew up as there was no more app.rb file for

Good forms helpers for Sinatra?

為{幸葍}努か 提交于 2019-12-21 05:16:26
问题 I am starting to do forms and am looking for form helpers in Sinatra. Sinatra doesn't seem to have built-in forms helpers. The helpers in Padrino look like what I'm after but I don't feel up to porting my app to another framework. Plus it's starting to look like a Rails application. I haven't found any Sinatra forms helpers out there that are the 'de facto' choice. Ideally, I'm after just a set of decent forms helpers that I can include as a gem and just start using, in place of hand-rolling

How to mount a Sinatra application inside another Sinatra app?

有些话、适合烂在心里 提交于 2019-12-21 04:23:07
问题 I am trying to write a Sinatra application that groups components together (sort of like controllers). So for the "blog" related things, I want an app called Blog mounted at /blog . All of the routes contained in the Blog app would be relative to its mounted path, so I could simply define an index route without having to specify the mount path in the route. I originally handled this by using a config.ru file and map ing the routes to the different apps. The problem with this that I ran into,

Sinatra not persisting session with redirect on Chrome

强颜欢笑 提交于 2019-12-21 04:18:06
问题 Sinatra is not persisting my session with a redirect on Chrome. It is creating an entirely new session and i'm losing all my previous session data. As an example (similar to the Sinatra docs), i'm doing something like this: enable :sessions get '/foo' do session[:user_id] = 123 session[:session_id] # "ABC", for example redirect to('/bar') end get '/bar' do # this is "DEF" when responding to Chrome (wrong), # but "ABC" when responding to Firefox or Safari (right) session[:session_id] # this is