sinatra

Is a global variable defined inside a Sinatra route shared between requests?

落爺英雄遲暮 提交于 2019-12-09 04:50:25
问题 Say I've got: get '/' do $random = Random.rand() response.body = $random end If I have thousands of requests per second coming to /, will the $random be shared and 'leak' outside the context or will it act like a 'local' variable to the get block? I imagine if it was defined outside of the context of get '/' do it would indeed be shared but I wonder if there's a mechanic to $ in ruby that I'm not aware of. 回答1: This part of the Sinatra README about scope is always helpful to read but if you

How to enable SSL for a standalone Sinatra app?

扶醉桌前 提交于 2019-12-09 04:44:24
问题 I want to write a quick server app in Sinatra. It has to be self-contained (i.e. not use apache/nginx/passenger) but also has to support SSL. Is there an easy way to enable SSL support for Sinatra (using WEBRick for example)? 回答1: To do this with MRI ruby, use the following monkeypatch: sinatra_ssl.rb : require 'webrick/https' module Sinatra class Application def self.run! certificate_content = File.open(ssl_certificate).read key_content = File.open(ssl_key).read server_options = { :Host =>

Multipart response in Ruby/Rack

淺唱寂寞╮ 提交于 2019-12-09 01:23:50
问题 I want my server to send a multipart response (multipart/x-mixed-replace). I'd prefer some kind of solution using the Sinatra framework or a generic Rack app, but any example in ruby would be nice. Here's the equivalent of what I'm trying to do, in PHP: <?php header('Content-type: multipart/x-mixed-replace;boundary="rn9012"'); print "--rn9012\n"; print "Content-type: application/xml\n\n"; print "<?xml version='1.0'?>\n"; print "<content>First Part</content>\n"; print "--rn9012\n"; flush();

Working around the lack of context in Sinatra's route methods

故事扮演 提交于 2019-12-08 19:42:32
问题 I have been having an issue with missing instances and nilClass errors when calling my routes. After delving around within the source it seems like the generate_method call basically creates a new method using the block of the initial method. get "/" do @some_local_instance.do_something() end So in the above method there could very well be a local variable within that class called some_local_instance, however when the rote is actually evaluated it has no context as to where the method was

Pony yandex.ru and mail.ru specifics

霸气de小男生 提交于 2019-12-08 19:41:29
I am creating a form in Sinatra that will be sending data to an e-mail on submit using Pony gem. This is my code so far: post '/pemco' do Pony.mail( :from => params[:name] + "<" + params[:email] + ">", :to => '___@yandex.ru', :subject => params[:name] + " has contacted you", :body => params[:message], :via => :smtp, :via_options => { :address => 'smtp.yandex.ru', :port => '465', :enable_starttls_auto => true, :user_name => '___', :password => '___', :authentication => :plain }) redirect '/' end I press submit , response pends for some time and then I get Net::ReadTimeout file: protocol.rb

cannot start sinatra process - eventmachine “no acceptor”

Deadly 提交于 2019-12-08 19:39:46
问题 I have a Sinatra app that I run as a daemon, using Apache port-forwarding to mediate between port 80 and port 7655. This has been working fine in the past. Today, not so well. I cannot figure out why. Problem: sudo ruby my_process.rb returns: /var/lib/gems/1.9.1/gems/eventmachine-1.0.0/lib/eventmachine.rb:526:in `start_tcp_server': no acceptor (port is in use or requires root privileges) (RuntimeError) Tried: updating all system packages, updating all gems. No help (except for the more clear

How to define a method to be called from the configure block of a modular sinatra application?

拜拜、爱过 提交于 2019-12-08 19:38:19
问题 I have a Sinatra app that, boiled down, looks basically like this: class MyApp < Sinatra::Base configure :production do myConfigVar = read_config_file() end configure :development do myConfigVar = read_config_file() end def read_config_file() # interpret a config file end end Unfortunately, this doesn't work. I get undefined method read_config_file for MyApp:Class (NoMethodError) The logic in read_config_file is non-trivial, so I don't want to duplicate in both. How can I define a method that

How do I require NumberHelper and make it work?

吃可爱长大的小学妹 提交于 2019-12-08 15:55:42
问题 I'm trying to write a simple Sinatra thingy but I need ActionView::Helpers::NumberHelper from action pack. http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html The question is, how do I install and use it? irb(main):001:0> require 'action_view/helpers/number_helper' irb(main):002:0> number_with_precision(1) NoMethodError: undefined method `number_with_precision' for main:Object irb(main):004:0> ActionView::Helpers::NumberHelper.number_with_precision(1) NoMethodError:

POSTing an HTML form to remote.cgi - written in Ruby?

青春壹個敷衍的年華 提交于 2019-12-08 14:15:51
问题 I am working on a website hosted on microsoft's office live service. It has a contact form enabling visitors to get in touch with the owner. I want to write a Ruby script that sits on a seperate sever and which the form will POST to. It will parse the form data and email the details to a preset address. The script should then redirect the browser to a confirmation page. I have an ubuntu hardy machine running nginx and postfix. Ruby is installed and we shall see about using Thin and it's Rack

How do I work with checkboxes with DataMapper and Sinatra?

≡放荡痞女 提交于 2019-12-08 13:29:36
问题 I'm trying to make a simple room management service. The rooms have these properties: class Room include DataMapper::Resource validates_is_unique :number property :id, Serial property :number, Integer property :guest, String property :status, Enum[ :free, :occupied ], :default => :free end Then I create a new room like this post '/new' do content_type :json @room = Room.new :guest => params[:guest], :number => params[:number], :status => params[:status] if @room.save { :number => @room.number