<commit_before>m2r
===

A [Mongrel2](http://mongrel2.org/) [™](#legal) backend handler written in Ruby. Also includes Rack adpater to get you up and running quickly.

[![Build Status](https://secure.travis-ci.org/perplexes/m2r.png)](http://travis-ci.org/perplexes/m2r) [![Dependency Status](https://gemnasium.com/perplexes/m2r.png)](https://gemnasium.com/perplexes/m2r)

Installation
------------

You'll need following prerequisites first:

* [Mongrel2](http://mongrel2.org/downloads)
* [ØMQ](http://www.zeromq.org/area:download)

Next, in your `Gemfile` add:

```ruby
gem 'm2r'
```

And finally run:

```
bundle install
```

Guides
------

### Running Rack Application

#### Gemfile

Add `m2r` to `Gemfile` and run `bundle install`

#### Mongrel 2

[Configure `Handler`](http://mongrel2.org/static/book-finalch4.html#x6-260003.4) for your application:

```
rack_example = Handler(
  send_spec  = "tcp://127.0.0.1:9997",
  send_ident = "14fff75f-3474-4089-af6d-bbd67735ab89",
  recv_spec  = "tcp://127.0.0.1:9996",
  recv_ident = ""
)
```

#### Start

```bash
[bundle exec] rackup -s mongrel2 application.ru
```

Add `-O option_name` to provide options for m2r handler:

```bash
[bundle exec] rackup -s mongrel2 another.ru -O recv_addr=tcp://127.0.0.1:9995 -O send_addr=tcp://127.0.0.1:9994
```

#### Options

* `recv_addr` - This is the `send_spec` option from `Handler` configuration in `mongrel2.conf`. Default: `tcp://127.0.0.1:9997`
* `send_addr` - This is the `recv_spec` option from `Handler` configuration in your `mongrel2.conf`. Default: `tcp://127.0.0.1:9996`
* `factory`   - Use it to load custom `ConnectionFactory` that implements rules for ZMQ connections to Mongrel 2.

#### Custom Connection Factory

ZMQ allows to set multiple options and connect to large number of endpoints. Providing every ZMQ option for handler connections
would be troublesome. Instead you can use your custom implementation that deals only with that fact.

##### Automatic require of custom connection factory

The first way to do it is to implement custom class in a file that can be required with 'm2r/connection_factory/custom_name'.
The location of such file might depends on how `$LOAD_PATH` is configured but for standard Rails application or gem that
would like to depend on `m2r` it would be: `lib/m2r/connection_factory/custom_name`.

Implement the Factory in the file:

```ruby
module M2R
  class ConnectionFactory
    # Just exemplary implementation ...
    class CustomName < ConnectionFactory
      def initialize(options)
        # OpenStruct with rackup options for the handler (added with -O)
        @options = options
      end

      def connection
        request_socket = @context.socket(ZMQ::PULL)
        request_socket.connect("tcp://127.0.0.1:2222")
        request_socket.setsockopt(ZMQ::RECONNECT_IVL, 5)

        response_socket = @context.socket(ZMQ::PUB)
        response_socket.connect("tcp://127.0.0.1:3333")
        response_socket.setsockopt(ZMQ::HWM, 100)
        response_socket.setsockopt(ZMQ::RECONNECT_IVL, 5)

        Connection.new(request_socket, response_socket)
      end
    end
  end<commit_msg>Readme improved<commit_after>m2r
===

A [Mongrel2](http://mongrel2.org/) [™](#legal) backend handler written in Ruby. Also includes Rack adpater to get you up and running quickly.

[![Build Status](https://secure.travis-ci.org/perplexes/m2r.png)](http://travis-ci.org/perplexes/m2r) [![Dependency Status](https://gemnasium.com/perplexes/m2r.png)](https://gemnasium.com/perplexes/m2r)

Documentation
-------------

* [Tutorial](http://documentup.com/perplexes/m2r/recompile)
* [API](http://www.rubydoc.info/gems/m2r/frames)

Installation
------------

You'll need following prerequisites first:

* [Mongrel2](http://mongrel2.org/downloads)
* [ØMQ](http://www.zeromq.org/area:download)

Next, in your `Gemfile` add:

```ruby
gem 'm2r'
```

And finally run:

```
bundle install
```

Guides
------

### Running Rack Application

#### Gemfile

Add `m2r` to `Gemfile` and run `bundle install`

#### Mongrel 2

[Configure `Handler`](http://mongrel2.org/static/book-finalch4.html#x6-260003.4) for your application:

```
rack_example = Handler(
  send_spec  = "tcp://127.0.0.1:9997",
  send_ident = "14fff75f-3474-4089-af6d-bbd67735ab89",
  recv_spec  = "tcp://127.0.0.1:9996",
  recv_ident = ""
)
```

#### Start

```bash
[bundle exec] rackup -s mongrel2 application.ru
```

Add `-O option_name` to provide options for m2r handler:

```bash
[bundle exec] rackup -s mongrel2 another.ru -O recv_addr=tcp://127.0.0.1:9995 -O send_addr=tcp://127.0.0.1:9994
```

#### Options

* `recv_addr` - This is the `send_spec` option from `Handler` configuration in `mongrel2.conf`. Default: `tcp://127.0.0.1:9997`
* `send_addr` - This is the `recv_spec` option from `Handler` configuration in your `mongrel2.conf`. Default: `tcp://127.0.0.1:9996`
* `factory`   - Use it to load custom `ConnectionFactory` that implements rules for ZMQ connections to Mongrel 2.

#### Custom Connection Factory

ZMQ allows to set multiple options and connect to large number of endpoints. Providing every ZMQ option for handler connections
would be troublesome. Instead you can use your custom implementation that deals only with that fact.

##### Automatic require of custom connection factory

The first way to do it is to implement custom class in a file that can be required with `m2r/connection_factory/custom_name`.
The location of such file might depends on how `$LOAD_PATH` is configured but for standard Rails application or gem that
would like to depend on `m2r` it would be: `lib/m2r/connection_factory/custom_name`.

Implement the Factory in the file:

```ruby
module M2R
  class ConnectionFactory
    # Just exemplary implementation ...
    class CustomName < ConnectionFactory
      def initialize(options)
        # OpenStruct with rackup options for the handler (added with -O)
        @options = options
      end

      def connection
        request_socket = @context.socket(ZMQ::PULL)
        request_socket.connect("tcp://127.0.0.1:2222")
        request_socket.setsockopt(ZMQ::RECONNECT_IVL, 5)

        response_socket = @context.socket(ZMQ::PUB)
        response_socket.connect("tcp://127.0.0.1:3333")
        response_socket.setsockopt(ZMQ::HWM, 100)
        response_socket.setsockopt(ZMQ::RECONNECT_IVL, 5)

        Connection.new(request_socket, response_socket)
      end
    end
  end
{'fraction_non_alphanumeric': 0.13547682222569066, 'fraction_numerical': 0.04307788356485094, 'mean_word_length': 3.8619119878603945, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 16, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2690514', 'n_tokens_mistral': 2303, 'n_tokens_neox': 2113, 'n_words': 587}