Введение в Cramp 21

Pratik Naik, “ Introducing Cramp 21”, public translation into Russian from English More about this translation.

See also 26 similar translations

Translate into another language.

Participants

Tyz48 points
and_rew4 points
Join Translated.by to translate! If you already have a Translated.by account, please sign in.
If you do not want to register an account, you can sign in with OpenID.
Pages: ← previous Ctrl next next untranslated
1 2 3 4 5 6 7

Introducing Cramp 21

Введение в Cramp 21

History of edits (Latest: and_rew 2 years ago) §

Cramp is the latest entry on the ruby web frameworks list. However, unlike all the others, Cramp is an asynchronous framework, always running inside EventMachine reactor loop. Cramp isn’t a good fit for most of the web applications out there. However, Cramp is good at holding and working with a large number of open connections. Hence it’ll work great for things like comet, long polling, streaming API or even when your application needs to handle thousands of concurrent connections.

This article assumes that you’re aware with the evented programming model. If you are not, things below this point might not make much of a sense. If you’re interested in learning, you could start by reading about EventMachine and Twisted.

Install

Инсталляция

History of edits (Latest: Tyz 2 years ago) §

As Cramp requires 0.2.0-pre Arel and 3.0-pre versions of ActiveSupport and ActiveModel. So you’ll have to install them first. This step will be irrelevant after Rails 3 gets released. But for now, the following should install them :

gem install arel --pre

gem install arel --pre

History of edits (Latest: Tyz 2 years ago) §

gem install activemodel --pre

gem install activemodel --pre

History of edits (Latest: Tyz 2 years ago) §

And then,

И затем,

History of edits (Latest: Tyz 2 years ago) §

gem install cramp

gem install cramp

History of edits (Latest: Tyz 2 years ago) §

That’ll install Cramp gem along with all the needed dependencies. Please note that Cramp depends on ActiveSupport 3.0-pre gem, which isn’t backwards compatible and this may affect any other gems requiring ActiveSupport without specifying the version number.

Two faces of Cramp

Cramp comes with two layers : Controller & Model.

1) Cramp::Controller

1) Cramp::Controller

History of edits (Latest: Tyz 2 years ago) §

Cramp::Controller is an asynchronous controller layer, that tries being rack compliant as much as possible. Currently, you must use thin or Rainbows! in order to run it. If you’re using Rainbows!, make sure you use EventMachine concurrency model.

Here’s the “hello world” of Cramp::Controller:

require 'rubygems'

require 'rubygems'

History of edits (Latest: Tyz 2 years ago) §

require 'cramp/controller'

require 'cramp/controller'

History of edits (Latest: Tyz 2 years ago) §

class WelcomeAction < Cramp::Controller::Action

class WelcomeAction < Cramp::Controller::Action

History of edits (Latest: Tyz 2 years ago) §

on_start :send_hello_world

on_start :send_hello_world

History of edits (Latest: Tyz 2 years ago) §

def send_hello_world

def send_hello_world

History of edits (Latest: Tyz 2 years ago) §

render "Hello World"

render "Hello World"

History of edits (Latest: Tyz 2 years ago) §

finish

finish

History of edits (Latest: Tyz 2 years ago) §

end

end

History of edits (Latest: Tyz 2 years ago) §

end

end

History of edits (Latest: Tyz 2 years ago) §

Rack::Handler::Thin.run WelcomeAction, :Port => 3000

Rack::Handler::Thin.run WelcomeAction, :Port => 3000

History of edits (Latest: Tyz 2 years ago) §

Now run it directly :

Запустим его напрямую:

History of edits (Latest: Tyz 2 years ago) §

ruby welcome_action.rb

ruby welcome_action.rb

History of edits (Latest: Tyz 2 years ago) §

Here WelcomeAction is the rack endpoint, which you can use with config.ru or what have you.

Pages: ← previous Ctrl next next untranslated
1 2 3 4 5 6 7