<commit_before>[![Build Status](https://travis-ci.org/smakagon/eventify_pro.svg?branch=master)](https://travis-ci.org/smakagon/eventify_pro)

# EventifyPro

Client for [EventifyPro API](http://api.eventify.pro). Allows to publish events from Ruby-applications.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'eventify_pro'
```

And then execute:

    $ bundle install

Or install it yourself as:

    $ gem install eventify_pro

## Usage
___
### Getting Started:
This guide assumes you have created an account and obtained an API key from [EventifyPro](http://eventify.pro)

It's really easy to start publishing events with EventifyPro, just 2 lines of code:

```ruby
client = EventifyPro::Client.new(api_key: 'secret')
client.publish(type: 'OrderCreated', data: { order_id: 1, amount: 500 })
```

### Ruby On Rails
In a basic Ruby On Rails application you could create class under `lib` directory:

```ruby
# lib/eventify.rb

class Eventify
  def self.client
    @client ||= EventifyPro::Client.new(api_key: 'secret', logger: Rails.logger)
  end

  def self.publish(event_type, data)
    client.publish(type: event_type, data: data)
  end
end
```
Great, now to publish event from any place of your app use:

```ruby
Eventify.publish('UserSignedUp', { user_id: 1, first_name: 'John', last_name: 'Doe' })
```

* `type`: type of the event

* `data`: key and value pairs pertaining to the event

PS: Don't forget to enable loading of the `lib` directory in application.rb file:

```ruby
# config/application.rb

config.autoload_paths << Rails.root.join('lib')
```
___
#### Example:
```ruby
class OrdersController < ApplicationController
  def create
    @order = Order.new(params[:order])

    if @order.save
      Eventify.publish(type: 'OrderCreated', data: { order_id: @order.id, amount: @order.amount })
      redirect_to @order, notice: 'Post was successfully created.'
    else
      render :new
    end
  end
end
```
___
## Configuration

### `raise_error:`
* By default, EventifyPro will swallow errors. It will return `true` or `false` depending on the result of publishing.
* It's possible to pass `raise_errors: true`. In that case EventifyPro will throw EventifyPro::Error exception if something went wrong.

Example:
```ruby
client = EventifyPro::Client.new(api_key: 'secret', raise_errors: true)
begin
  client.publish(type: 'OrderCreated', data: { order_id: 1, amount: 1500 })
rescue EventifyPro::Error
  # exception handling
end
```

### `logger`
* By default, EventifyPro will use STDOUT to print error details.
* You can provide any logger that responds to `.info(message)`. For example `Rails.logger`.

Example:<commit_msg>Documentation updated<commit_after>[![Build Status](https://travis-ci.org/smakagon/eventify_pro.svg?branch=master)](https://travis-ci.org/smakagon/eventify_pro)

# EventifyPro

Client for [EventifyPro API](http://api.eventify.pro). Allows to publish events from Ruby-applications.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'eventify_pro'
```

And then execute:

    $ bundle install

Or install it yourself as:

    $ gem install eventify_pro

## Usage
___
### Getting Started:
This guide assumes you have created an account and obtained an API key from [EventifyPro](http://eventify.pro)

It's really easy to start publishing events with EventifyPro, just 2 lines of code:

```ruby
client = EventifyPro::Client.new(api_key: 'secret')
client.publish(type: 'OrderCreated', data: { order_id: 1, amount: 500 })
```

### Ruby On Rails
In a basic Ruby On Rails application you could create class under `lib` directory:

```ruby
# lib/eventify.rb

class Eventify
  def self.client
    @client ||= EventifyPro::Client.new(api_key: 'secret', logger: Rails.logger)
  end

  def self.publish(event_type, data)
    client.publish(type: event_type, data: data)
  end
end
```
Great, now to publish event from any place of your app use:

```ruby
Eventify.publish('UserSignedUp', { user_id: 1, first_name: 'John', last_name: 'Doe' })
```

* `type`: type of the event

* `data`: key and value pairs pertaining to the event

PS: Don't forget to enable loading of the `lib` directory in application.rb file:

```ruby
# config/application.rb

config.autoload_paths << Rails.root.join('lib')
```
___
#### Example:
```ruby
class OrdersController < ApplicationController
  def create
    @order = Order.new(params[:order])

    if @order.save
      Eventify.publish(type: 'OrderCreated', data: { order_id: @order.id, amount: @order.amount })
      redirect_to @order, notice: 'Post was successfully created.'
    else
      render :new
    end
  end
end
```
___
## Configuration

#### `raise_error:`
* By default, EventifyPro will swallow errors. It will return `true` or `false` depending on the result of publishing.
* It's possible to pass `raise_errors: true`. In that case EventifyPro will throw EventifyPro::Error exception if something went wrong.

Example:
```ruby
client = EventifyPro::Client.new(api_key: 'secret', raise_errors: true)
begin
  client.publish(type: 'OrderCreated', data: { order_id: 1, amount: 1500 })
rescue EventifyPro::Error
  # exception handling
end
```

#### `logger`
* By default, EventifyPro will use STDOUT to print error details.
* You can provide any logger that responds to `.info(message)`. For example `Rails.logger`.

Example:
{'fraction_non_alphanumeric': 0.122334455667789, 'fraction_numerical': 0.00411522633744856, 'mean_word_length': 4.6284210526315785, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6140491', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1628, 'n_words': 564}