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

Returns a new instance of Documents.



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

def initialize
  uri = [SearchKit.config.app_uri, "documents"].join("/")
  @connection = Faraday.new(uri)
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



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

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



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

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



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

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



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

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