Class: Elasticsearch::Resources::ResponseFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/elasticsearch/resources/response_factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource:, action:, response:) ⇒ ResponseFactory

Returns a new instance of ResponseFactory.



6
7
8
9
10
# File 'lib/elasticsearch/resources/response_factory.rb', line 6

def initialize(resource:, action:, response:)
  @resource = resource
  @action = action
  @response = response
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



4
5
6
# File 'lib/elasticsearch/resources/response_factory.rb', line 4

def action
  @action
end

#resourceObject (readonly)

Returns the value of attribute resource.



4
5
6
# File 'lib/elasticsearch/resources/response_factory.rb', line 4

def resource
  @resource
end

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/elasticsearch/resources/response_factory.rb', line 4

def response
  @response
end

Instance Method Details

#buildObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/elasticsearch/resources/response_factory.rb', line 12

def build
  case action
  when :get
    build_get
  when :search
    build_search
  else
    response
  end
end

#build_getObject



23
24
25
26
27
28
# File 'lib/elasticsearch/resources/response_factory.rb', line 23

def build_get
  DocumentFactory.new(
    content: response,
    resources: resources_for(response)
  ).build
end

#build_searchObject



30
31
32
33
34
35
36
37
38
# File 'lib/elasticsearch/resources/response_factory.rb', line 30

def build_search
  raw_documents = response['hits']['hits']
  raw_documents.collect do |raw_document|
    DocumentFactory.new(
      content: raw_document,
      resources: resources_for(raw_document)
    ).build
  end
end

#resources_for(content) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/elasticsearch/resources/response_factory.rb', line 40

def resources_for(content)
  {
    cluster: resource.find_cluster,
    index: resource.find_index(index: content['_index']),
    type: resource.find_type(index: content['_index'], type: content['_type'])
  }
end