Added NATS

Andrew Kane committed Jan 25, 2017
commit a498920a24541e20cfc8eeaa913f7fac593110d8
Showing 5 changed files with 76 additions and 0 deletions
README.md +17 -0
@@ @@ -32,6 +32,7 @@ Ahoy supports a number of data stores out of the box. You can start with one of
- [Kafka](#kafka)
- [Fluentd](#fluentd)
- [RabbitMQ](#rabbitmq)
+ - [NATS](#nats-master)
- [Amazon Kinesis Firehose](#amazon-kinesis-firehose)
- [Logs](#logs)
- [Custom](#custom)
@@ @@ -99,6 +100,22 @@ rails generate ahoy:stores:bunny
Use `ENV["RABBITMQ_URL"]` to configure.
+ ### NATS [master]
+
+ Add [nats-pure](https://github.com/nats-io/pure-ruby-nats) to your Gemfile.
+
+ ```ruby
+ gem 'nats-pure'
+ ```
+
+ And run:
+
+ ```sh
+ rails generate ahoy:stores:nats
+ ```
+
+ Use `ENV["NATS_URL"]` to configure.
+
### Amazon Kinesis Firehose
Add [aws-sdk](https://github.com/aws/aws-sdk-ruby) to your Gemfile.
ahoy.rb b/lib/ahoy.rb +1 -0
@@ @@ -27,6 +27,7 @@ require "ahoy/stores/log_store"
require "ahoy/stores/fluentd_store"
require "ahoy/stores/mongoid_store"
require "ahoy/stores/kafka_store"
+ require "ahoy/stores/nats_store"
require "ahoy/stores/kinesis_firehose_store"
require "ahoy/stores/bunny_store"
require "ahoy/engine" if defined?(Rails)
ahoy/stores/nats_store.rb b/lib/ahoy/stores/nats_store.rb +34 -0
@@ @@ -0,0 +1,34 @@
+ module Ahoy
+ module Stores
+ class NatsStore < LogStore
+ def log_visit(data)
+ publish(visits_subject, data)
+ end
+
+ def log_event(data)
+ publish(events_subject, data)
+ end
+
+ def publish(subject, data)
+ client.publish(subject, data.to_json)
+ end
+
+ def client
+ @client ||= begin
+ require "nats/io/client"
+ client = NATS::IO::Client.new
+ client.connect(servers: (ENV["NATS_URL"] || "nats://127.0.0.1:4222").split(","))
+ client
+ end
+ end
+
+ def visits_subject
+ "ahoy_visits"
+ end
+
+ def events_subject
+ "ahoy_events"
+ end
+ end
+ end
+ end
generators/ahoy/stores/nats_generator.rb b/lib/generators/ahoy/stores/nats_generator.rb +15 -0
@@ @@ -0,0 +1,15 @@
+ require "rails/generators"
+
+ module Ahoy
+ module Stores
+ module Generators
+ class NatsGenerator < Rails::Generators::Base
+ source_root File.expand_path("../templates", __FILE__)
+
+ def create_initializer
+ template "nats_initializer.rb", "config/initializers/ahoy.rb"
+ end
+ end
+ end
+ end
+ end
generators/ahoy/stores/templates/nats_initializer.rb b/lib/generators/ahoy/stores/templates/nats_initializer.rb +9 -0
@@ @@ -0,0 +1,9 @@
+ class Ahoy::Store < Ahoy::Stores::NatsStore
+ def visits_subject
+ "ahoy_visits"
+ end
+
+ def events_subject
+ "ahoy_events"
+ end
+ end