Class: YogaPants::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/yoga_pants/client.rb

Defined Under Namespace

Classes: ElasticSearchError, HTTPRequestError, RequestError

Constant Summary collapse

BULK_OPERATIONS_WITH_DATA =
[:index, :create].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hosts, options = {}) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
# File 'lib/yoga_pants/client.rb', line 10

def initialize(hosts, options = {})
  @hosts       = [hosts].flatten.freeze # Accept 1 or more hosts
  @options     = options
  @max_retries = options[:max_retries] || 10
  @retries     = 0
  reset_hosts
end

Instance Attribute Details

#active_hostObject

This class will handle:

* connecting to ES nodes
* failing over to nodes in a list
* ES-specific error handling


8
9
10
# File 'lib/yoga_pants/client.rb', line 8

def active_host
  @active_host
end

#hostsObject

This class will handle:

* connecting to ES nodes
* failing over to nodes in a list
* ES-specific error handling


8
9
10
# File 'lib/yoga_pants/client.rb', line 8

def hosts
  @hosts
end

#optionsObject

This class will handle:

* connecting to ES nodes
* failing over to nodes in a list
* ES-specific error handling


8
9
10
# File 'lib/yoga_pants/client.rb', line 8

def options
  @options
end

Instance Method Details

#bulk(path, operations, args = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/yoga_pants/client.rb', line 48

def bulk(path, operations, args = {})
  return [] if operations.empty?

  path = path.sub(%r{/(?:_bulk)?$}, '/_bulk')

  with_error_handling do
    payload = StringIO.new

    operations.each do |action, , data|
      payload << JSON.dump({action => })
      payload << "\n"
      if BULK_OPERATIONS_WITH_DATA.include?(action.to_sym)
        payload << JSON.dump(data)
        payload << "\n"
      end
    end

    payload.rewind
    connection.post(path, :query_string => args, :body => payload.read)
  end
end

#delete(path, args = {}) ⇒ Object



41
42
43
44
45
# File 'lib/yoga_pants/client.rb', line 41

def delete(path, args = {})
  with_error_handling do
    connection.delete(path, args)
  end
end

#exists?(path, args = {}) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/yoga_pants/client.rb', line 86

def exists?(path, args = {})
  with_error_handling do
    begin
      if path.count("/") >= 3 # More than
        connection.get(path, args)
      else
        connection.head(path).status_code == 200
      end
    rescue Connection::HTTPError => e
      if e.status_code == 404
        false
      else
        raise e
      end
    end
  end
end

#get(path, args = {}) ⇒ Object



23
24
25
26
27
# File 'lib/yoga_pants/client.rb', line 23

def get(path, args = {})
  with_error_handling do
    connection.get(path, args)
  end
end

#multi_search(path, operations, args = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/yoga_pants/client.rb', line 70

def multi_search(path, operations, args={})
  path = path.sub(%r{/?(?:_msearch)?$}, '/_msearch')

  with_error_handling do
    payload = StringIO.new

    operations.each do |header, body|
      payload << JSON.dump(header) << "\n"
      payload << JSON.dump(body) << "\n"
    end

    payload.rewind
    connection.get(path, :query_string => args, :body => payload.read)
  end
end

#post(path, args = {}) ⇒ Object



29
30
31
32
33
# File 'lib/yoga_pants/client.rb', line 29

def post(path, args = {})
  with_error_handling do
    connection.post(path, args)
  end
end

#put(path, args = {}) ⇒ Object



35
36
37
38
39
# File 'lib/yoga_pants/client.rb', line 35

def put(path, args = {})
  with_error_handling do
    connection.put(path, args)
  end
end

#resetObject



104
105
106
# File 'lib/yoga_pants/client.rb', line 104

def reset
  connection.reset
end

#reset_hostsObject



18
19
20
21
# File 'lib/yoga_pants/client.rb', line 18

def reset_hosts
  @active_hosts = hosts.dup
  @active_host = @active_hosts.shift
end