sinatra

Start and call Ruby HTTP server in the same script

落爺英雄遲暮 提交于 2019-11-30 20:46:43
问题 I wonder how I could start a Ruby Rack application (such as Sinatra) and call it with Net::HTTP or similar in the same script. Of couse I could do something like... require 'sinatra/base' require 'net/http' t = Thread.new do class App < Sinatra::Base get '/' do 'Hi!' end end App.run! :host => 'localhost', :port => 1234 end sleep 2 puts Net::HTTP.start('localhost', 1234) { |http| http.get('/') }.body t.join puts 'Bye!' ...but it doesn't feel optimal to sleep for two seconds, waiting for Thin

Having difficulty accessing validation errors in Sinatra

和自甴很熟 提交于 2019-11-30 19:58:31
问题 I have a very basic form, with some very basic validations (though I need to create a custom validation later... you'll probably see a question on that tomorrow. =P ), but I'm having trouble showing the user the validation errors. Here's my main Sinatra file: $LOAD_PATH.unshift(File.dirname(__FILE__)) require 'sinatra' require 'rubygems' require 'datamapper' require 'dm-core' require 'dm-validations' require 'lib/authorization' DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/entries.db")

Calling a Sinatra app instance method from TestCase

痴心易碎 提交于 2019-11-30 18:27:02
I have an util method into a Sinatra application and I would like to tested from my TestCase . The problem is that I don't know how to invoke it, if I just use app.util_method I have the error NameError: undefined local variable or method 'util_method' for #<Sinatra::ExtendedRack:0x007fc0c43305b8> my_app.rb: class MyApp < Sinatra::Base # [...] routes methods # utils methods def util_method return "hi" end end my_app_test.rb: require "my_app.rb" require "test/unit" require "rack/test" class MyAppTest < Test::Unit::TestCase include Rack::Test::Methods def app MyApp.new end # [...] routes methods

How to receive a JSON object with Rack

…衆ロ難τιáo~ 提交于 2019-11-30 15:45:59
问题 I have a very simple Ruby Rack server, like: app = Proc.new do |env| req = Rack::Request.new(env).params p req.inspect [200, { 'Content-Type' => 'text/plain' }, ['Some body']] end Rack::Handler::Thin.run(app, :Port => 4001, :threaded => true) Whenever I send a POST HTTP request to the server with an JSON object: { "session": { "accountId": String, "callId": String, "from": Object, "headers": Object, "id": String, "initialText": String, "parameters": Object, "timestamp": String, "to": Object,

How to receive a JSON object with Rack

十年热恋 提交于 2019-11-30 15:07:07
I have a very simple Ruby Rack server, like: app = Proc.new do |env| req = Rack::Request.new(env).params p req.inspect [200, { 'Content-Type' => 'text/plain' }, ['Some body']] end Rack::Handler::Thin.run(app, :Port => 4001, :threaded => true) Whenever I send a POST HTTP request to the server with an JSON object: { "session": { "accountId": String, "callId": String, "from": Object, "headers": Object, "id": String, "initialText": String, "parameters": Object, "timestamp": String, "to": Object, "userType": String } } I receive nothing. I can detect the request received but can't get the data. The

Sinatra Warden with existing Ruby on Rails application that uses Devise

纵饮孤独 提交于 2019-11-30 14:51:28
I am trying to split my current Ruby on Rails 3 web-application and it's web-services (API). My web-application is running on Heroku and implements API as a namespaced route within my application. For example /events returns a HTML page and /api/v1/events returns a JSON data. According to some best practices , I want to split those into two different applications. I have chosen Sinatra to implement the API application. It works now for simple requests where authentication is not required. My Ruby on Rails 3 application is using Devise to authenticate users. There's also ability to login with

Specifying Content Type in rspec

自闭症网瘾萝莉.ら 提交于 2019-11-30 14:36:36
问题 I'm trying to build an rspec test that sends JSON (or XML) via POST. However, I can't seem to actually get it working: json = {.... data ....}.to_json post '/model1.json',json,{'CONTENT_TYPE'=>'application/json'} and this json = {.... data ....}.to_json post '/model1.json',json,{'Content-Type'=>'application/json'} any ideas? THANKS! 回答1: There's a way to do this described in this thread -- it's a hack, but it seems to work: @request.env["HTTP_ACCEPT"] = "application/json" json = { ... data ..

Is it possible include Nesta CMS into Rails3 application?

坚强是说给别人听的谎言 提交于 2019-11-30 14:19:09
问题 I'd like "to mount" a Nesta CMS app onto a Rails3 app This should be possible couse of being Nesta a Sinatra app, which should be a Rack mountable layer, ... but how would you do it ? Where will you start from ? Does anybody has experiences on this topic ? Suggested docs ? 回答1: Hey Luca. I've been meaning to write this up a for a month or two. You just need to mount Nesta as a Rack app, using Rails Metal. Have a watch of this: http://railscasts.com/episodes/222-rack-in-rails-3 You'll be able

Mustache and Haml

扶醉桌前 提交于 2019-11-30 14:14:43
问题 I have got this haml/mustache template: {{#data}} ok {{#items}} {{#item}} %b ID: {{id}} {{/item}} {{/items}} {{/data}} And I have got Illegal nesting: nesting within plain text is illegal Error. I render it in Sinatra Mustache.render(haml(:index), hash) 回答1: I'm not sure about rendering with Sinatra, but with this command: cat example.yml foo.haml.mustache | mustache | haml -e this data file example.yml --- data: - items: - item: - id: 1 - id: 2 - id: 3 --- and template (foo.haml.mustache ):

Could someone give me an example of node.js application

心已入冬 提交于 2019-11-30 14:00:51
I'm trying to understand the differences between some of the newer web programming frameworks that now exists, namely Node.js, Rails, and Sinatra. Could someone give me an example of applications that would work best on each of the frameworks? That is to say, what is an application that would be best suited for Node.js as opposed to Rails or Sinatra and what is an application that is best suited for Rails as opposed to Node.js and Sinatra etc..... Sinatra and Rails are both web frameworks. They provide common web development abstractions, such as routing, templating, file serving, etc. node.js