sinatra

How to convert a Net::HTTP response to a certain encoding in Ruby 1.9.1?

人走茶凉 提交于 2019-11-30 13:01:41
I have a Sinatra application ( http://analyzethis.espace-technologies.com ) that does the following Retrieve an HTML page (via net/http) Create a Nokogiri document from the response.body Extract some info and send it back in the response. The response should be UTF-8 encoded So I came to the problem while trying to read sites that use windows-1256 encodings like www.filfan.com or www.masrawy.com. The problem is the result of the encoding conversion is not correct though no errors are thrown. The net/http response.body.encoding gives ASCII-8BIT which can not be converted to UTF-8 If I do

How to detect language from URL in Sinatra

随声附和 提交于 2019-11-30 12:20:51
问题 I have a multi-language website and I'm puting the language in the URL like domain.com/en/. When the user doesn't put the language in the URL I want to redirect him to the page in the main language like "domain.com/posts" to "domain.com/en/posts". Is there an easy way to do this with Sinatra? I have more than one hundred routes. So doing this for every route is not a very good option. get "/:locale/posts" do... end get "/posts" do... end Can someone help me? Thanks 回答1: Use a before filter,

Stream console output through HTTP (with Ruby)

女生的网名这么多〃 提交于 2019-11-30 11:37:24
问题 I am trying to run some commands remotely and SSH'ing in to the machine is not an option. What I am trying to do is setup a Sinatra app that runs some specific commands and streams the output through HTTP. The sample action looks like this: get "/log" do `tail -f some.log` end 1 As far as I've read, I need to use Unicorn (or Mongrel) because Thin does not support streaming data 2 I think I need to pipe the commands output through some kind of IO ruby object I almost know how to do (1) but

Is it possible include Nesta CMS into Rails3 application?

蓝咒 提交于 2019-11-30 10:38:00
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 ? 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 to refer to Nesta in your routes by referring to it as Nesta::App (I only merged the commit that allows you

Mustache and Haml

会有一股神秘感。 提交于 2019-11-30 09:54:46
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) 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 ): {{#data}} #ok {{#items}} {{#item}} %b ID: {{id}} {{/item}} {{/items}} {{/data}} I get following result: <div

cannot load such file — rack/handler/puma

偶尔善良 提交于 2019-11-30 09:47:01
My setup and the error I get an error when I start my Sinatra application with rackup and puma. My config.ru file looks like this: #\ -s puma require './controller/main.rb' run Sinatra::Application So when I now use rackup I get this error: /home/username/.rvm/gems/ruby-1.9.3-p392/gems/rack-1.5.2/lib/rack/handler.rb:76:in `require': cannot load such file -- rack/handler/puma (LoadError) I use ruby 1.9.3p392 (2013-02-22 revision 39386) [i686-linux] What I have tried so far My first thought was that I forgot to install puma, or puma is broken in some way. So I tried: puma -v puma version 2.0.1

Sinatra with a persistent variable

前提是你 提交于 2019-11-30 08:41:38
My sinatra app has to parse a ~60MB XML-file. This file hardly ever changes: on a nightly cron job, It is overwritten with another one. Are there tricks or ways to keep the parsed file in memory, as a variable, so that I can read from it on incoming requests, but not have to parse it over and over for each incoming request? Some Pseudocode to illustrate my problem. get '/projects/:id' return @nokigiri_object.search("//projects/project[@id=#{params[:id]}]/name/text()") end post '/projects/update' if params[:token] == "s3cr3t" @nokogiri_object = reparse_the_xml_file end end What I need to know,

Passwords in git tree + Heroku + Github

◇◆丶佛笑我妖孽 提交于 2019-11-30 08:27:10
I have a very basic application done in Sinatra I have deployed it in Heroku (http://frasesbarrio.heroku.com) There is a button for sharing in Facebook and in fact it can be used as a Facebook application. For Facebook authentification, my application has its own app id and app secret (right now they are two constants in the main file of the source) I also want to share my code in Github for everyone to enjoy. How can I send the code with the app id and secret to Heroku and without them to Github in a maintainable fashion? (I mean that I will keep changing the app now and then, deploying and

How mix in routes in Sinatra for a better structure

筅森魡賤 提交于 2019-11-30 06:22:21
问题 I found nothing about how I can mix-in routes from another module, like this: module otherRoutes get "/route1" do end end class Server < Sinatra::Base include otherRoutes get "/" do #do something end end Is that possible? 回答1: You don't do include with Sinatra. You use extensions together with register . I.e. build your module in a separate file: require 'sinatra/base' module Sinatra module OtherRoutes def self.registered(app) app.get "/route1" do ... end end end register OtherRoutes # for

What's the best way to talk to a database while using Sinatra?

匆匆过客 提交于 2019-11-30 06:15:50
问题 As I understand it, the Sinatra framework, unlike Rails, does not provide an ORM. In that case, how do you talk to a DB in a Sinatra app? Or is Sinatra only for apps that don't use a DB? 回答1: If you like ActiveRecord, use that. Or something else. Datamapper, for instance. For AR with SQLite, this works: require 'rubygems' # may not be needed, depending on platform require 'sinatra' require 'active_record' class Article < ActiveRecord::Base end get '/' do Article.establish_connection( :adapter