sinatra

Reading body stream in Sinatra

半世苍凉 提交于 2019-12-12 04:05:17
问题 I'm trying to upload a file with XHR request using PUT method with Sinatra. My first idea was to upload the file and writing the stream directly into a MongoDB GridFS @fs.open("test.iso", "w") do |f| f.write request.body.read end It works, but, it loads the entire file into the RAM and it write it into the MongoDB GridFS. I'd like to avoid this behavior by writing it continuously to the GridFS (stream the file, not loading it and put it in the GridFS) without loading the entire file into the

How can I make Rack::Session::Pool work in a test using Sinatra and RSpec?

送分小仙女□ 提交于 2019-12-12 03:55:51
问题 How can I make sessions work in my RSpec tests? I have tried something like this: describe "createnewlist_route_spec" do include Rack::Test::Methods use Rack::Session::Pool def app @app ||= Sinatra::Application end it "should save listitem to database" do post '/addnewlistitem', {:item => 'testitem'}, :sessions => {:userid => '123'} end end I'm a noob to sinatra, so I might be on the wrong track here... 回答1: This solved my problem: http://gist.github.com/375973 Not quite what I wanted, but it

Redirect Sinatra request changing method and body

泄露秘密 提交于 2019-12-12 03:55:27
问题 Is there a way to handle a GET request on Sinatra and make a PATCH request with a different body on the same server? User makes a request GET /clean_beautiful_api and server redirects it to PATCH /dirty/clogged_api_url_1?crap=2 "{request_body: 1}" ? I want to clean up legacy API without interfering with the functionality. 回答1: If I've understood correctly, the easiest way is to extract the block used for the patch into a helper: patch "/dirty/clogged_api_url_1" crap= params[:crap] end to:

Sinatra with Figaro Gem

杀马特。学长 韩版系。学妹 提交于 2019-12-12 02:45:48
问题 I m trying to use Figaro gem with Sinatra. I have installed Figaro and it created the following file/ folder... /config/application.yml On this file I have added some environment variables... ENV['GMAIL_USERNAME'] ENV['GMAIL_PASSWORD'] Then on my my "app.rb" file I am trying to include the yml file like... require 'config/application.yml' How can I have access to my "ENV['BIG_SECRET']" in my app.rb file? Mail.defaults do delivery_method :smtp, { :address => 'smtp.gmail.com', :port => '587',

Retrieve records which have many-to-many association, using ruby and datamapper

我的未来我决定 提交于 2019-12-12 02:27:14
问题 I'm learning Sinatra, and I have read datamapper documentation and found this n to n relationship example: class Photo include DataMapper::Resource property :id, Serial has n, :taggings has n, :tags, :through => :taggings end class Tag include DataMapper::Resource property :id, Serial has n, :taggings has n, :photos, :through => :taggings end class Tagging include DataMapper::Resource belongs_to :tag, :key => true belongs_to :photo, :key => true end What I understood from the code above is

Sinatra: NameError at '/route', undefined local variable or method

旧街凉风 提交于 2019-12-12 02:21:45
问题 I'm trying to build an admin backend for a cms website. this is the structure of my application ├── app.rb ├── Gemfile ├── models │ └── models.rb ├── routes │ └── routes.rb └── views ├── categories.erb ├── # ... other view files app.rb require 'sinatra' require 'data_mapper' require 'dm-core' require 'dm-migrations' require 'digest' enable :sessions DataMapper.setup(:default, 'mysql://username:password@localhost/database') require './models/models.rb' require './routes/routes.rb' DataMapper

Sinatra Ruby iterate over an array in a hash to send jQuery Sortable to database

白昼怎懂夜的黑 提交于 2019-12-12 01:32:35
问题 I've isolated my lack of knowledge down to this snipit I keep tweaking in irb. I'm trying to use jQuery's UI Sortable to send a post to the database, so far I can get it to send the update but it sends the entire array instead of each array item at a time. It's amazing that I have not found one post yet with someone trying Sinatra and Sortable. Hmmm Hash and sinatra code superslide = {"weee"=>["3", "4", "1", "2"]} moo = Sort.all moo.each do |o| o.sortorder = superslide['weee'] puts o

Capybara server can't access data created in a spec

不打扰是莪最后的温柔 提交于 2019-12-12 00:58:30
问题 I am running Sinatra, Capybara and RSpec. I am testing javascript interactions with webkit headless browser. I am using factory girl to create data that I need to be present for my test. I then use capybara to perform interactions with my application. During these interactions the data that I created at the start of the test is no longer available. It's created with no issue however when debugging in the controller the database is empty. Why is my database empty in the controller but not in

Add routes to Dashing dashboard

假如想象 提交于 2019-12-12 00:35:36
问题 How do I add a route in my dashboard that I can access, for example... get '/:id' do protected! return params[:'id'] end Which I can call from http://localhost:3030?id=1234 回答1: The easiest way to do this would be to define a new application and call it inside the config.ru that gets created by Dashing. For instance, I created a new file called my_app.rb in a dashing repo with the following contents: # my_app.rb require 'sinatra/base' class MyApp < Sinatra::Base get '/:id' do "My own custom

Sub routing in Sinatra

一世执手 提交于 2019-12-11 23:45:41
问题 Is there a way by which one can map different controllers to urls that are related to each other, specifically when one is a sub resource of the other? To be more specific, here's an example: I have 2 types of resources: jobs and articles . A job contains multiple articles . Despite their relationship, I want to handle the actual code related to each in separate files. As such I have: helpers/job_api.rb and helpers/article_api.rb They each extend SinatraBase like so: class ArticleAPI <