Client for EventifyPro API. Allows to publish events from Ruby-applications.
Add this line to your application's Gemfile:
gem 'eventify_pro'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install eventify_pro
This guide assumes you have created an account and obtained an API key from EventifyPro
It's really easy to start publishing events with EventifyPro, just 2 lines of code:
client = EventifyPro::Client.new(api_key: 'secret')
client.publish(type: 'OrderCreated', data: { order_id: 1, amount: 500 })
In a basic Ruby On Rails application you could create class under lib
directory:
# 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:
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
Don't forget to enable loading of the lib
directory in application.rb file:
# config/application.rb
config.autoload_paths << Rails.root.join('lib')
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
- By default,
EventifyPro::Client
will swallow errors. It will returntrue
orfalse
depending on the result of publishing. - It's possible to pass
raise_errors: true
. In that case client will throwEventifyPro::Error
exception if something went wrong.
Example:
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
- By default, client will use STDOUT to print error details.
- You can provide any logger that responds to
.info(message)
. For exampleRails.logger
.
Example:
client = EventifyPro::Client.new(api_key: 'secret', logger: Rails.logger)
By default publishing will happen in async way in the separate thread.
To publish event in synchronous way, just specify async: false
option
client = EventifyPro::Client.new(api_key: 'secret')
client.publish(type: 'OrderCreated', data: { amount: 500 }, async: false)
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Eventify project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.