Class: SearchKit::Clients::Populate
- Inherits:
-
Object
- Object
- SearchKit::Clients::Populate
- Defined in:
- lib/search_kit/clients/populate.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
- #create(slug, documents) ⇒ Object
- #delete(slug, ids) ⇒ Object
-
#initialize ⇒ Populate
constructor
A new instance of Populate.
- #update(slug, documents) ⇒ Object
Constructor Details
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
7 8 9 |
# File 'lib/search_kit/clients/populate.rb', line 7 def connection @connection end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
7 8 9 |
# File 'lib/search_kit/clients/populate.rb', line 7 def token @token end |
Instance Method Details
#create(slug, documents) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/search_kit/clients/populate.rb', line 15 def create(slug, documents) documents = documents.map do |document| { type: 'documents', id: document.fetch(:id, nil), attributes: document } end params = { token: token, data: documents } response = connection.post(slug, params) body = JSON.parse(response.body, symbolize_names: true) fail Errors::BadRequest if response.status == 400 fail Errors::Unauthorized if response.status == 401 fail Errors::IndexNotFound if response.status == 404 fail Errors::Unprocessable if response.status == 422 body end |
#delete(slug, ids) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/search_kit/clients/populate.rb', line 57 def delete(slug, ids) documents = ids.map do |id| { type: 'documents', id: id, attributes: { id: id } } end params = { token: token, data: documents } response = connection.delete(slug, params) body = JSON.parse(response.body, symbolize_names: true) fail Errors::BadRequest if response.status == 400 fail Errors::Unauthorized if response.status == 401 fail Errors::IndexNotFound if response.status == 404 fail Errors::Unprocessable if response.status == 422 body end |
#update(slug, documents) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/search_kit/clients/populate.rb', line 36 def update(slug, documents) documents = documents.map do |document| { type: 'documents', id: document.fetch(:id, nil), attributes: document } end params = { token: token, data: documents } response = connection.patch(slug, params) body = JSON.parse(response.body, symbolize_names: true) fail Errors::BadRequest if response.status == 400 fail Errors::Unauthorized if response.status == 401 fail Errors::IndexNotFound if response.status == 404 fail Errors::Unprocessable if response.status == 422 body end |