Class: Cabin::Outputs::ElasticSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/cabin/outputs/elastic_search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ ElasticSearch

Returns a new instance of ElasticSearch.



7
8
9
10
11
12
# File 'lib/cabin/outputs/elastic_search.rb', line 7

def initialize(args={})
  logger = args.delete 'logger'
  self.settings = {index: 'cabin', type: 'cabin'}.merge(args)
  sanity_check!
  self.es = Elasticsearch::Client.new urls: settings[:urls], logger: logger
end

Instance Attribute Details

#esObject

Returns the value of attribute es.



5
6
7
# File 'lib/cabin/outputs/elastic_search.rb', line 5

def es
  @es
end

#settingsObject

Returns the value of attribute settings.



5
6
7
# File 'lib/cabin/outputs/elastic_search.rb', line 5

def settings
  @settings
end

Instance Method Details

#<<(event) ⇒ Object



22
23
24
25
26
27
# File 'lib/cabin/outputs/elastic_search.rb', line 22

def <<(event)
  type = event.delete :_type
  type = event.delete "_type" if type.nil?
  type = settings[:type] if type.nil?
  es.index index: settings[:index], type: type.to_s, body: event
end

#sanity_check!Object



14
15
16
17
18
19
20
# File 'lib/cabin/outputs/elastic_search.rb', line 14

def sanity_check!
  settings[:urls] ||= [settings.delete(:url)] if settings.key? :url
  [:index, :type].each {|x| settings[x] = settings[x].to_s}
  raise Exception.new 'Index must be not blank for Cabin::Outputs::ElasticSearch' if settings[:index] =~ /^\s*$/
  raise Exception.new 'Type must be not blank for Cabin::Outputs::ElasticSearch' if settings[:type] =~ /^\s*$/
  raise Exception.new 'At least one URL must be provided to Cabin::Outputs::ElasticSearch' if settings[:urls].nil? or settings[:urls].count == 0
end