Class: Ej::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/ej/util.rb

Class Method Summary collapse

Class Method Details

.generate_id(template, record, id_keys) ⇒ Object



15
16
17
# File 'lib/ej/util.rb', line 15

def self.generate_id(template, record, id_keys)
  template % id_keys.map { |key| record[key] }
end

.get_sources(results) ⇒ Object



19
20
21
# File 'lib/ej/util.rb', line 19

def self.get_sources(results)
  results.hits.hits.map { |result| result._source }
end

.get_transport(hosts) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ej/util.rb', line 23

def self.get_transport(hosts)
  transport = ::Elasticsearch::Transport::Transport::HTTP::Faraday.new(
    {
      hosts: hosts,
      options: {
        reload_connections: true,
        reload_on_failure: false,
        retry_on_failure: 5,
        transport_options: {
          headers: { 'Content-Type' => 'application/json' },
          request: { timeout: 300 }
        }
      }
    }
  )
  return transport
end

.parse_hosts(host_string, user = nil, password = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/ej/util.rb', line 41

def self.parse_hosts(host_string, user = nil, password = nil)
  host, port = (host_string || DEFAULT_HOST), DEFAULT_PORT
  if !host_string.nil? && host_string.include?(":")
    host, port = host_string.split(':')
  end

  hosts = [{ host: host, port: port.to_i, user: user, password: password }]
  return hosts
end

.parse_json(buffer) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/ej/util.rb', line 3

def self.parse_json(buffer)
  begin
    data = JSON.parse(buffer)
  rescue => e
    data = []
    buffer.lines.each do |line|
      data << JSON.parse(line)
    end
  end
  data.class == Array ? data : [data]
end