Class: SearchKit::Documents

Inherits:
Object
  • Object
show all
Defined in:
lib/search_kit/documents.rb,
lib/search_kit/documents/cli.rb

Defined Under Namespace

Classes: CLI

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDocuments



11
12
13
# File 'lib/search_kit/documents.rb', line 11

def initialize
  @connection = SearchKit::Client.connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



9
10
11
# File 'lib/search_kit/documents.rb', line 9

def connection
  @connection
end

Instance Method Details

#create(slug, options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/search_kit/documents.rb', line 15

def create(slug, options)
  document = { data: { type: "documents", attributes: options } }
  response = connection.post(slug, document)
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::BadRequest    if response.status == 400
  fail Errors::IndexNotFound if response.status == 404
  fail Errors::Unprocessable if response.status == 422

  body
end

#delete(slug, id) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/search_kit/documents.rb', line 48

def delete(slug, id)
  response = connection.delete("#{slug}/#{id}")
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::IndexNotFound if response.status == 404

  body
end

#show(slug, id) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/search_kit/documents.rb', line 27

def show(slug, id)
  response = connection.get("#{slug}/#{id}")
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::IndexNotFound if response.status == 404

  body
end

#update(slug, id, options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/search_kit/documents.rb', line 36

def update(slug, id, options)
  document = { data: { type: "documents", id: id, attributes: options } }
  response = connection.patch("#{slug}/#{id}", document)
  body     = JSON.parse(response.body, symbolize_names: true)

  fail Errors::BadRequest    if response.status == 400
  fail Errors::IndexNotFound if response.status == 404
  fail Errors::Unprocessable if response.status == 422

  body
end